In this chapter, we will create a chatbot application, using Node-RED. For the chatbot application UI, we'll use Slack, and we'll use IBM Watson AI for skills. After completing the tutorials in this chapter, you will learn how to combine Node-RED with an external API to create an application. This will help you create extensible web applications with Node-RED in the future.
Let's get started with the following topics:
By the end of this chapter, you will have mastered how to make a Slack chatbot application with Node-RED.
To progress in this chapter, you will need the following:
This hands-on tutorial uses Slack as the UI for your chatbot application. Node-RED is responsible for controlling the exchange of messages in the background of the chatbot application.
The overall view of this chatbot application is as follows:
First of all, create a Slack workspace for use in this application with the following steps. If you already have a Slack workspace, you can use your existing one. In that case, skip the following steps and create a channel called learning-node-red in your workspace:
You have created the workspace for this tutorial:
Important note
The channel on which the chatbot resides should preferably be a channel that only you participate in unless you have a public purpose. This is because the chatbot's movement can be noisy for participants who do not like (or are not interested in) the chatbot.
At this point, you've got your workspace and channels ready to run your chatbot in Slack. Next, we will create a mechanism that will be the engine of the chatbot.
This hands-on tutorial uses IBM's Watson Assistant API as the engine for chatbots. Watson Assistant can use natural language analysis to interpret the intent and purpose of natural conversation and return an appropriate answer.
Details about Watson Assistant can be found at the following URL: https://www.ibm.com/cloud/watson-assistant-2/.
To use the Watson Assistant API, you need to create an instance of the Watson Assistant API on IBM Cloud. Follow these steps to create it:
You will be moved to the My first skill screen automatically when you open the Watson Assistant console for the first time.
Normally, you would create a Watson Assistant skill here, but this hands-on tutorial will focus on Node-RED rather than how to use Watson Assistant. Therefore, a skill in Watson Assistant is created by importing the definition file prepared in advance.
If you want to create your own skill, that's fine. In that case, the official Watson Assistant documentation will help: https://cloud.ibm.com/apidocs/assistant/assistant-v2.
This time, I prepared a skill that will randomly return a joke phrase when told tell me a joke.
The chat window will be opened when you click the Try it button. Try typing the conversation that follows in the chat window:
"Hello"; "Hi"; "Tell me jokes"; "Do you know any jokes?"; and so on…
If you don't get a good answer, try another phrase. Watson Natural Language Understanding divides conversations spoken in Watson Assistant's Try it out window into classes of intents or entities. If a conversation is not divided into the desired classes, you can train the Assistant API in the Try it out window.
Now that you've created an auto-answer conversation using Watson Assistant, there's one more thing to do, that is, confirmation of the Skill ID. This is the ID you will need later to operate Watson Assistant as an API from Node-RED.
Check the Skill ID from the Skills screen by following these steps:
We have now created a chatbot service that automatically responds to chats. Next, let's integrate this with the Slack user interface.
Next, let's move on to the preparation of a Slack node on your Node-RED environment. Launch the Node-RED flow editor created on IBM Cloud.
What you do in this step is to install a node to connect to Slack in your Node-RED environment. The method is easy. All you have to do is find and install the node from the Manage palette window, which you've done several times in other chapters.
Follow these steps to proceed:
Important note
I believe that the Node-RED flow editor on your IBM Cloud has already been created as a service (as a Node.js application), but if you haven't done so already, refer to Chapter 6, Implementing Node-RED in the Cloud, to create a Node-RED service on IBM Cloud, before proceeding with this chapter.
You can also access the Slack App Directory top page with the following URL: https://<your workspace>.slack.com/apps.
The following URL is just an example: https://packtnode-red.slack.com/apps.
This URL is generated automatically depending on each workspace name on Slack.
Important note
Be careful when sharing bot user tokens with applications. Do not publish bot user tokens in public code repositories. This is because anyone can access the bot with this API token.
Now you are ready. Let's move on to the flow creation procedure.
So far, you've created a chatbot engine in Watson Assistant, created a Slack workspace, and integrated the Bot app, which you can use in that Slack workspace.
Here, we will combine these services with Node-RED and create a mechanism with Node-RED so that the bot will answer when talking in Slack's workspace.
Follow these steps to create a flow:
You already have Slack nodes and Watson nodes that are available to use for this hands-on tutorial.
Follow this procedure to set the parameters on each node. For the nodes that need to be coded, code them as follows:
a) Click the edit button (pencil icon) to open the Properties panel:
b) Enter the Token value you generated on your Slack Bots app. You can set any name for this configuration. In the example here, it's named packt-bot:
When you go back to the main panel of this node, you will see the configuration has been set in the Slack Client property.
c) Click the Done button to close this setting:
a) In the first function node, enter the following:
global.set("channel",msg.payload.channel);
msg.topic = "message";
msg.payload = msg.payload.text;
return msg
You can also refer to the following figure:
In this function node, the message that is posted on Slack is taken out from the JSON data sent from Slack and put in msg.payload again.
Another important process is to store the channel information sent from Slack in the Global variable in Node-RED. The channel information stored here will be used later when sending a response message back to Slack.
In the previous step, you connected Watson Assistant to Node-RED. This means that you can call the Assistant API from Node-RED without using an API key or secret.
When I double-click the assistant node to open the settings panel, I don't see any properties such as API keys. If you see them in your settings panel, it means that the Watson Assistant and Node-RED connection process is failing. In that case, perform the connection process again.
There is only one property to set here. Set the Watson Assistant Skill ID you wrote down earlier as the Workspace ID property in the assistant node's settings panel:
This completes the settings for the assistant node. Save your settings and close the settings panel.
In the first function node, enter the following code:
var g_channel=global.get("channel");
msg.topic = "message";
msg.payload = {
channel: g_channel,
text: msg.payload.output.text[0]
}
return msg
You can also refer to the following figure:
The second function node stores the autoresponder message returned from Watson Assistant in msg.payload.text, and gets the Slack channel information saved in the first function node and stores it in msg.payload.channel.
Next is the slack-rtm-out node, which is easy to set up:
a) Double-click on the slack-rtm-out node to open the settings panel.
b) You will see that the configuration named packt-bot you created is already placed in this node property. If it is not set yet, please select it from the drop-down list manually. Once you click on Done, the settings will be complete:
The debug node here simply outputs the log. No settings are required.
An auto-answer chatbot has been created using Slack. Let's try the conversation.
Now, let's actually have a conversation. Mention and talk to your bot (packt-bot in the example) on the channel where you added this bot app. Since the only conversations we are learning with Watson Assistant this time are greetings and listening to jokes, we will send a message from Slack that seems to be related to either of these.
First, let's say Hello. You will see a greeting kind of response:
Then send a message like Please tell me a joke. It randomly responds with a bot-selected joke as a reply:
Great work! You finally made the chatbot application with Node-RED.
If you want the flow configuration file to make this flow in your Node-RED environment, you can get it here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter12/slack-watson-chatbot-flows.json.
In this chapter, we experienced how to make a chatbot application with Slack, Watson, and Node-RED. This time, we used Slack as a chat platform, but you can use any chat platforms that have APIs, such as LINE, Microsoft Teams, and so on, instead of Slack.
This chapter is also very helpful for creating any applications that are not IoT-based. Node-RED can develop various applications by linking with any Web API.
In the next chapter, let's develop our own node. Of course, it can be used in any environment. Developing your own node with Node-RED means developing a new node that cannot be done with the existing nodes. This is surely the first step for advanced users of Node-RED.
18.219.22.169