[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)

Create a reply function by connecting Heroku and LINE API!

I want to make a ramen bot using the LINE API! so First of all, use Java LINE Bot SDK to create an "echo" bot that returns parrots. ▼ Implemented with reference to the following URL "Create a sample bot on Heroku" https://developers.line.me/ja/docs/messaging-api/building-sample-bot-with-heroku/

Tools used

LINE Messaging API -Java LINE Bot SDK ・ Heroku ・ Java

procedure

  1. Set up the LINE Messaging API
  2. Set up Heroku
  3. Generate Maven Project
  4. Deploy to Heroku

Create Maven project

1.pom.xml

pom.xml


.
.
.

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
    
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>
		<dependency>
			<groupId>com.linecorp.bot</groupId>
			<artifactId>line-bot-api-client</artifactId>
			<version>1.12.0</version>
			<scope>compile</scope>
			<exclusions>
				<exclusion>
					<artifactId>commons-logging</artifactId>
					<groupId>commons-logging</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.linecorp.bot</groupId>
			<artifactId>line-bot-model</artifactId>
			<version>1.12.0</version>
			<scope>compile</scope>
			<exclusions>
				<exclusion>
					<artifactId>commons-logging</artifactId>
					<groupId>commons-logging</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.linecorp.bot</groupId>
			<artifactId>line-bot-spring-boot</artifactId>
			<version>1.11.0</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

2.application.yml Acccess Token and Channel Secret are generated when you create a new channel with the LINE Messaging API

application.yml


line.bot:
  channel-token: 'AcccessToken'
  channel-secret: 'Channel Secret'
  handler.path: /callback

3.Procfile ★ ☆ Point ☆ ★

  1. Generate a file under the root directory
  2. The file name is "Procfile" * The first "P" is capitalized! https://devcenter.heroku.com/articles/procfile

Procfile


web: java $JAVA_OPTS -jar target/{APPNAME}-0.0.1-SNAPSHOT.jar --server.port=$PORT

4.App.java I copied the code in the Java LINE Bot SDK.

App.java


@SpringBootApplication
@LineMessageHandler
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
		System.out.println("hello");
	}

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

Deploy to Heroku!

//Local repository generation, commit
$ git init
$ git add -A  
$ git commit

//Login to heroku from CLI tool
$ heroku login

//Web application generation in Heroku (remote repository is also generated at this time)
$ heroku create APPNAME 

//Link local repository and remote repository
$ heroku git:remote --app APPNAME 

git push heroku master

Webhook settings

・ Send Webhook ⇒ Use ・ Webhook URL ⇒ https: // {APPNAME} .herokuapp.com / callback {APPNAME} is the name of the application you created

無題.png

** OK if the connection is confirmed successfully! !! ** **

You have now implemented the reply function! !!

I thought it was easy, but I struggled because the LINE Message API and heroku didn't connect. Please refer to it ☆ Next time, we will implement Ramen BOT.

Recommended Posts

[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
[Java] LINE integration with Spring Boot
I made a simple search form with Spring Boot + GitHub Search API.
LINE Bot x Java (Spring Boot) construction procedure
I wrote a test with Spring Boot + JUnit 5 now
I made a simple MVC sample system using Spring Boot
I made a shopify app @java
I tried GraphQL with Spring Boot
I made a command line interface with WinMerge Plugin using JD-Core
I implemented an OAuth client with Spring Boot / Security (LINE login)
Hello World with Eclipse + Spring Boot + Maven
I made a risky die with Ruby
I tried Lazy Initialization with Spring Boot 2.2.0
I made a rock-paper-scissors app with kotlin
I made a new Java deployment tool
I made a rock-paper-scissors app with android
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
I made a Diff tool for Java files
I made a primality test program in Java
Create a website with Spring Boot + Gradle (jdk1.8.x)
Configure Spring Boot application with maven multi module
Create a simple search app with Spring Boot
04. I made a front end with SpringBoot + Thymeleaf
I made a mosaic art with Pokemon images
I made a gender selection column with enum
I made a rock-paper-scissors game in Java (CLI)
I made a Docker container to run Maven
Java tips-Create a Spring Boot project in Gradle
Learn Java with Progate → I will explain because I made a basic game myself
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
I tried to clone a web application full of bugs with Spring Boot
Easy to make LINE BOT with Java Servlet
Create a web api server with spring boot
I tried to break a block with java (1)
Deploy Line bot with rails5 + Docker + Heroku Note
Create a Spring Boot development environment with docker
I made a portfolio with Ruby On Rails
I tried to create a shopping site administrator function / screen with Java and Spring
What I was addicted to when developing a Spring Boot application with VS Code
I made a function to register images with API in Spring Framework. Part 1 (API edition)
Create a portfolio app using Java and Spring Boot
Include external jar in package with Spring boot2 + Maven3
I made a simple calculation problem game in Java
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Let's make a LINE Bot with Ruby + Sinatra --Part 2
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[Ruby] I made a crawler with anemone and nokogiri.
Implement a simple Rest API with Spring Security with Spring Boot 2.0
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
I made a Restful server and client in Spring.
I tried OCR processing a PDF file with Java
A memorandum when creating a REST service with Spring Boot
Create a simple demo site with Spring Security with Spring Boot 2.1
I made a Wrapper that calls KNP from Java
Nginx + Spring Boot parrot return LineBot made with VPS
How to boot by environment with Spring Boot of Maven
Let's make a LINE Bot with Ruby + Sinatra --Part 1
[Java] How to start a new line with StringBuilder
Spring Boot + Heroku Postgres