How event mapping and argument handling works
masterThe SDK uses reflection to match incoming webhook events to methods annotated with @EventMapping.
Argument Rules:
- Event Objects: You can request an object that implements
Event(e.g.,MessageEvent,FollowEvent). - Message Content: You can request specific
MessageContentobjects (e.g.,TextMessageContent) as arguments to simplify access to message data. - Destination: You can capture the
destinationfield from the LINE request body by using the@LineBotDestinationannotation on aStringparameter. This parameter must be the first argument in the method. - Return Values: If your
@EventMappingmethod returns a message type (likeTextMessage), the SDK will automatically handle the reply logic for you.
Example: Handling Text Messages with Destination and Content
@LineMessageHandler
public class EchoApplication {
@EventMapping
public TextMessage handleTextMessageEvent(@LineBotDestination String destination,
MessageEvent<TextMessageContent> event) {
System.out.println("destination: " + destination);
return new TextMessage(event.getMessage().getText());
}
}