pubnub chatengine ibm watson chatbot

Build a Serverless Chatbot with Watson Assistant and ChatEngine

Chatbot adoption is growing. With improvements in natural language processing and AI tools, chatbots offer a glimpse of how the future of man-machine communication is going to be. And they aren’t just automating human interaction, but also understanding voice and text commands and triggering a device action.

Hey bot, turn on my lights!

pubnub chatengine

In this tutorial, we’re going to combine ChatEngine with the IBM Watson Assistant BLOCK using serverless PubNub Functions. Our application will receive text commands, process the command, and carry out an action (in this case, in a smart home).

Serverless Functions and Watson Assistant

Watson Assistant is a powerful IBM API that allows you to build and deploy chatbots. Assistant will act as a natural language processing backend for our chatbot. When you send a command in the chat window in natural language, the command is interpreted by Watson Assistant service and parsed into intent (the command to turn on or off) and entity (the device which should abide by the command). The entities and intents are programmed within the conversation service via an external script provided as part of this project.

So how is it serverless? We use PubNub Functions (which runs our Watson Assistant BLOCK), to act as an intermediary to accept the commands and access the Assistant service via external API using the credentials created on IBM Watson. There are no third party servers required. All business logic is executed directly over the PubNub network.


Building a chat app is usually a complicated job. Apart from the UI functionality, you also have to figure out the modalities of hosting the chatbots, design patterns to use and the integration with third-party applications. And not to mention, there’s the important question of “how do you reliably deliver chat messages across the globe in realtime?”

If you’ve used PubNub, then you know how robust and reliable it is in handling messages. ChatEngine builds upon this battle-tested infrastructure and provides a rich SDK for building chatbot applications. The ChatEngine is an open and extensible framework that provides an easy way to deploy chat functionality within existing applications or to build standalone & massively scalable chat applications.

ChatEngine has all the essential features of a typical chat app baked into it, such as private and group chat, user status, typing indicators, message history, unread message counts and read receipts. It is based on the object-oriented event emitter framework for building chat applications in JavaScript. It can also be seamlessly integrated with PubNub Functions, the serverless framework for building web applications on the fly (which is what we’ll be using today).


Tutorial Assets and Introduction

Tutorial Source Code
Complete source code for this tutorial is available in our GitHub repository.
A quick gif demo of the app in-action.


Our application is split into two parts:

Our ChatEngine-powered chat application that receives chat commands and sends status updates. Our chat app also sends the chat commands to the smart home and uses Functions to process the command and carry out the appropriate action.

Our smart home, which we’re simulating with an SVG-image of a home’s floor plan. In a real-world scenario, these would be smart devices in a home.


Watson Assistant Service Setup

You’ll first have to setup your Watson account to set up the Assistant service on IBM Cloud. Login to IBM Cloud with your IBM login ID. Go to catalog, then select Assistant under Watson services.


Enter a name for the service. Leave other fields to their default values.

Scroll down and select ‘Lite pricing plan’ and then click on ‘Create’.

The pricing plans shown to you may differ based on your IBM account type, region or other details.

From the service page, go to ‘Service credentials’ and click on ‘New credentials’.

On the ‘Add new credential’ dialog box, enter a name for the credentials or use the default name and click on ‘Add’

Your IBM Watson Assistant service and the credentials to access the service are created now. Copy the username and password from these credentials to a file on your computer.

We will use them later while configuring PubNub Function.

  • Clone this repository with git clone or download zip file from GitHub.
  • Open a terminal (or command prompt in Windows) on your computer.
  • Navigate to the root folder of this cloned repository and then install required packages with the command:
npm install --save
  • In the folder watson-chatbot in this cloned repository, create a file named .env.
  • Put your Watson Assistant service credentials obtained earlier in this .env file in the following format.

Note that the user name and password are different from your IBM login id and password.

Your completed .env file should look like this:

  • From the terminal, change directory to watson-chatbot and execute the script updatebot.js which creates a workspace within your Watson Assistant service and creates required entities, intents and dialogs.

The script will then output a message about the creation of workspace in Watson Assistant service. Copy the workspace id shown in the output on your terminal. We will need this id later.

cd  watson-chatbot
node updatebot.js
Created workspace: 1290c543-13f3-2893-g627-9f74k716t361

PubNub ChatEngine Setup

To get started with ChatEngine, login in or signup with PubNub, then head over the ChatEngine quickstart. Click on the ‘Setup’ button and PubNub will automatically create a new ChatEngine app. It will also create all required Functions for ChatEngine.

Head back to your PubNub Admin Dashboard. You should see a new ChatEngine app created for you.

Click on the app and you can see your publish/subscribe keys created for the app as well. Click on the keys.

On the next screen, click the copy buttons towards the right side of the keys and copy both publish and subscribe keys in a text file on your computer. We will use them later.

From the left sidebar, click on FUNCTIONS. You can see the function is already created for ChatEngine. Click on the Function box.

Within the Function, two modules are already created and running. These are required for ChatEngine functionality. We need to create one more module within this Function to access Watson Assistant service from within PubNub.

Click on the ‘+CREATE’ button which will pop-up a ‘Create a New Function’ dialog box.

In the dialog box to create a new Function module, enter a name, select trigger as ‘Before Publish or Fire’ and put an asterisk in the channel name box. Then click on ‘CREATE’ button on the dialog box.

A new function will be created with a default code. Replace this default code with the code from file chatbot-pubnub-function.js.

Replace the Watson credentials in this code with your Watson Assistant service credentials obtained in step 4 earlier.

Save the newly created module by clicking on ‘SAVE’ button and then restart the module by clicking on ‘Restart module’ button.

Configure PubNub ChatEngine

Open the file chat.js from the userapp/scripts folder. Replace the publish/subscribe keys in the file with your own publish/subscribe keys obtained in step 9 earlier. Save the file when done.

pubnub_pub_key = 'YOUR_PUBNUB_PUBLISH_KEY';
pubnub_sub_key = 'YOUR_PUBNUB_SUBSCRIBE_KEY'

Put your PubNub publish/subscribe keys in the chat.js file from homesimulator/scripts folder as well and save the file.

pubnub_pub_key = 'YOUR_PUBNUB_PUBLISH_KEY';
pubnub_sub_key = 'YOUR_PUBNUB_SUBSCRIBE_KEY'

Now open the file homeView.html in your browser and then in another browser open the file chat.html.

You can see that chat window shows two users online. One user is yourself and the other is your home.


Once the setup is done both for Watson and PubNub, it’s time to run the demo. To run the app you only need to open the chat in a local browser window and the home simulator in another browser window.

You can see that chat window shows two users online. One user is yourself and the other is your home. You can now send commands to your home. Try giving a few commands like ‘turn off Kitchen light’ or ‘turn off bedroom light’ and you can see the bulbs in home simulator being switched on and off as per the command.

pubnub chatengine

Home automation supported commands:

  • Turn on Living Room light. / Turn off Living Room light.
  • Turn on Kitchen light. / Turn off Kitchen light.
  • Turn on Bedroom light. / Turn off Bedroom light.
  • Turn on Portico light. / Turn off Portico light.
  • Turn on Children room light. / Turn off Children room light.

Instead of turn on, you can also use ‘switch on’ or ‘put on’.

ChatEngine In-Action

Let’s dig into how this app works.

You send a message in the chat window to ‘turn off kitchen light’. ChatEngine is running in the background sends the message to PubNub network. A Function running within PubNub gets triggered before the message gets published to another chat subscriber.

This function executes a REST API call to IBM Watson conversation service, which parses the message and returns the intent (‘turn off’) and entity (‘kitchen light’). The intent and entity are then added to the original message and then published over the network to other chat subscribers.

In our case, the other chat subscriber is the simulated home itself. When it receives the message, it looks for valid intent and entity.

The script in the simulated home then turns off the kitchen light in the simulated home SVG. This script also publishes another message through PubNub network to acknowledge and confirm the command execution as ‘Turned off kitchen light’.

The chat window for the user then shows this confirmation message.


ChatEngine provides a simple way to create scalable chatbots. In this chatbot demo, we explored only a few of the powerful features that ChatEngine offers. You can explore more advanced features as well as other plugins to support emoji or your customized plugins.

In this demo, we also leveraged the serverless infrastructure of PubNub to add innovative features to your chat applications using IBM Watson. Similarly, PubNub’s ChatEngine can seamlessly integrate with other third-party APIs at the cloud layer.

Try PubNub Today

Share this on facebookShare this on TwitterShare this on Linkedin