Ejemplo n.º 1
0
        public static void ValidateUpload <T>(this ITextConverter converter, T sourceObject, string convertedText)
        {
            var reconvertedObject = converter.ToObject <object>(convertedText);

            var reconvertToText = converter.ToText(reconvertedObject);

            if (convertedText != reconvertToText)
            {
                throw new ArgumentException(string.Format("Could not convert {0} consistently.\r\n"
                                                          + "Either the object is not a POCO with public get and set properties,"
                                                          + "or your ITextConverter {1} does not suppport the type {2}.",
                                                          sourceObject.ToString(), converter.ToString(), typeof(T).ToString()));
            }
        }
Ejemplo n.º 2
0
        private async void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            if (!itemsQueue.Any())
            {
                return;
            }

            var item = itemsQueue.Dequeue();

            try
            {
                var newInfo = item.Source switch
                {
                    PullAndBear.SHOP_NAME => await _pullAndBearClient.GetItemInfoAsync(item.Url),
                    Bershka.SHOP_NAME => await _bershkaClient.GetItemInfoAsync(item.Url),
                    _ => throw new Exception(Exceptions.UNKNOWN_SHOP_EXCEPTION),
                };
                if (item.Status != newInfo.Status || item.Price != newInfo.Price)
                {
                    await _botService.SendMessageMarkdownV2Async(
                        item.ChatId,
                        _textConverter.ToString(item, newInfo));
                }
                _updateInfoHelper.GetUpdatedItem(ref item, newInfo);
                await _trackingRepository.UpdateInfoOfItemAsync(item);
            }
            catch (Exception ex)
            {
                // it's ok; lets update item information next time;
            }
            if ((DateTime.Now - item.LastUpdateDate).TotalHours > maxTimeWithoutUpdateIntervalHours)
            {
                await _trackingRepository.RemoveItemAsync(item.Url);

                await _botService.SendMessageAsync(
                    item.ChatId,
                    WANNA_REMOVE_ITEM_EXCEPTION);
            }
            else
            {
                itemsQueue.Enqueue(item);
                if ((DateTime.Now - item.LastUpdateDate).TotalHours > maxUpdateTimeIntervalHours)
                {
                    await _botService.SendMessageAsync(
                        item.ChatId,
                        CANT_UPDATE_EXCEPTION);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task ReplyAsync(Update update)
        {
            if (update.CallbackQuery != null)
            {
                var message = update.CallbackQuery.Message;
                var input   = update.CallbackQuery.Data;

                switch (input)
                {
                case Commands.REMOVE:
                    try
                    {
                        await _shopService.RemoveItemAsync(message);

                        await _botService.SendMessageAsync(
                            message.Chat.Id,
                            message.MessageId,
                            Exceptions.REMOVED);
                    }
                    catch (Exception ex)
                    {
                        await _botService.SendMessageMarkdownV2Async(
                            message.Chat.Id,
                            message.MessageId,
                            ex.Message);
                    }
                    break;

                default:
                    break;
                }
            }
            if (update.Type == UpdateType.Message)
            {
                var message = update.Message;
                if (message.Type != MessageType.Text)
                {
                    return;
                }

                var input = message.Text;
                if (input == null)
                {
                    return;
                }

                try
                {
                    var isUserStatusExists = await _trackingRepository.IsUserStatusExistsAsync(message.From.Id);

                    var isWaitingForAdd = isUserStatusExists ? await _trackingRepository.IsWaitingForAddAsync(message.From.Id) :
                                          await _trackingRepository.AddUserStatusAsync(message.From.Id);

                    if (input.StartsWith(Commands.SLASH_ADD) || input.StartsWith(Commands.ADD_BOT))
                    {
                        await _trackingRepository.SetWaitingForAddAsync(message.From.Id, false);

                        await _shopService.AddNewItemAsync(message);
                    }
                    else
                    {
                        switch (input)
                        {
                        case Commands.ADD:
                            await _trackingRepository.SetWaitingForAddAsync(message.From.Id, true);

                            throw new Exception(Exceptions.NEED_LINK_EXCEPTION);

                        case Commands.CANCEL:
                            await _trackingRepository.SetWaitingForAddAsync(message.From.Id, false);

                            return;

                        case Commands.SLASH_CART:
                        case Commands.CART:
                        case Commands.CART_BOT:
                            await _trackingRepository.SetWaitingForAddAsync(message.From.Id, false);

                            var items = await _shopService.GetTrackedItemsAsync(message.From.Id);

                            if (!items.Any())
                            {
                                if (message.From.Username != null)
                                {
                                    await _botService.SendMessageMarkdownV2Async(
                                        message.Chat.Id,
                                        $@"[@{message.From.Username}](tg://user?id={message.From.Id}) {Exceptions.EMPTY_CART}");
                                }
                                else
                                {
                                    await _botService.SendMessageAsync(
                                        message.Chat.Id,
                                        message.MessageId,
                                        Exceptions.EMPTY_CART);
                                }
                                return;
                            }
                            var textItems = _textConverter.ToStrings(items);
                            foreach (var item in textItems)
                            {
                                if (message.From.Username != null)
                                {
                                    await _botService.SendMessageButtonMarkdownV2Async(
                                        message.Chat.Id,
                                        $@"{item}
[@{message.From.Username}](tg://user?id={message.From.Id})");
                                }
                                else
                                {
                                    await _botService.SendMessageButtonMarkdownV2Async(
                                        message.Chat.Id,
                                        message.MessageId,
                                        item);
                                }
                            }
                            break;

                        case Commands.SLASH_START:
                        case Commands.START_BOT:
                        case Commands.SLASH_HELP:
                        case Commands.HELP:
                        case Commands.HELP_BOT:
                            await _trackingRepository.SetWaitingForAddAsync(message.From.Id, false);

                            await _botService.SendMessageAsync(
                                message.Chat.Id,
                                $"{HELP_TEXT}");

                            break;

                        default:
                            var url = Regex.Match(input, Common.UrlTemplate).Value;
                            if (string.IsNullOrEmpty(url))
                            {
                                if (isWaitingForAdd)
                                {
                                    throw new Exception(Exceptions.NEED_CORRECT_LINK_EXCEPTION);
                                }
                                else
                                {
                                    return;
                                }
                            }

                            await _trackingRepository.SetWaitingForAddAsync(message.From.Id, false);

                            var newItem = await _shopService.AddNewItemAsync(message);

                            await _botService.SendMessageButtonMarkdownV2Async(
                                message.Chat.Id,
                                message.MessageId,
                                _textConverter.ToString(newItem));

                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    await _botService.SendMessageAsync(
                        message.Chat.Id,
                        message.MessageId,
                        ex.Message);
                }
            }
        }