-
Notifications
You must be signed in to change notification settings - Fork 0
2. Creating controller
In order to start working with controllers, you must first create a class and put the @BotController
annotation. Example is show below.
@BotController
public class SayController {
}
Now you can define a handler method, to do this, create a new method with the @CommandMapping
annotation.
Because we want to process text messages, we have specified the event
parameter as MessageType.TEXT_MESSAGE
. Although this is not necessary, since it has such a value by default. In the value
parameter, we specify the path or the name of the command that we want to handle.
@CommandMapping(
value = {"say"},
event = MessageType.TEXT_MESSAGE
)
public SendMessage say(TelegramMessage message) {
return SendMessage.builder()
.chatId(String.valueOf(message.getChatId()))
.text("Something")
.build();
}
Let's add handling of some command arguments. In our case, this will be say {input}
.
In curly braces, we can specify command parameters. In order to access such arguments later, we must specify a parameter in the method and annotate it with the annotation @PathVariable
, we can also set the parameter name in the annotation parameters if it differs from what we use in the method, since the name in the annotation and the name of the parameter in the method are the same, we will skip this step.
@CommandMapping(
value = {"say {input}"},
event = MessageType.TEXT_MESSAGE
)
public SendMessage say(TelegramMessage message, @PathVariable String input) {
return SendMessage.builder()
.chatId(String.valueOf(message.getChatId()))
.text(input)
.build();
}
As you have seen, we can also receive arguments like TelegramMessage
, in addition to this we can receive an AbsSender
argument that will the bot that received the command. You can also register your own argument providers and receive them in handler methods.
As a result, we return SendMessage
, this is not single one possible type, if you have other result handlers defined that are suitable for your result type.
To configure result handlers and argument providers you can use TelegramMvcConfigurer
.