/// <summary>
        /// Get a command which should handle the specified message.
        /// </summary>
        /// <param name="commandContext">Associated command context.</param>
        /// <returns>Instance of a command which should handle the message.</returns>
        private ICommand GetAssociatedCommand(int senderId, long chatId, string messageText, out CommandContext commandContext)
        {
            messageText    = messageText ?? string.Empty;
            commandContext = new CommandContext
            {
                ChatId     = chatId,
                Message    = messageText,
                RawMessage = messageText
            };
            var    matchCommand = CommandNameRegex.Match(messageText);
            string commandName  = string.Empty;

            if (matchCommand.Success)
            {
                commandName = matchCommand.Value
                              .TrimEnd()
                              .Substring(1) // Omit the / symbol
                              .ToLowerInvariant();
                commandContext.Message = messageText.Substring(matchCommand.Length).Trim();
            }
            commandContext.Command = commandName;

            var conversationContext = conversationService.GetAssociatedContext(chatId);

            if (conversationContext.CurrentExecutingCommand != null)
            {
                return(serviceProvider.GetService(conversationContext.CurrentExecutingCommand) as ICommand);
            }

            if (commandName != string.Empty)
            {
                Type commandType = null;
                if (CommandList.PublicCommands.ContainsKey(commandName))
                {
                    commandType = CommandList.PublicCommands[commandName];
                }

                if (commandType == null)
                {
                    var senderIsAdmin = senderId == appSettings.HelpUserTelegramId;
                    if (senderIsAdmin)
                    {
                        switch (commandName)
                        {
                        case AdminCommandList.Broadcast:
                            commandType = typeof(BroadcastMessageCommand);
                            break;
                        }
                    }
                }
                if (commandType != null)
                {
                    return(serviceProvider.GetRequiredService(commandType) as ICommand);
                }
            }

            return(serviceProvider.GetService <NotifyUnknownCommandCommand>());
        }
Beispiel #2
0
        public async Task <ICommandExecutionResult> HandleCommand()
        {
            var conversation = conversationService.GetAssociatedContext(CommandContext.ChatId);

            if (conversation.CurrentExecutingCommand == null)
            {
                conversation.CurrentExecutingCommand = typeof(BroadcastMessageCommand);
                return(new TextResult("The next message will be broadcasted to all users.")
                {
                    AdditionalMarkup = new ReplyKeyboardMarkup(new[] { new KeyboardButton(CommandCancel) })
                    {
                        ResizeKeyboard = true
                    }
                });
            }

            conversation.CurrentExecutingCommand = null;
            if (CommandContext.RawMessage == CommandCancel)
            {
                conversation.ConversationData        = null;
                conversation.CurrentExecutingCommand = null;
                return(new TextResult()
                {
                    TextMessage = "Action cancelled.",
                    AdditionalMarkup = new ReplyKeyboardRemove()
                });
            }

            var resultMessages =
                (await chatService.GetActiveChats())
                .Select(chatId => (ICommandExecutionResult) new DirectTextResult(CommandContext.Message, chatId))
                .ToList();

            resultMessages.Add(new TextResult("Success")
            {
                AdditionalMarkup = new ReplyKeyboardRemove()
            });
            return(new MultipleResult(resultMessages));
        }
Beispiel #3
0
        public async Task <ICommandExecutionResult> HandleCommand()
        {
            var conversation = conversationService.GetAssociatedContext(CommandContext.ChatId);

            // This is the first message, should contain date of the daily report
            if (conversation.CurrentExecutingCommand == null)
            {
                var date = await CommandUtils.ParseDateFromMessage(CommandContext, uowFactory);

                conversation.ConversationData = new DailyReportCommandData
                {
                    DailyReportDate = date
                };
                conversation.CurrentExecutingCommand = typeof(UpdateDailyReportCommand);

                return(new TextResult($"Creating a daily report for {date:D}. Please enter your daily report text.")
                {
                    AdditionalMarkup = new ReplyKeyboardMarkup(new[] { new KeyboardButton(CommandSubmit), new KeyboardButton(CommandCancel) })
                    {
                        ResizeKeyboard = true
                    }
                });
            }

            var data = conversation.ConversationData as DailyReportCommandData;

            if (CommandContext.RawMessage == CommandSubmit)
            {
                if (string.IsNullOrEmpty(data.Message))
                {
                    return(new TextResult("Daily report with empty message cannot be created."));
                }

                conversation.ConversationData        = null;
                conversation.CurrentExecutingCommand = null;

                var success = await UpdateDailyReportAsync(data);

                telemetry.TrackEvent("Daily report created");
                string resultMessage = success ? "Successfuly created daily report." : "Unexpected error occurred while updating the daily report.";
                return(new TextResult(resultMessage)
                {
                    AdditionalMarkup = new ReplyKeyboardRemove()
                });
            }

            if (CommandContext.RawMessage == CommandCancel)
            {
                conversation.ConversationData        = null;
                conversation.CurrentExecutingCommand = null;
                return(new TextResult()
                {
                    TextMessage = "Daily report creation cancelled.",
                    AdditionalMarkup = new ReplyKeyboardRemove()
                });
            }

            if (!string.IsNullOrEmpty(data.Message))
            {
                data.Message += "\n";
            }
            data.Message += CommandContext.RawMessage;

            return(new EmptyResult());
        }