コード例 #1
0
ファイル: BotController.cs プロジェクト: oguzhankiyar/bitter
        public async Task <IActionResult> Update([FromBody] BotUpdateInput input)
        {
            UserEntity user = _userRepository.FindUser(input.Message.Chat.Id.ToString());

            _messageRepository.InsertMessage(new MessageEntity()
            {
                UserId = user?.Id,
                ChatId = input.Message.Chat.Id.ToString(),
                Text   = input.Message.Text,
                Date   = DateTime.Now
            });

            string commandString = (input.Message.Text + " ").Split(' ')[0].TrimStart('/');

            if (!commands.ContainsKey(commandString))
            {
                await _messageService.SendMessageAsync(input.Message.Chat.Id.ToString(), "Invalid command!");
            }
            else
            {
                Type commandType = commands[commandString];

                IBotCommand command = HttpContext.RequestServices.GetService(commandType) as IBotCommand;

                await command.ExecuteAsync(input);
            }

            return(Ok());
        }
コード例 #2
0
        private async Task ExecuteCommand(IBotCommand command, CommandEventArgs args)
        {
            Logger.Info($"Executing command '{command.Command}'");

            try
            {
                await args.Channel.DeleteMessages(new[] { args.Message });

                await command.ExecuteAsync(args);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
            }
        }
コード例 #3
0
        public async Task <OkResult> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                DecodeActivityText(activity);

                string replyText = null;
                ParsedCommandRequest parsedCommandRequest = commandRequestParser.Parse(activity);

                if (parsedCommandRequest != null)
                {
                    IBotCommand command = commandSelector.GetCommand(parsedCommandRequest.Name);

                    if (command != null)
                    {
                        ExecutionResult <string> executionResult =
                            await command.ExecuteAsync(activity, parsedCommandRequest.Arguments);

                        replyText = executionResult.IsSuccess
                                                        ? executionResult.Entity
                                                        : executionResult.ErrorMessage;
                    }
                }

                if (replyText == null)
                {
                    replyText = "Sorry, I don't understand you :(";
                }

                logger.LogInformation("Replying with '{0}'", replyText);

                await messageProcessor.ReplyAsync(activity, replyText, CancellationToken.None);
            }

            return(Ok());
        }