Hey there, fellow developer! Ready to dive into the world of Line API integration? You're in for a treat. Line's messaging platform is a powerhouse, and with the line-bot-spring-boot-handler
package, we'll be up and running in no time. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get our project off the ground:
pom.xml
:<dependency> <groupId>com.linecorp.bot</groupId> <artifactId>line-bot-spring-boot</artifactId> <version>4.3.0</version> </dependency>
Time to get your secret sauce:
application.properties
:line.bot.channel-token=YOUR_CHANNEL_TOKEN line.bot.channel-secret=YOUR_CHANNEL_SECRET line.bot.handler.path=/callback
Let's get our controller up and running:
@RestController @LineMessageHandler public class LineBotController { @EventMapping public TextMessage handleTextMessageEvent(MessageEvent<TextMessageContent> event) { return new TextMessage("You said: " + event.getMessage().getText()); } }
Boom! You've got a basic echo bot. How cool is that?
Now, let's flex those messaging muscles:
@EventMapping public TextMessage handleTextMessage(MessageEvent<TextMessageContent> event) { return new TextMessage("Hello, World!"); }
@EventMapping public ImageMessage handleImageMessage(MessageEvent<ImageMessageContent> event) { return new ImageMessage("https://example.com/image.jpg", "https://example.com/preview.jpg"); }
@EventMapping public LocationMessage handleLocationMessage(MessageEvent<LocationMessageContent> event) { return new LocationMessage("My Location", "1-chome, Shibuya, Tokyo", 35.65910807942215, 139.70372892916203); }
@EventMapping public TemplateMessage handleTemplateMessage(MessageEvent<TextMessageContent> event) { ButtonsTemplate buttonsTemplate = new ButtonsTemplate( "https://example.com/image.jpg", "My Button Template", "Please select", Arrays.asList( new MessageAction("Label 1", "Text 1"), new MessageAction("Label 2", "Text 2") ) ); return new TemplateMessage("Button Template", buttonsTemplate); }
Let's catch those user interactions:
@EventMapping public void handleFollowEvent(FollowEvent event) { // Handle new follower } @EventMapping public void handleUnfollowEvent(UnfollowEvent event) { // Handle unfollower } @EventMapping public void handleJoinEvent(JoinEvent event) { // Handle bot joining a group or room } @EventMapping public void handleLeaveEvent(LeaveEvent event) { // Handle bot leaving a group or room } @EventMapping public void handlePostbackEvent(PostbackEvent event) { // Handle postback actions }
Want to take it up a notch? Check these out:
RichMenu richMenu = RichMenu.builder(...) .build(); lineMessagingClient.createRichMenu(richMenu).get();
QuickReply quickReply = QuickReply.items( QuickReplyItem.builder() .action(new MessageAction("Label", "Text")) .build() ); TextMessage textMessage = TextMessage .builder() .text("Select an option") .quickReply(quickReply) .build();
FlexMessage flexMessage = FlexMessage .builder() .altText("Flex Message") .contents( Bubble.builder() .body(Box.builder() .layout(FlexLayout.VERTICAL) .contents(Text.builder().text("Hello, Flex!").build()) .build()) .build()) .build();
Ready to see your bot in action?
ngrok http 8080
Once you're happy, deploy to your favorite server and watch your bot come to life!
A few pro tips to keep in mind:
And there you have it! You've just built a Line bot integration that would make any developer proud. Remember, this is just the beginning – there's so much more you can do with the Line API. Keep exploring, keep coding, and most importantly, have fun with it!
Need more inspiration? Check out the Line Developers documentation for even more cool features to play with.
Now go forth and build some awesome bots! 🚀