[Java] LINE integration with Spring Boot

Introduction

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.

About LINE cooperation (LINE BOT)

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

line_message_api.png

Development environment

・ STS (Spring Tool Suite) * Single package version ・ JDK1.8 ・ Heroku

LINE BOT operation check (preparation)

1. Registration to "LINE Developers"

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.

1. Click the "Login" button in the red frame. ![line_develop_1.PNG](https://qiita-image-store.s3.amazonaws.com/0/215568/a47c9be7-12ce-abb3-0c71-07ab0f5edf55.png)
  1. Log in with your LINE account. line_develop_2.PNG

  2. Enter the developer information (developer name / email address). line_develop_3.png

  3. If there is no problem with the input contents, create an account. line_develop_4.png

2. Create a new provider

The provider is the name of the service provider (company / individual).

1. Click the "Create new provider" button in the red frame. ![line_develop_5.png](https://qiita-image-store.s3.amazonaws.com/0/215568/877eade7-73f4-e57f-186f-b57427fc2041.png)
  1. Enter the provider name. line_develop_6.png

  2. If you are satisfied with the input contents, create a provider. line_develop_7.png

3. Create a new channel (BOT)

There are three channels, "LINE Login", "Message API", and "Clova Skill". This time, we will create a channel for "Message API".

1. Click the "Create Channel" button in the red frame. ![line_develop_8.png](https://qiita-image-store.s3.amazonaws.com/0/215568/491809c6-e32d-98e6-4d22-7bc230471ea8.png)
  1. 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) line_develop_9.png

  2. If there is no problem with the input contents, create a channel. line_develop_10.png

\def\textlarge#1{%
   {\rm\Large #1}
}
\def\textsmall#1{%
  {\rm\scriptsize #1}
}

4. Operation check

This time, check the response of the following message.

message Overview
\textsmall{Keyword response message} \textsmall{A message that is automatically sent when a specific keyword is entered}
\textsmall{Autoresponder message} \textsmall{A message that is automatically sent when a message other than a keyword response message is entered}
1. Press the channel created by the red frame. ![line_develop_11.png](https://qiita-image-store.s3.amazonaws.com/0/215568/754b127c-9019-703b-5e81-f39ca2c7a692.png)
  1. Transition to the "LINE management screen" from the link in the red frame. line_develop_12.png

  2. Create a keyword response message from the message menu on the LINE management screen. This time, I created it with "test" as the keyword. line_develop_13.png

  3. Create an automatic response message from the message menu on the LINE management screen. line_develop_14.png

  4. Actually check the message response.

  • To add a BOT friend, read the "QR code to the LINE app" and add it. line_develop_15.png

Spring Boot LINE cooperation

The preparation has become long, but the main subject is from here. Implement the LINE linkage part of Spring Boot.

1. Add dependency

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>

2. Add property

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

3. Modify application class

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);
    }
    
}

4. Deploy your application to Heroku

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) line_develop_16.png

[Extra edition] I made it

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;
		}
	}
	
}

line_develop_17.png

Summary

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.

Referenced site

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