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.
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();
}
}
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.
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
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.
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 **.