Beispiel #1
0
        private async Task CommandAskQuestion(IDialogCommand cmd, ChatInfoEntity chatInfo, ITgOutboundMessage previousCmdAcknowledgement)
        {
            if (cmd == null)
            {
                return;
            }

            ITgOutboundMessage nextDlgQuestion = await cmd.AscUserAsync(chatInfo, _cancellationTokenSource.Token);

            ITgOutboundMessage botResponse = nextDlgQuestion;

            if (!string.IsNullOrWhiteSpace(previousCmdAcknowledgement?.Message))
            {
                botResponse.Message = $"{previousCmdAcknowledgement.Message}{Environment.NewLine}{nextDlgQuestion.Message}";
            }

            if (null != previousCmdAcknowledgement?.ReplyKeyboard)
            {
                botResponse.ReplyKeyboard = new ReplyKeyboardMarkup
                {
                    Keyboard = botResponse.ReplyKeyboard?.Keyboard != null
                        ? previousCmdAcknowledgement.ReplyKeyboard.Keyboard.Concat(botResponse.ReplyKeyboard.Keyboard)
                        : previousCmdAcknowledgement.ReplyKeyboard?.Keyboard,
                    OneTimeKeyboard = botResponse.ReplyKeyboard?.OneTimeKeyboard ?? true,
                    ResizeKeyboard  = botResponse.ReplyKeyboard?.ResizeKeyboard ?? true
                };
            }

            await Task.Run(() => _botService.SendMessageAsync(chatInfo.ChatId, botResponse), _cancellationTokenSource.Token);
        }
Beispiel #2
0
        public async Task <bool> Delete(ChatInfoEntity chatData)
        {
            _dbContext.TlChats.Remove(chatData);

            try
            {
                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                Trace.TraceError($"Failed to delete chat item {ex.Message}");
                return(false);
            }
        }
Beispiel #3
0
        public async Task <ChatInfoEntity> Create(long chatId, string culture, CancellationToken cancellationToken)
        {
            try
            {
                var entity = new ChatInfoEntity {
                    ChatId = chatId, Culture = culture
                };
                _dbContext.TlChats.Add(entity);
                await _dbContext.SaveChangesAsync(cancellationToken);

                return(entity);
            }
            catch (Exception ex)
            {
                Trace.TraceInformation($"DbException {ex.Message}");
                return(null);
            }
        }
Beispiel #4
0
        public async Task <bool> Update(ChatInfoEntity newValue)
        {
            ChatInfoEntity oldValue = await GetChatInfoById(newValue.ChatId);

            if (oldValue == null)
            {
                return(false);
            }

            _dbContext.Entry(newValue).State = EntityState.Modified;

            try
            {
                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                Trace.TraceError($"Failed to update chat item {ex.Message}");
                return(false);
            }
        }
Beispiel #5
0
        private async void OnMessage(object sender, ITgInboundMessage tLInboundMessage)
        {
            Trace.TraceInformation($"{tLInboundMessage.ChatId} {tLInboundMessage.Message}");
            string message = tLInboundMessage.Message;
            long   chatId  = tLInboundMessage.ChatId;

            if (IsAdminCommand(tLInboundMessage))
            {
                return;
            }

            using ITgChatsRepository chatsRepository = _dbService.GetChatsRepository();

            ChatInfoEntity chatInfo = await chatsRepository.Get(chatId)
                                      ?? await chatsRepository.Create(chatId, "ru", _cancellationTokenSource.Token);

            chatInfo.LastMessage = DateTime.Now;

            //check if user wants to return at first
            var startCmd = _commands.First();

            if (startCmd.IsMessageCorrect(message))
            {
                using (var usersRepository = _dbService.GetUsersRepository())
                {
                    var userEntity = usersRepository.Get(chatInfo.ChatId);
                    if (null == userEntity)
                    {
                        await usersRepository.Create(chatInfo.ChatId, chatInfo.Culture, _cancellationTokenSource.Token);
                    }
                }

                chatInfo.Clear();
            }

            var prevCmd = GetPreviousCommand(chatInfo);

            if (message.ToLower().StartsWith("нет") || (prevCmd?.IsReturnCommand(message) ?? false))
            {
                var prevCommand = GetPreviousCommand(chatInfo);

                if (prevCommand == null)
                {
                    return;
                }

                chatInfo.DialogState     = DialogStateEnum.DialogReturned;
                chatInfo.CurrentStepId  -= 1;
                chatInfo.PreviousStepId -= 1;

                await CommandAskQuestion(prevCommand, chatInfo, null);

                if (null == GetNextCommand(chatInfo))
                {
                    await chatsRepository.Delete(chatInfo);
                }
                else
                {
                    await chatsRepository.Update(chatInfo);
                }

                return;
            }

            IDialogCommand command = _commands.FirstOrDefault(cmd => cmd.Label == chatInfo.CurrentStepId);

            if (!command.IsMessageCorrect(message))
            {
                SendWrongCommandMessage(chatId, message, chatInfo.CurrentStepId);
                return;
            }

            ITgOutboundMessage acknowledgement =
                await command.ApplyResultAsync(chatInfo, message, _cancellationTokenSource.Token);

            chatInfo.PreviousStepId = chatInfo.CurrentStepId;
            ++chatInfo.CurrentStepId;
            await chatsRepository.Update(chatInfo);

            var nextCommand = _commands.FirstOrDefault(cmd => cmd.Label == chatInfo.CurrentStepId);

            await CommandAskQuestion(nextCommand, chatInfo, acknowledgement);

            if (null == GetNextCommand(chatInfo))
            {
                await chatsRepository.Delete(chatInfo);
            }
        }