I created a LineBot that returns a parrot of Nginx + SpringBoot in a VPS environment.
・ VPS: Conoha (https://www.conoha.jp/) ・ OS: CentOS7 ・ SSL certificate: Let ’s Encrypt ・ Nginx 1.12.2 ・ SpringBoot 2.1.6
LineBot webhooks are only valid for https URLs. VPS is Conoha, and my own domain was acquired from another domain site. Here, I will omit the acquisition method and setting method of the original domain.
I got the SSL certificate by referring to the following. I also installed Nginx in the article below.
** Publish a secure website with Let ’s Encrypt SSL certificate ** https://knowledge.sakura.ad.jp/5573/
Open the basic channel settings in Line Developers and configure settings such as WebHook. Make a note of the Channnel Secret as you will use it later.
Reissue the access token, and make a note of the displayed access token for later use. Webhook transmission changed to "use" Please specify the acquired domain name in the grayed out part of the Webhook URL.
Select "Do not use" for the automatic response message. Select "Do not use" as the greeting when adding a friend.
Create a project with Spring Initializr https://start.spring.io/ ・ Project: Gradle Project ・ Language: Java ・ Spring Boot: 2.1.6 ・ Project Metadata ・ Group: com.example ・ Artifact: linebot
Import the created project with STS and [line-bot-java sample (echo)](https://github.com/line/line-bot-sdk-java/blob/master/sample- Copy spring-boot-echo / src / main / java / com / example / bot / spring / echo / EchoApplication.java) to LinebotApplication.java. At this point, the linebot library is not included, so a compile error will occur, which will be resolved in a later step.
com/example/linebot/LinebotApplication.java
@SpringBootApplication
@LineMessageHandler
public class LinebotApplication {
public static void main(String[] args) {
SpringApplication.run(LinebotApplication.class, args);
}
@EventMapping
public Message handleTextMessageEvent(MessageEvent<TextMessageContent> event) {
System.out.println("event: " + event);
final String originalMessageText = event.getMessage().getText();
return new TextMessage(originalMessageText);
}
@EventMapping
public void handleDefaultMessageEvent(Event event) {
System.out.println("event: " + event);
}
}
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile 'com.linecorp.bot:line-bot-spring-boot:2.7.0'
}
jar {
manifest {
attributes 'Main-Class': 'LinebotApplication'
}
}
Right click on the project-> Gradle-> Refresh Gradle Project
src/main/resources/application.properties
line.bot.channelSecret =XXX (Channnel Secret of Line Developers)
line.bot.channelToken =XXX (Line Developers access token)
line.bot.handler.path = /callback
The next step "Linking Nginx and Spring Boot" will also edit application.properties, so it will not be built yet.
Works with Nginx and Spring Boot. I referred to the following for the cooperation method.
** Use nginx to enable SSL (HTTPS) in Spring boot ** https://qiita.com/keigohtr/items/5b2b423fe0e9da027db6
/etc/nginx/conf.d/ssl.conf
server {
listen 80;
listen [::]:80;
return 301 https://$host$request_uri;
}
upstream spring-boot {
server 127.0.0.1:8443;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl on;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE+RSAGCM:ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!DSS;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
resolver 8.8.8.8;
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains;';
server_name example.com;
location / {
proxy_pass http://spring-boot;
}
}
src/main/resources/application.properties
line.bot.channelSecret = XXX
line.bot.channelToken = XXX
line.bot.handler.path = /callback
server.port=8443
#server.ssl.key-store=keystore.p12
#server.ssl.key-store-password=mypassword
#server.ssl.keyStoreType=PKCS12
#server.ssl.keyAlias=tomcat
endpoints.enabled=false
management.add-application-context-header=false
Right-click on the project-> Run As-> Gradle Build could not be done, so I built from STS to Jar by the following method
1 Right-click on the project-> Run As-> Run Configuration ...
Double-click Gradle Project-> Select New_configration, enter "build" in Gradle Tasks :, click Workspalce ...
Select the project to be built with Select Project
Click Run when the path is set in the Working Directory
If the build is successful, a jar will be generated in build / libs.
Upload the created jar to the user directory (/ home / [user name]) as the server location, execute the following command, and start Spring Boot.
sudo java -jar linebot-0.0.1-SNAPSHOT.jar
Spring Boot is in the booted state with the following message displayed.
Check the connection of the set Webhook URL in Line Developers. If the message "Successful" is displayed, the settings so far have been made correctly and the connection from the LineBot to the VPS has been established.
When you send a message to a channel created from the Line app, the same message you sent will be returned. Now you have a parrot return bot.
Recommended Posts