Exemple #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);
        }
Exemple #2
0
        private async void Clicked()
        {
            IDialogCommand response = await Model.WindowManager.ShowDialog(new DialogViewModel("You clicked me!", "Do you like what you are seeing?", DialogCommand.Yes, DialogCommand.No));

            string title   = response.Label == DialogCommand.Yes.Label ? "Hooray!" : "Oh well...";
            string message = response.Label == DialogCommand.Yes.Label
                ? "I'm glad you like it."
                : "I wonder how I can improve?";

            INotificationControl control = Model.WindowManager.Notify(new Message(title, message));

            control.AutoHideAfter(TimeSpan.FromSeconds(5));
            control.AutoCloseAfter(TimeSpan.FromSeconds(15));
        }
Exemple #3
0
        private void OnMessage(object sender, ITLMessage tLMessage)
        {
            Trace.TraceInformation($"{tLMessage.ChatId} {tLMessage.Message}");
            string message = tLMessage.Message;
            long   chatId  = tLMessage.ChatId;

            IChatDataInfo chatInfo = GetChatInfo(chatId);

            chatInfo.LastMessage = DateTime.Now;

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

            if (startCmd.IsMessageClear(message))
            {
                chatInfo.Clear();
            }

            if (message.ToLower().StartsWith("нет"))
            {
                if (chatInfo.ChatStep > 0)
                {
                    --chatInfo.ChatStep;
                }

                chatInfo.DialogState = DialogStateEnum.DialogReturned;

                var prevCommand = _commands.FirstOrDefault(cmd => cmd.Label == chatInfo.ChatStep);
                CommandAskQuestion(prevCommand, chatInfo, null);
                return;
            }

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

            if (!command.IsMessageClear(message))
            {
                SendWrongCommandMessage(chatId, message, chatInfo.ChatStep);
                return;
            }

            string acknowledgement = command.ApplyResult(chatInfo, message);

            ++chatInfo.ChatStep;

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

            CommandAskQuestion(nextCommand, chatInfo, acknowledgement);
        }
        public async Task <IDialogCommand> ShowDialog(DialogViewModel dialogViewModel)
        {
            IDialogCommand primaryCommand   = dialogViewModel.Commands.FirstOrDefault(cmd => cmd.IsPrimary) ?? dialogViewModel.Commands.FirstOrDefault();
            IDialogCommand secondaryCommand = dialogViewModel.Commands.FirstOrDefault(cmd => !cmd.IsPrimary) ?? dialogViewModel.Commands.LastOrDefault();

            if (dialogViewModel.Commands.Count == 1)
            {
                secondaryCommand = null;
            }

            ContentDialog dialog = new ContentDialog();

            if (primaryCommand != null)
            {
                dialog.PrimaryButtonText      = primaryCommand.Label;
                dialog.IsPrimaryButtonEnabled = true;
            }

            if (secondaryCommand != null)
            {
                dialog.SecondaryButtonText      = secondaryCommand.Label;
                dialog.IsSecondaryButtonEnabled = true;
            }

            BindingOperations.SetBinding(dialog, ContentDialog.TitleProperty, new Binding
            {
                Source = dialogViewModel.Model,
                Path   = new PropertyPath("Title")
            });

            dialog.Content = CreateAndBindView(dialogViewModel);

            IDialogCommand command = null;

            switch (await dialog.ShowAsync())
            {
            case ContentDialogResult.Primary:
                command = primaryCommand;
                break;

            case ContentDialogResult.Secondary:
                command = secondaryCommand;
                break;
            }

            return(command);
        }
Exemple #5
0
        private void CommandAskQuestion(IDialogCommand cmd, IChatDataInfo chatInfo, string previousCmdAcknowledgement)
        {
            if (cmd == null)
            {
                return;
            }

            string nextDlgQuestion = cmd.ExecuteAsync(chatInfo).GetAwaiter().GetResult();
            string botResponse     = string.IsNullOrWhiteSpace(previousCmdAcknowledgement)
                ? nextDlgQuestion
                : $"{previousCmdAcknowledgement}{Environment.NewLine}{nextDlgQuestion}";

            Task.Run(() => _botService.SendMessageAsync(chatInfo.ChatId, botResponse));

            if (_commands.FirstOrDefault(cmd => cmd.Label == chatInfo.ChatStep + 1) == null)
            {
                chatInfo.ChatStep = (int)DialogStep.Start;
            }
        }
        public void ChooseCommand(IDialogCommand command)
        {
            command?.Invoked?.Invoke(command);

            Result.SetResult(command);
        }
Exemple #7
0
 public DialogCommandExecuteEventArgs(IDialogCommand command)
 {
     Command = command;
 }
Exemple #8
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);
            }
        }
Exemple #9
0
 public void ExecuteCommand(IDialogCommand command)
 {
     command.Execute(this);
 }
Exemple #10
0
 public bool CanExecuteCommand(IDialogCommand command)
 {
     return(true);
 }