Nowadays, chatbots are widely used to communicate with customers or perform simple tasks, and their popularity is only increasing. Businesses are finding chatbots to be valuable because they can handle a high volume of requests 24/7 without getting tired, they don’t require breaks, and they can be deployed quickly and cheaply.
While there are many different ways to build a chatbot, in this article we will focus on how to build one using IBM Watson and Node-RED. Node-RED is a visual programming tool that runs on Node.js, and it makes it easy to wire together flows using the wide range of nodes in the palette. It also has a built-in library for interacting with IBM cloud services, including Watson.
The first thing you need to do is set up Node-RED. If you don’t already have Node.js installed, you can download it from the Node.js website.
Once you have Node.js installed, you can install Node-RED using the following command:
npm install -g node-red
Once Node-RED is installed, you can launch it by running the following command:
node-red
This will start the Node-RED server and open up the Node-RED web interface in your browser.
Now that you have Node-RED up and running, you need to set up your IBM Watson services. You can do this by going to the IBM Cloud catalog and creating a new instance of the following services:
Once you have created your Watson services, you need to go to the Service Credentials tab for each service and create a new set of credentials. Be sure to copy these credentials somewhere safe, as you will need them later.
Now that you have Node-RED and Watson set up, you can start creating your chatbot flow. To do this, go to the Node-RED web interface and click on the Create new flow button.
This will open up the Node-RED flow editor. The first thing you need to do is drag a http in node from the palette and drop it onto the canvas.
Next, double-click on the http in node to open up the node configuration. In the URL field, enter /chatbot
. This will be the URL that your chatbot will be accessible at. In the Method field, select POST
. Leave the Name field blank and click on the Done button.
Now, drag an IBM Watson node from the palette and drop it onto the canvas. Connect the http in node to the IBM Watson node.
Double-click on the IBM Watson node to open up the node configuration. In the Service field, select the Assistant service that you created earlier. In the Assistant ID field, enter the Assistant ID of your service. This can be found in the Service Credentials tab of your service. In the API Key field, enter the API Key of your service. This can also be found in the Service Credentials tab of your service.
Next, drag a http response node from the palette and drop it onto the canvas. Connect the IBM Watson node to the http response node.
Now, you need to configure the IBM Watson node to send the user input to Watson Assistant. To do this, double-click on the IBM Watson node to open up the node configuration. In the Input field, enter { "text": "$input.body" }
. This will send the user input to Watson Assistant as JSON in the format { "text": "user input" }
. In the Output field, leave the default value of $output.intents[0].intent
. This will cause the node to output the first intent that Watson Assistant detects.
Now, you need to configure the http response node to send the output of the IBM Watson node back to the user. To do this, double-click on the http response node to open up the node configuration. In the Status code field, enter 200
. This will send a successful HTTP response back to the user. In the Message field, enter $output.text
. This will send the output text of the IBM Watson node back to the user.
Now, you need to deploy your chatbot flow. To do this, click on the Deploy button in the top right corner of the screen.
This will deploy your chatbot flow and make it accessible at the URL that you specified earlier.
To test your chatbot, you can send a POST request to the URL that you specified earlier with a JSON body in the format { "text": "user input" }
.
For example, if you send the following JSON body to the chatbot URL:
{
"text": "Hello"
}
The chatbot will respond with the following JSON body:
{
"text": "Hello, how are you?"
}
You can also test your chatbot using the cURL command line tool. To do this, you can use the following command:
curl -X POST -H "Content-Type: application/json" -d '{"text": "Hello"}' <chatbot URL>
Replace <chatbot URL>
with the URL of your chatbot.
In this article, we have shown you how to build a chatbot using IBM Watson and Node-RED. We have also shown you how to test your chatbot using the cURL command line tool.
If you want to learn more about building chatbots, we recommend the following resources: