We are currently doing LINE cooperation (LINE BOT) on Spring Boot for the assigned projects. I found the content interesting, so I investigated and implemented it separately this time.
LINE BOT is a popular name for "LINE Messaging API", which is an API that can automatically exchange messages with users. The main functions are as follows.
function | Overview |
---|---|
Push API | Ability to send messages starting from BOT |
Reply API | Ability to send a message starting from a message from the user |
・ STS (Spring Tool Suite) * Single package version ・ JDK1.8 ・ Heroku
To use LINE BOT, you need to register with "[LINE Developers](https://developers.line.biz/ja/" LINE Developers ")". All you need is a LINE account.
Log in with your LINE account.
Enter the developer information (developer name / email address).
If there is no problem with the input contents, create an account.
The provider is the name of the service provider (company / individual).
Enter the provider name.
If you are satisfied with the input contents, create a provider.
There are three channels, "LINE Login", "Message API", and "Clova Skill". This time, we will create a channel for "Message API".
Enter the following application information. ・ App icon image ·app name ・ App description ・ Plan (use "Developer Trial" if you just want to test) ・ Large industry ・ Small industry ・ Notification e-mail address (Unless otherwise specified, specify the e-mail address when registering an account)
If there is no problem with the input contents, create a channel.
\def\textlarge#1{%
{\rm\Large #1}
}
\def\textsmall#1{%
{\rm\scriptsize #1}
}
This time, check the response of the following message.
message | Overview |
---|---|
Transition to the "LINE management screen" from the link in the red frame.
Create a keyword response message from the message menu on the LINE management screen. This time, I created it with "test" as the keyword.
Create an automatic response message from the message menu on the LINE management screen.
Actually check the message response.
The preparation has become long, but the main subject is from here. Implement the LINE linkage part of Spring Boot.
Since we are using Maven this time, add the following dependency to pom.xml.
pom.xml
<dependency>
<groupId>com.linecorp.bot</groupId>
<artifactId>line-bot-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
Add the following properties to the properties file. The "channel-token" and "channel-secret" are listed in the channel preferences for the BOT you created. As for "channel-token", it has not been created for the first time, so it will need to be reissued.
application.yml
line:
bot:
channel-token:Character string in the "Access token (long term)" column
channel-secret:Character string in the "Channel Secret" column
handler.path: /callback
Add processing when receiving a LINE message to the application class. Sample "[line-bot-sdk](https://github.com/line/line-bot-sdk-java/tree/master/line-bot-spring-boot" line-bot-sdk ")" Create based on. It is a specification that returns the LINE sent message as it is.
SpringBootResearchApplication.java
package spring.research;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.linecorp.bot.model.event.Event;
import com.linecorp.bot.model.event.MessageEvent;
import com.linecorp.bot.model.event.message.TextMessageContent;
import com.linecorp.bot.model.message.TextMessage;
import com.linecorp.bot.spring.boot.annotation.EventMapping;
import com.linecorp.bot.spring.boot.annotation.LineMessageHandler;
/**
*Application class
* @author s-tsuchida
*/
@SpringBootApplication
@LineMessageHandler
public class SpringBootResearchApplication {
public static void main(String[] args) {
final SpringApplication springApplication = new SpringApplication(SpringBootResearchApplication.class);
springApplication.run(args);
}
@EventMapping
public TextMessage handleTextMessageEvent(MessageEvent<TextMessageContent> event) {
System.out.println("event: " + event);
return new TextMessage(event.getMessage().getText());
}
@EventMapping
public void handleDefaultMessageEvent(Event event) {
System.out.println("event: " + event);
}
}
Now, deploy the created application to Heroku and check the operation. The registration method of Heroku was explained in an easy-to-understand manner at the following URL, so please refer to this. ・ Details of making LINE bot ③ ~ LINE bot self-made (2/2)
Based on the above application, I edited it so that it would return the management philosophy and core values of the company I work for. If you would like to know the details of our management philosophy, please refer to the following URL. ・ Ienter Co., Ltd.
SpringBootResearchApplication.java
package spring.research;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.linecorp.bot.model.event.Event;
import com.linecorp.bot.model.event.MessageEvent;
import com.linecorp.bot.model.event.message.TextMessageContent;
import com.linecorp.bot.model.message.TextMessage;
import com.linecorp.bot.spring.boot.annotation.EventMapping;
import com.linecorp.bot.spring.boot.annotation.LineMessageHandler;
import spring.research.domain.service.line.LineService;
/**
*Main program
* @author s-tsuchida
*/
@SpringBootApplication
@LineMessageHandler
public class SpringBootResearchApplication {
//LINE service
@Autowired
private LineService lineService;
public static void main(String[] args) {
final SpringApplication springApplication = new SpringApplication(SpringBootResearchApplication.class);
springApplication.run(args);
}
@EventMapping
public TextMessage handleTextMessageEvent(MessageEvent<TextMessageContent> event) {
System.out.println("event: " + event);
return new TextMessage(lineService.createResponseMessage(event.getMessage().getText()));
}
@EventMapping
public void handleDefaultMessageEvent(Event event) {
System.out.println("event: " + event);
}
}
LineService.java
package spring.research.domain.service.line;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
/**
*LINE service
* @author s-tsuchida
*/
@Component
public class LineService {
//Reply message
private static final String MISSION_MESSAGE = "Enjoy to the world";
private static final String PHILOSOPHY_MESSAGE = "Aiming to be a company that is trusted and loved\r\n We will continue to make unremitting efforts.\r\n"
+ "one.Enjoy work\r\n"
+ "one.Enjoy growth\r\n"
+ "one.Enjoy the challenge\r\n"
+ "one.Enjoy the service\r\n"
+ "one.Enjoy gratitude\r\n";
private static final String CORE_VALUE_01_MESSAGE = "1.Always act positively";
private static final String CORE_VALUE_02_MESSAGE = "2.Let's do everything hard";
private static final String CORE_VALUE_03_MESSAGE = "3.Let's practice the best manners in Japan";
private static final String CORE_VALUE_04_MESSAGE = "4.NO.Let's make 1";
private static final String CORE_VALUE_05_MESSAGE = "5.Pursue speed and give joy";
private static final String CORE_VALUE_06_MESSAGE = "6.Let's set up excitement for everyone";
private static final String CORE_VALUE_07_MESSAGE = "7.Let's hone ourselves";
private static final String CORE_VALUE_08_MESSAGE = "8.Be honest and humble";
private static final String CORE_VALUE_09_MESSAGE = "9.Create a family-like team";
private static final String CORE_VALUE_10_MESSAGE = "10.Keep thinking strongly about your dreams and hopes and make them a reality";
private static final String CORE_VALUE_ALL_MESSAGE = CORE_VALUE_01_MESSAGE + "\r\n"
+ CORE_VALUE_02_MESSAGE + "\r\n"
+ CORE_VALUE_03_MESSAGE + "\r\n"
+ CORE_VALUE_04_MESSAGE + "\r\n"
+ CORE_VALUE_05_MESSAGE + "\r\n"
+ CORE_VALUE_06_MESSAGE + "\r\n"
+ CORE_VALUE_07_MESSAGE + "\r\n"
+ CORE_VALUE_08_MESSAGE + "\r\n"
+ CORE_VALUE_09_MESSAGE + "\r\n"
+ CORE_VALUE_10_MESSAGE + "\r\n";
//Message Not applicable Message
private static final String OTHER_MESSAGE = "Welcome!\r\to nSpringLineBot!";
//Message MAP
private static final Map<String, String> MESSAGE_MAP = Collections.unmodifiableMap(new HashMap<String, String>(){
private static final long serialVersionUID = 1L;
{
//Mission
put("misson", MISSION_MESSAGE);
put("MISSION", MISSION_MESSAGE);
put("Mission", MISSION_MESSAGE);
put("Mission", MISSION_MESSAGE);
//Management Philosophy
put("philosophy", PHILOSOPHY_MESSAGE);
put("PHILOSOPHY", PHILOSOPHY_MESSAGE);
put("Keieirinen", PHILOSOPHY_MESSAGE);
put("Management Philosophy", PHILOSOPHY_MESSAGE);
//Core value 1
put("corevalue1", CORE_VALUE_01_MESSAGE);
put("COREVALUE1", CORE_VALUE_01_MESSAGE);
put("Koabaryu 1", CORE_VALUE_01_MESSAGE);
put("Core value 1", CORE_VALUE_01_MESSAGE);
//Core value 2
put("corevalue2", CORE_VALUE_02_MESSAGE);
put("COREVALUE2", CORE_VALUE_02_MESSAGE);
put("Koabaryu 2", CORE_VALUE_02_MESSAGE);
put("Core value 2", CORE_VALUE_02_MESSAGE);
//Core value 3
put("corevalue3", CORE_VALUE_03_MESSAGE);
put("COREVALUE3", CORE_VALUE_03_MESSAGE);
put("Koabaryu 3", CORE_VALUE_03_MESSAGE);
put("Core value 3", CORE_VALUE_03_MESSAGE);
//Core value 4
put("corevalue4", CORE_VALUE_04_MESSAGE);
put("COREVALUE4", CORE_VALUE_04_MESSAGE);
put("Koabaryu 4", CORE_VALUE_04_MESSAGE);
put("Core value 4", CORE_VALUE_04_MESSAGE);
//Core value 5
put("corevalue5", CORE_VALUE_05_MESSAGE);
put("COREVALUE5", CORE_VALUE_05_MESSAGE);
put("Koabaryu 5", CORE_VALUE_05_MESSAGE);
put("Core value 5", CORE_VALUE_05_MESSAGE);
//Core value 6
put("corevalue6", CORE_VALUE_06_MESSAGE);
put("COREVALUE6", CORE_VALUE_06_MESSAGE);
put("Koabaryu 6", CORE_VALUE_06_MESSAGE);
put("Core value 6", CORE_VALUE_06_MESSAGE);
//Core value 7
put("corevalue7", CORE_VALUE_07_MESSAGE);
put("COREVALUE7", CORE_VALUE_07_MESSAGE);
put("Koabaryu 7", CORE_VALUE_07_MESSAGE);
put("Core value 7", CORE_VALUE_07_MESSAGE);
//Core value 8
put("corevalue8", CORE_VALUE_08_MESSAGE);
put("COREVALUE8", CORE_VALUE_08_MESSAGE);
put("Koabaryu 8", CORE_VALUE_08_MESSAGE);
put("Core value 8", CORE_VALUE_08_MESSAGE);
//Core value 9
put("corevalue9", CORE_VALUE_09_MESSAGE);
put("COREVALUE9", CORE_VALUE_09_MESSAGE);
put("Koabaryu 9", CORE_VALUE_09_MESSAGE);
put("Core value 9", CORE_VALUE_09_MESSAGE);
//Core value 10
put("corevalue10", CORE_VALUE_10_MESSAGE);
put("COREVALUE10", CORE_VALUE_10_MESSAGE);
put("Koabaryu 10", CORE_VALUE_10_MESSAGE);
put("Core value 10", CORE_VALUE_10_MESSAGE);
//All core values
put("corevalue", CORE_VALUE_ALL_MESSAGE);
put("COREVALUE", CORE_VALUE_ALL_MESSAGE);
put("Core value", CORE_VALUE_ALL_MESSAGE);
put("Core value", CORE_VALUE_ALL_MESSAGE);
}
});
/**
*Returns a reply message based on the sent LINE message
* @param sendMessage The LINE message sent
* @return reply message
*/
public String createResponseMessage(String sendMessage) {
if(MESSAGE_MAP.containsKey(sendMessage)) {
//When the sent LINE message exists in the MAP key
return MESSAGE_MAP.get(sendMessage);
} else {
//If the sent LINE message does not exist in the MAP key
return OTHER_MESSAGE;
}
}
}
I didn't have much experience in linking with existing services, so I found it fresh and interesting. In the future, I would like to try sending LINE messages regularly using cron.
・ Playing program diary -[Run LINE BOT made with Spring Boot and LINE Messaging API on Heroku](http://kikutaro777.hatenablog.com/entry/2017/01/16/230122 "LINE BOT made with Spring Boot and LINE Messaging API" On Heroku ") ・ LINE bot that issues an alert before the garbage collection date
Recommended Posts