Exemplo n.º 1
0
        public async Task HandleCallbackQuery(TGCallbackQueryDTO dto)
        {
            var queryData = JsonConvert.DeserializeObject <TGCallbackQueryDTO.TGCallbackQueryDataDTO>(dto.Data);

            if (queryData.PageIndex == 0)
            {
                return;
            }
            var command = await _userService.FindLastCommandByTGUserId(dto.From.Id);

            var message = await _repository.TableNoTracking.FirstOrDefaultAsync(s => s.TGMessageId == queryData.MessageId);

            if (command == null || message == null)
            {
                return;
            }
            if (command.Type == UserCommandType.List)
            {
                var category = command.Text;
                if (!string.IsNullOrWhiteSpace(category) && category.StartsWith("全部群组", StringComparison.OrdinalIgnoreCase))
                {
                    category = null;
                }

                var paged         = _chatService.GetChatsPaged(pagedIndex: queryData.PageIndex, pageSize: 20, category: category);
                var messageDTO    = TGSendMessageDTO.BuildChatListMessage(paged, dto.Message.Chat.Id, queryData.MessageId);
                var editedMessage = TGSendMessageDTO.BuildChatListEditMessage(messageDTO, message.ResponseId);
                await _telegramHttpClient.EditMessage(editedMessage);
            }
        }
Exemplo n.º 2
0
        private async Task HandleCommand(TGMessageDTO dto, UserCommand command, Message message)
        {
            switch (command.Type)
            {
            case UserCommandType.Help:
            {
                command.Completed = true;
                await _userService.CompleteCommand(command);

                await _telegramHttpClient.SendMessage(TGSendMessageDTO.BuildHelpMessage(dto.Chat.Id));

                break;
            }

            case UserCommandType.Reset:
            {
                command.Completed = true;
                await _userService.CompleteCommand(command);

                await _telegramHttpClient.SendMessage(TGSendMessageDTO.BuildResetMessage(dto.Chat.Id));

                break;
            }

            case UserCommandType.List:
            {
                if (ApplicationDefaults.Commands.Contains(dto.Text))
                {
                    var categories = await _chatService.GetChatCategories();

                    var messageRequest = TGSendMessageDTO.BuildChatCategoriesMessage(dto.Chat.Id, categories.OrderBy(c => c.DisplayOrder).Select(c => c.Name).ToList());
                    await _telegramHttpClient.SendMessage(messageRequest);
                }
                else
                {
                    var category = dto.Text;
                    if (!string.IsNullOrWhiteSpace(category) && category.StartsWith("全部群组", StringComparison.OrdinalIgnoreCase))
                    {
                        category = null;
                    }
                    var paged           = _chatService.GetChatsPaged(pagedIndex: 1, pageSize: 20, category: category);
                    var messageRequest  = TGSendMessageDTO.BuildChatListMessage(paged, dto.Chat.Id, dto.MessageId);
                    var messageResponse = await _telegramHttpClient.SendMessage(messageRequest);

                    message.ResponseId = messageResponse.Result.MessageId;
                    await _repository.UpdateAsync(message);
                }
                break;
            }

            case UserCommandType.MyList:
            {
                //TODO
                break;
            }

            case UserCommandType.Join:
            {
                if (ApplicationDefaults.Commands.Contains(dto.Text))
                {
                    await _telegramHttpClient.SendMessage(TGSendMessageDTO.BuildChatJoinMessage(dto.Chat.Id));
                }
                else
                {
                    _eventBus.Publish(new TelegramChatJoinEvent(dto.Chat.Id, dto.From.Id, dto.Text));
                }
                break;
            }

            case UserCommandType.Remove:
            {
                await _telegramHttpClient.SendMessage(TGSendMessageDTO.BuildChatRemoveMessage(dto.Chat.Id));

                break;
            }

            case UserCommandType.Update:
            {
                await _telegramHttpClient.SendMessage(TGSendMessageDTO.BuildChatUpdateMessage(dto.Chat.Id));

                break;
            }

            case UserCommandType.Search:
            {
                if (ApplicationDefaults.Commands.Contains(dto.Text))
                {
                    await _telegramHttpClient.SendMessage(TGSendMessageDTO.BuildChatSearchMessage(dto.Chat.Id));
                }
                else
                {
                    var paged           = _chatService.GetChatsPaged(pagedIndex: 1, pageSize: 20, keywords: dto.Text);
                    var messageRequest  = TGSendMessageDTO.BuildChatListMessage(paged, dto.Chat.Id, dto.MessageId);
                    var messageResponse = await _telegramHttpClient.SendMessage(messageRequest);

                    await _repository.UpdateAsync(message);
                }
                break;
            }
            }
        }