I tried to create a Clova skill in Java

Introduction

[9/3 @ Sapporo] LINE BOOT AWARDS --Clova Skill Hands-on co-sponsored by Java user group Hokkaido (Java Do) is also attended by slapstick participants. It was a great success. Thank you to everyone who cooperated in the event.

I used node.js for hands-on, but since JUG co-sponsored it, I listened to hands-on and did almost the same content Clova CEK SDK Java. It was implemented with -sdk-java).

I'll leave this implementation note for this article.

procedure

Here, the following configuration is realized.

[You] ----- [Clova] ----- [EchoHandler class on Spring Boot]

Preparation

Mr. Nobisuke's How to start skill development with Clova CEK ~ Start development with Node.js ~ used as a hands-on text / #% E4% BD% BF% E3% 81% 84% E5% A7% 8B% E3% 82% 81% E3% 81% AE% E7% 94% B3% E8% AB% 8B)

3.Application to start using
4.Creating an interaction model
Intents and slots
slot
Intent
5.Build interaction model

Proceed in the same way.

Make a note of the intent name CurreySearchIntent created here, as it is important.

Clova skill implementation in Java (Spring Boot)

In Nobisuke's document, the Clova skill is implemented in node.js, but here it is implemented using Java (Spring Boot).

Creating a Spring Boot project

Access Spring Initializr and set the following items.

--Spring Boot version: 2.0.4

Download clova.zip with the Generate Project button.

Extract clova.zip and open it as a Maven project in an IDE (IntelliJ, Eclipse, Netbeans, etc.).

pom.xml settings

Add the Clova CEK SDK Java dependency between <dependencies> ~ </ dependencies> in pom.xml,

pom.xml


<dependencies>

(Omitted)

  <dependency>
    <groupId>com.linecorp.clova</groupId>
    <artifactId>clova-extension-boot-web</artifactId>
    <version>1.0.0</version>
  </dependency>

</dependencies>

application.properties

Set the file path of the server that accepts requests from Clova.

application.properties


cek.api-path=/clova

This will configure https: // xxxx ... / clova to accept POST requests from Clova.

(You can also set cek.client.default-locale etc. if needed)

Create request handler class

Create a request handler class to handle POST requests from Clova.

This class is based on [Clova CEK SDK Java Implementation Example](https://github.com/line/clova-cek-sdk-java/blob/master/samples/echo/src/main/java/com/linecorp/ It is made by referring to clova / extension / sample / hello / EchoHandler.java).

EchoHandler.java


package com.example.clova;

import com.linecorp.clova.extension.boot.handler.annotation.*;
import com.linecorp.clova.extension.boot.message.response.CEKResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

import static com.linecorp.clova.extension.boot.message.speech.OutputSpeech.text;

@CEKRequestHandler
public class EchoHandler {

  private static final Logger log = LoggerFactory.getLogger(EchoHandler.class);

  @LaunchMapping
  CEKResponse handleLaunch() {
    return CEKResponse.builder()
      .outputSpeech(text("Find a curry shop."))
      .shouldEndSession(false)
      .build();
  }

  @IntentMapping("CurreySearchIntent")
  CEKResponse handleRepeatIntent(@SlotValue Optional<String> area) {
    String outputSpeechText = area
      .map(this::callbackShop)
      .orElse("I couldn't hear it.");
    return CEKResponse.builder()
      .outputSpeech(text(outputSpeechText))
      .shouldEndSession(false)
      .build();
  }

  private String callbackShop(String inArea) {
    switch (inArea) {
      case "Akihabara":
        return inArea + "The recommended curry shop is Fujiyama Dragon Curry.";
      case "Kanda":
        return inArea + "The recommended curry shop is Kyoeidou.";
      default:
        return "I'm sorry, I didn't understand.";
    }
  }

  @IntentMapping("Clova.CancelIntent")
  CEKResponse handleCancelIntent() {
    return CEKResponse.builder()
      .outputSpeech(text("The search for curry shop will end."))
      .shouldEndSession(true)
      .build();
  }

  @SessionEndedMapping
  CEKResponse handleSessionEnded() {
    log.info("The curry shop search skill has ended.");
    return CEKResponse.empty();
  }

}

I will explain some important points.

IntentMapping, SlotValue annotation

@IntentMapping is the process (method) corresponding to the custom intent name / built-in intent name. ) Is an annotation.

In this example, the method handleCancelIntent () with @IntentMapping (Clova.CancelIntent) corresponds to the built-in intent Clova.CancelIntent when the skill is canceled (finished).

In addition, the method handleRepeatIntent (@SlotValue ...) with@IntentMapping (CurreySearchIntent)corresponds to the question" Tell me the curry shop in Akihabara (area) "created in the preparation. To do.

The argument @SlotValue indicates that the value of the slot is the argument passed. In this example, the value of the ʻarea` slot of "Tell me the curry shop in Akihabara (area)" is passed.

CEKResponse class

The CEKResponse class set in the return value represents Clova's response. Instantiate with builder.

ʻOutputSpeechis the string to reply to.shouldEndSession` indicates whether the skill should be terminated with true / false.

others

@LaunchMapping is an annotation given to the corresponding method when the skill is started.

@SessionEndedMapping is an annotation given to the corresponding method at the end of the skill.

Operation check

Run the Spring Boot ClovaApplication class.

Nobisuke's [How to start skill development with Clova CEK ~ Start development with Node.js ~](https://dotstud.io/blog/clova-cek-nodejs-tutorial/#%E4%BD%BF % E3% 81% 84% E5% A7% 8B% E3% 82% 81% E3% 81% AE% E7% 94% B3% E8% AB% 8B)

 7.Confirm communication without hosting with ngrok
10.Actual machine test

Proceed in the same way (ngrok can be downloaded and executed from the home site without using npm, or homebrew for macOS. Is also good).

--You: "Hey Clover" --Clova: "Pon (LED glows green)" --You: "Launch curry information" --Clova: "Find a curry shop." (← written in Java) --Clova: "Pon (LED glows green)" --You: "Tell me about the curry shop in Akihabara" --Clova: "The recommended curry shop in Akihabara is Fujiyama Dragon Curry." (← Described in Java)

If you can communicate like this, you are successful.

in conclusion

Just like line-bot-sdk-java, which creates a Bot (Messaging API), Clova also has Clova CEK SDK Java It seems that you can easily create Clova skills in Java.

The corresponding slot (Sapporo, Chitose) and the response message are different, but the equivalent source code is placed in gishi-yama / ClovaSample. There is.

I would like to continue to challenge how to cooperate with Bot.

** Promotion **

Hands-on material for making LINE Bot in Java is now available!

Recommended Posts

I tried to create a Clova skill in Java
I tried to create Alexa skill in Java
I tried to make a login function in Java
I tried to create a java8 development environment with Chocolatey
I tried to convert a string to a LocalDate type in Java
I tried to make a client of RESAS-API in Java
I tried to create a simple map app in Android Studio
java I tried to break a simple block
Try to create a bulletin board in Java
I tried to implement deep learning in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to create a LINE clone app
I tried to output multiplication table in Java
I tried to break a block with java (1)
I tried to make a talk application in Java using AI "A3RT"
I tried metaprogramming in Java
How to create a Java environment in just 3 seconds
I tried to implement Firebase push notification in Java
# 2 [Note] I tried to calculate multiplication tables in Java.
How to create a data URI (base64) in Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried to find out what changed in Java 9
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
I created a PDF in Java.
I tried to interact with Java
[Java] How to create a folder
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
Create a method to return the tax rate in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
I tried using Dapr in Java to facilitate microservice development
I tried to implement a buggy web application in Kotlin
I tried to create a padrino development environment with Docker
I tried to create a shopping site administrator function / screen with Java and Spring
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried using Elasticsearch API in Java
I tried a calendar problem in Ruby
I can't create a Java class with a specific name in IntelliJ
I tried to summarize Java lambda expressions
I tried to write code like a type declaration in Ruby
I tried the new era in Java
How to create a new Gradle + Java + Jar project in Intellij 2016.03
I tried to create a Spring MVC development environment on Mac
Create a TODO app in Java 7 Create Header
[Java] I tried to make a maze by the digging method ♪
I tried embedding a formula in Javadoc
I tried to create an Amazon echo skill that teaches scraped information in Java using the Alexa Skills Kit (ASK)
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
To create a Zip file while grouping database search results in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I tried to build a Firebase application development environment with Docker in 2020
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I thought about the best way to create a ValueObject in Ruby