Watson and Node: an efficient way to set up a chatbot

Grégoire Marnotte
Wolox
Published in
8 min readSep 19, 2018

--

This is going to be a complete guide about chatbots. Their utilities, possibilities, implementations… Details on how to start creating a chatbot with Watson AI and doing a custom implementation with a Node.js application.

Introduction

It’s astounding the pace at which how chatbots have been evolving recently and how Turing Test is pushing its limits more and more every day. Even regarding the question— has an AI model (Artificial Intelligence) passed or not the Turing test? — is still being debated by specialized searchers and universities. We now have access to AIs which can speak on an open conversation with fluency.

In our daily life, we experiment with these AIs every day, for example, Alexa or Siri being the most common. But also Facebook’s and Slacks’s chatbots are AI speaking with us or organizing our life.

In this article I would like to show two things:

  • Why you should consider implementing a chatbot for your business;
  • How simple it is nowadays to employ a chatbot for your business;

Good reasons to use chatbots

Surely when chatbot is mentioned the commonly related terms are complex technologies, high development cost, further future and other ones that make this technology seem unviable for your business.

  • Yes, technology was involved in the development of this AI but now there are implementations easy to access and use by everyone.
  • No, there’s not a high development cost. A basic chatbot could be set up in a few days.
  • It’s not for a further future because you are already dealing with them in your daily life.

To be more convincing we will present a solution proposed by IBM cloud services called Watson AI, and use a Node.js app to integrate it into a project.

Note: There is a lot of options giving access to chatbot AI, with the interface or not, with less or more AI complexity. As Botpress which propose a complete framework, MobileMonkey or Chatfuel for messenger. This article presents other solutions.

But before going ahead on how to implement it, let’s delve deeper into some reasons why this could prove a good way to improve your business.

Chat applications are exploding

For more than 3 years, as we can see in this report from Business Insider, chat applications have overcome, in a number of the user, social networks. Chat applications are using to communicate with friends and family but also with brand, organization, company….

Possibilities are infinite

Really with the performance of AI at disposition today possibilities are infinite and there is a lot of example. From customer services, to send money, passing by finding love.

Here I let you an article that came across just 11 ideas, from MobileMonkey CEO.

So what are you expecting? Let’s continue on this article and see how simple is to set up a chatbot.

Set up a chatbot with Watson AI

As announced above we will present in this article how to start with IBM cloud and his service Watson AI. Before starting a word on this AI. It is an AI developed to have a conversation in natural language and start to be recognized as very performant when winning American TV show Jeopardy!. Since it is only improving is performance.

IBM on his cloud service gives access to this AI with full customization. In this part, we will present the basic steps to configure a workspace and how to find all the needed data to use the API service.

Step 1: Prepare a workspace

First, to be able to follow this tutorial you need an account on bluemix.

Now you can create a Watson Assistance service reaching out to this link.

Choose your plan. For testing, you can start with the free one, but to run on production you will need to upgrade this.

Do not forget to change the name if needed.

For complementary info you can check this official tutorial. But here we will give all basic info to be able to follow set up bases.

As it’s specified in the oficial tutorial:

After you create a Watson Assistant service instance, you’ll land on the Manage page of the service dashboard.

On the Manage page, click Launch tool.

If you’re prompted to log into the tool, provide your IBM Cloud credentials.

Now you can create a workspace clicking on Create.

Workspace dashboard from Waston service

Step 2: Configure a workspace

After creating the workspace you can do almost everything you want, the AI is very performant. However to be able to configure it well you need to understand 3 basics concepts, for that, we will imagine we have a business for food delivering:

  • Itents: are words, list of words or sentences which could be present in the user message and allow the AI to choose the good response to the message.

For example, the user writes: “I want to order a pizza.” If an intent was set up with the word order in (#order), the AI will detect this word and could follow the conversation on specifying the order (time, place, quantity…).

  • Entities: represent a group of a word, object, a data type which could have same intents but for a different value.

For example, the user sends: “I want to order a pizza.” Here the intent is #order, but in my restaurant, we can order pizza or pasta. So I could create an entity @food which contains (pizza and pasta). This will allow to give context to intents and allow the AI to follow the order, with pizza specificities.

  • Dialog: are the responses which will be used by the bot when an intent is detected. If the bot detects multiple intents on a message it will choose by statistics (this statistics are evolving continuously and are more and more accurate if the bot is trained and used often as possible).

The basic tutorial from IBM Cloud cover lot of configuration aspect and well constructed, so to follow constructing your chatbot no doubt to consulted it.

Step 3: Find needed variables

Here the goal is to find all environment variables on WATSON service for Node.js application:

  • WATSON_WORKSPACE_ID
  • WATSON_VERSION
  • WATSON_URL
  • WATSON_USERNAME
  • WATSON_PASSWORD

On workspaces dashboard on View details you will find the WATSON_WORKSPACE_ID

A workspace from Watson services dashboard
Workspace details from Watson services dashboard, with workspace_id and other details

For the WATSON_VERSION use the date of creation of the service with format YYYY-MM-DD (2018–09–16 for example)

Now you can go there or click on left top corner to access to menu and select Dashboard.

In Dashboard select your service recently created and here you have the data you need: WATSON_URL, WATSON_USERNAME and WATSON_PASSWORD

General dashboard with all IBM cloud services
General details from Watson service with url, username and password

Note: You can also find most of this data on Deploy -> Credentials on the workspace configuration.

Part summary

This part show how simple is to access AI and start creating a chatbot. Now two challenges persist: configure all the workspace to have a performant chatbot — which will help you in your business — and integrate this one into your business.

The configuration will depend on each business implemented and will not be treated here.

The service allows you direct integration with slack and messenger, but at the time I am writing this article, to realize a custom integration, one of the best option is to use a Node.js application. You are lucky is the topic of the next part!

Integration with Node.js

Here we will build a basic application to communicate with Watson AI. It’s a step by step tutorial. As prerequisite you will need npm (version 3.9.1 at least, personally I used 5.6.0).

Step 1: Creating a basic express application

Starting by creating the application, enter this lines in your terminal:

Now, add this file in the directory:

With this simple config, we have a Node.js application running with the express framework. More simple impossible!

Run: node index.js and access to http://localhost:3000/ to see your “Hello world!”.

Step 2: adding some useful packages

To be able to do the things right we will need 3 packages more:

A little description on each one:

  • dotenv: simply storing secret or environmental variables;
  • body-parser: simplify body parsing;
  • watson-developer-cloud: a huge help to ease integration with Watson API. Thanks to this package integration are resumed to few lines of code.

So let modify a little bit our index.js:

Nothing magic was added:

  • dotenv package is required at the top;
  • body-parser received simple configuration for JSON bodies;
  • a new post endpoint is added and will use the ask method from the controller.

Step 3: adding logic to the controller

Now the controller configuration. Here you can add all the logic specific to your business. In this case, we simply call a service which encapsulates calls to Watson API and render the entire response body.

Step 4: creating the service to send messages to Watson AI

All needed logic for integration with your workspace are in the service:

From waston-developer-cloud, we use AssistantV1 method to configure general settings. Also, we set a promise to send a message and waiting for API response. In this promise, we respect the structure of the body request (other fields can be handled).

Do not forgot to set the .env file (do not forgot to add this file to .gitignore):

Step 5: Start chatting with your chatbot

Newly run: node index.js, and with a software like Postman do a post request to http://localhost:3000/ask/ with the following JSON body:

Note: Maybe you are asking you, why not directly integrate front-end with the Waston AI, mostly for security. With a back-end application, you can keep your credentials hide and handle better security process. Also because watson-developer-cloud package exists. And finally because if you adding some logic between Watson AI and printing the messages on the client user, it’s better to do it in back-end application.

Félicitations!! The only limit now is your imagination… Or maybe not, now you need to set up the front-end | fearful scream | !

It really seems is your lucky day! I have something for you. A widget for React.js that allow to set up this simply as almost a program line:

  • here you can find the react-chat-widget;
  • there a post which explains how to use it.

Conclusion

In this article, we tried to give all elements to understand how chatbot could be a solution or a help for your actual or future business. Moreover, we present a solution (Watson with Node.js application) step by step to allow implementing such technology.

Now, you hold all the card to start developing your own chatbot.

--

--