Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack

Target

Even with zero knowledge, we will bring you to the point where you can create Slack Bot and Line Bot using Watson.

[Updated on June 6, 2019] This post was written during the Watson Conversation era, the predecessor of Watson Assistant, and although screen captures are still old, Watson Assitant's basic ideas and operations Does not change, so please read it as the latest environment. </ font>

■ I would like to touch on the following.

(1) Introduction to thinking, creating an account ② Design method of dialogue flow using "Hotel reservation" as an example ③ Utilization of context, practical usage of various functions not mentioned in Tutorial ④ How to link with Java logic ** ⑤ Create a chatbot with Watson + Java + Slack ** ← This article

This time, as the 5th time, I would like to make a chatbot ** with ** ④ Watson + Java + Slack.


This time, we will use ** Java ** to link ** Watson ** and ** Slack ** to create a chatbot.

It's not particularly difficult to work together, just pass the text that the user entered in ** Slack ** to ** Watson Conversation ** and return the result to the user again.

arch.png

Source code

Run the code below to launch the Slack Bot. This is the only description. Slack and Watson are now working together.

WcsSlackBotExample00.java



public class WcsSlackBotExample00 {

    //Edit here and enter Watson credentials
    private static final String WATSON_CONVERSATION_USERNAME = "EDIT_ME_USERNAME_HERE";
    private static final String WATSON_CONVERSATION_PASSWORD = "EDIT_ME_PASSWORD_HERE";
    private static final String WATSON_CONVERSATION_WORKSPACE_ID = "EDIT_ME_WORKSPACE_ID_HERE";

    //Edit here and enter the Slack bot API token
    private static final String SLACK_BOT_API_TOKEN = "EDIT_ME_SLACK_API_TOKEN";

    public static void main(String[] args) throws IOException {

        final WcsClient watson = new WcsClient(
                WATSON_CONVERSATION_USERNAME,
                WATSON_CONVERSATION_PASSWORD,
                WATSON_CONVERSATION_WORKSPACE_ID);

        SlackletService slackService = new SlackletService(SLACK_BOT_API_TOKEN);
        slackService.addSlacklet(new Slacklet() {
            @Override
            public void onDirectMessagePosted(SlackletRequest req, SlackletResponse resp) {
                //A direct message addressed to BOT was posted

                SlackUser slackUser = req.getSender();

                //Message content (text)
                String userInputText = req.getContent();

                //Get the id of the slack user, and let that slack user's id be Watson's unique user id
                String wcsClientId = slackUser.getId();

                //Send user-entered text to Watson and output Watson(outputText)To receive
                String botOutputText = watson.sendMessageForText(wcsClientId, userInputText);

                //Display the response from Watson in slack
                slackService.sendDirectMessageTo(slackUser, botOutputText);
            }
        });
        //Launched slacklet service(Connect to slack)
        slackService.start();
    }
}

Execution example

When executed, it is a one-to-one direct message exchange with Slackbot as shown below. We will run Watson's chatbot (dialogue flow).

By the way, it is okay for multiple users to connect to Slackbot at the same time.

Source code download

The complete source code can be found in the following repositories https://github.com/riversun/watson-java-slackbot-ja

The dialogue flow uses the ** hotel reservation (enhanced version) chatbot ** created previously as an example. You can download it from the following. https://riversun.github.io/wcs/org.riversun.WcsContextTestJa.zip

Description

private static final String WATSON_CONVERSATION_USERNAME = "EDIT_ME_USERNAME_HERE";
private static final String WATSON_CONVERSATION_PASSWORD = "EDIT_ME_PASSWORD_HERE";
private static final String WATSON_CONVERSATION_WORKSPACE_ID = "EDIT_ME_WORKSPACE_ID_HERE";

private static final String SLACK_BOT_API_TOKEN = "EDIT_ME_SLACK_API_TOKEN";

First, edit it here to enter your connection information to Watson and Slack.

--Watson Conversation workspace credentials (username, password, workspaceId) For more information here --For more information on Slack BOT's BOT API TOKEN here



final WcsClient watson = new WcsClient(
   WATSON_CONVERSATION_USERNAME,
   WATSON_CONVERSATION_PASSWORD,
   WATSON_CONVERSATION_WORKSPACE_ID);
        
SlackletService slackService = new SlackletService(SLACK_BOT_API_TOKEN);

The helper class ** WcsClient ** for using Watson Conversation from Java and the helper class ** SlackletService ** for creating Slack Bot in Java are ** new ** respectively.


 slackService.addSlacklet(new Slacklet() {
            @Override
            public void onDirectMessagePosted(SlackletRequest req, SlackletResponse resp) {
・ ・ ・ ・ ・ ・ ・
            }
         });

Create a callback class ** Slacklet ** that receives the ** direct message ** that the user sent to the BOT in Slack. (For ** Slacklet **, see this article "Easy to make Slack Bot with Java")


Let's take a look at the contents of ** public void onDirectMessagePosted (SlackletRequest req, SlackletResponse resp) **.

String userInputText = req.getContent();

Get the text entered by the user in Slack with ** # getContent **

String wcsClientId = slackUser.getId();

Get the slack user ID with ** slackUser # getId **. This user ID will be used as is by Watson as a unique ID to identify the user.


String botOutputText = watson.sendMessageForText(wcsClientId, userInputText);

** WcsClient # sendMessageForText ** sends the text entered by the user to Watson and receives the response (botOutputText) from Watson as a return value.

slackService.sendDirectMessageTo(slackUser, botOutputText);

Sends the response from Watson as a direct message to the user.

Summary

We've seen an example of how Watson and Slack work together in Java.

I was able to make a Slack Bot using Watson relatively easily, but To make it even easier to use as a Slack Bot Not only direct messages but also BOTs on channels I think it's a good idea to talk to the BOT at @mention and make various ingenuity. (The know-how about that is summarized in Easy to make Slack Bot with Java.)

Next time, as a sequel, we will link ** LINE ** (LINE BOT) and ** Watson **.

Recommended Posts

Let's go with Watson Assistant (formerly Conversation) ⑤ Create a chatbot with Watson + Java + Slack
Let's go with Watson Assistant (formerly Conversation) ② Make a hotel reservation chatbot
Let's go with Watson Assistant (formerly Conversation) ④ How to link with Java logic
Let's create a timed process with Java Timer! !!
Let's go with Watson Assistant (formerly Conversation) ① Introduction What is the idea and intention understanding of interactive apps?
Watson Assistant (formerly Conversation) on Android
Create a program to post to Slack with GO and make it a container
Let's make a calculator application with Java ~ Create a display area in the window
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
[Java] Create a collection with only one element
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Beginner] Create a competitive game with basic Java knowledge
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java basics] Let's make a triangle with a for statement
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
[Java] Let's create a mod for Minecraft 1.14.4 [2. Add block]
[Java] Let's create a mod for Minecraft 1.16.1 [Add block]
[Java] Create a filter
Let's scrape with Java! !!
I tried to create a java8 development environment with Chocolatey
[Java] Let's create a mod for Minecraft 1.14.4 [3. Add creative tab]
Create a SlackBot with AWS lambda & API Gateway in Java
[Java] Let's replace data objects with a mapper ~ BeanMapper Orika ~
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Let's experiment with Java inlining
Let's operate Excel with Java! !!
Create a java method [Memo] [java11]
[Java] Create a temporary file
Create a playground with Xcode 12
Create a simple web server with the Java standard library com.sun.net.httpserver
Let's create a TODO application in Java 4 Implementation of posting function
I can't create a Java class with a specific name in IntelliJ
Let's create a TODO application in Java 6 Implementation of search function
Create a high-performance enum with fields and methods like Java with JavaScript
Let's create a TODO application in Java 8 Implementation of editing function
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
Create a restaurant search app with IBM Watson + Gurunavi API (with source)
[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]
Let's create a parameter polymorphic mechanism with Generic Dao and Hibernate