Contents

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!

/images/chatengine-home-automation-screencast.gif

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).

This blog post was originally posted in PubNub Tutorials.

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.

ChatEngine

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).

/images/chatengine-home-automation.jpg

Setup and Introduction

Complete source code for this tutorial is available in our GitHub repository.

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.

/images/homeauto-chatengine-UI-1.png

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.

/images/homeauto-chatengine-UI-2.png

Setup

Watson Assistant Service Setup

You’ll first have to setup your Watson account to set up the Assistant service on IBM Cloud. Steps 1 to 7 in the GitHub README provide details to create and configure the conversation service. Note that the Watson Assistant was formerly called Watson Conversation Service.

PubNub ChatEngine Setup

To get started with ChatEngine, follow steps 8 to 14 in the GitHub README.

Demo

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.

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.

Conclusion

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 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.