コード例 #1
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);
                }
            }
        }