This blog is about to integration of Line Messaging API with spring boot. I will tell you how Line Messaging API will communicate to spring boot application.
What is Line Messaging API?
The Line Messaging API is to establish communication between Line ChatBot and your server. If you want to create a chatbot where line user can send message’s and your server will reply for those messages then Line Messaging API is the right choice for you.
How Line Works
This is very important to understand how the Line Messaging API work. When a user sends any message, that message will reach to line server and line server will create a request JSON and redirect that json to webhook URL which is your server’ url.when line request reach to your server, your server will immediately give success response code and then process the line request. When you want to reply from your server you need to hit a posting API to this URL(https://api.line.me/v2/bot/message/reply) and after that line, the server will send your message to line app.
If you send any message to line bot then line also have Webhook URL which is your server URL. Means to say both line and your server have target URL of each other, now you can imagine how communication work.
If you want to send any response message then Line Messaging API provides the URL.
https://api.line.me/v2/bot/message/reply.
Line Platform can communicate in the following type of messages:
Text
Image
Video
Audio
File
Location
Sticker
In the following blog, we will discuss only Text Message’s.
Prerequisites :
1-You should have a line account and in the line, account creates a provider and in the provider create a channel. For detail to how to create account provider and channel, you can refer the line messaging API documentation.
2-You need an SSL certificate.
Change these setting in Line Channel:
1-Enable the Use webhooks in a channel setting.
2-Fill the URL of your Rest API in webhook URL.
3-Note down the channel access token.
In your spring boot project Add the below dependency in the pom.xml:
<dependency> <groupId>com.linecorp.bot</groupId> <artifactId>line-bot-api-client</artifactId> <version>2.4.0</version> </dependency>
In the application.properties create a constant named with authorization, set channel_access_token(Key value to access your line channel) value to this which you note down above. Create a controller in the project and remember one thing that line Messaging API is asynchronous, which means when the line passes the request to your server your server will return response code 200 Success immediately and then process line request in a new thread. Once your task is done you will hit a line API to give the response so please do not confuse in the asynchronous concept.
@RestController @RequestMapping(AppConstant.RequestApi.API_PREFIX) public class TestLineMessagingController { private static final Logger LOGGER = Logger.getLogger(GetLoyaltyPointController.class); /*This annotatino is for make this method call Async*/ @Async("asyncExecutor") @CrossOrigin @RequestMapping(value = AppConstant.RequestApi.TEST_LINE_API) public void replyToLineMessaginAPI(@RequestBody LineRequest lineRequest) throws AppException, InterruptedException { /*Get the channel access token*/ String authorization = environment.getRequiredProperty(AppConstant.Config.CHANNEL_ACCESS_TOKEN); String replyMessage = "Welcome to Line Messaging API"; TextMessage textMessage = null; final LineMessagingClient client = LineMessagingClient.builder(authorization).build(); textMessage = new TextMessage(replyMessage); final ReplyMessage replyMessage = new ReplyMessage(lineServerToAsyncApiRequest.getReplyToken(), textMessage); final BotApiResponse botApiResponse; try { botApiResponse = client.replyMessage(replyMessage).get(); } catch (InterruptedException | ExecutionException exception) { LOGGER.error("Unable to hit reply API for Line server : " + exception); exception.printStackTrace(); return; } } }
When you type anything form the line chat bot your server will reply “Welcome to Line Messaging API”. In the above example, I create only controller layer you can create service and DAO layer also according to your requirement.
Refrences::https://developers.line.biz/en/docs/messaging-api/overview/