コード例 #1
0
        private async void OnMessage(object sender, MessageEventArgs arguments)
        {
            var message = arguments.Message;

            if (message.Text != null)
            {
                try
                {
                    string      command = ExtractCommand(message.Text);
                    IBotCommand handler = CommandSelector.MapCommandToHandler(command);
                    await handler.Handle(message, this._botClient, this._pokeApiClient);
                }
                catch (Exception exception)
                {
                    // Workaround since C# does not support multi catch as in Java
                    if (exception is UnknownCommandException || exception is NoCommandHandlerException)
                    {
                        await this._botClient.SendTextMessageAsync(
                            chatId : message.Chat,
                            text : "Unfortunately the bot does not support this command. Enter /help or /start for more options."
                            );
                    }
                    else
                    {
                        Console.WriteLine(exception.Message);
                        Console.WriteLine(exception.StackTrace);
                        await this._botClient.SendTextMessageAsync(
                            chatId : message.Chat,
                            text : "500 - Internal Server Error"
                            );
                    }
                }
            }
        }