[JAVA] Nginx + Spring Boot parrot return LineBot made with VPS

I created a LineBot that returns a parrot of Nginx + SpringBoot in a VPS environment.

Environmental composition

・ VPS: Conoha (https://www.conoha.jp/) ・ OS: CentOS7 ・ SSL certificate: Let ’s Encrypt ・ Nginx 1.12.2 ・ SpringBoot 2.1.6

https conversion

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/

Setting and checking Line Developers

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. LineDev1.png

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. LineDev2.png

Select "Do not use" for the automatic response message. Select "Do not use" as the greeting when adding a friend. LineDev3.png

Create Spring Boot project

  1. 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 SpringInitializr.png

  2. 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);
    }
}
  1. Edit build.gradle. I changed it from the initial state to the following and built it in Jar.

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'
    }
}
  1. Refresh Gradle Project In the previous step, we added "line-bot-spring-boot: 2.7.0" to Gradle, so refresh the Gradle Project and import the Jar. (This eliminates Java compilation errors.)

Right click on the project-> Gradle-> Refresh Gradle Project GradleUPdate.png

  1. Add the following 3 lines to application.properties of Spring Boot

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.

Cooperation between Nginx and Spring Boot

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

  1. The ssl.conf in the article has been changed as follows.

/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;
    }
}
  1. The contents of application.properties in the above article are added. Finally application.properties is as follows.

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

Build Spring Boot in Jar with Gradle

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 ... GradleBuild01.png

  1. Double-click Gradle Project-> Select New_configration, enter "build" in Gradle Tasks :, click Workspalce ... GradleBuild02.png

  2. Select the project to be built with Select Project GradleBuild03.png

  3. Click Run when the path is set in the Working Directory GradleBuild04.png

  4. If the build is successful, a jar will be generated in build / libs. GradleBuild05.png

Start Spring Boot on VPS

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. Spring起動.png

Connection confirmation

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. LineBot接続確認.png

Check with the Line app

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.

Lineメッセージ確認.png

Recommended Posts

Nginx + Spring Boot parrot return LineBot made with VPS
Launch Nginx + Spring Boot application with docker-compose
Download with Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Use Basic Authentication with Spring Boot
Introduction to Spring Boot x OpenAPI ~ OpenAPI made with Generation gap pattern ~
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
I made a simple search form with Spring Boot + GitHub Search API.
Create an app with Spring Boot
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
Processing at application startup with Spring Boot
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Start web application development with Spring Boot
I tried Lazy Initialization with Spring Boot 2.2.0
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Implement paging function with Spring Boot + Thymeleaf
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Form class validation test with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security
Create a website with Spring Boot + Gradle (jdk1.8.x)
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Until data acquisition with Spring Boot + MyBatis + PostgreSQL
Create a simple search app with Spring Boot
Hash passwords with Spring Boot + Spring Security (with salt, with stretching)
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
Run Scala applications with Spring Boot through Gradle