Making of a Smart Business Chatbot: Part 1

Published

Chatbots have become an incredibly popular method for providing real-time communication with customers, and in this first in a series of futuristic interaction articles, we’ll use Bluemix+Compose to build a chatbot that can reliably converse with our users while collecting data and gaining insights on their personality and mood.

It's the holy grail of business: to have the manpower (or womanpower) to have a personal conversation with every customer or business lead you come across. Of course, it's not possible especially for large businesses, to have the call-center personnel required for a personal interaction with every potential customer. But what if we could train a computer with Artificial Intelligence to interact with our users' the same way a person would?

In this first of a multi-part series, we'll walk through the process of creating a smart business chatbot using AI technology from the IBM Watson Data Platform to provide human-like text chat interactions with your customers. We'll also take a look at how we can gather information from those users and gain insights into our customers' mood and personality based on the language they use in the chat. Our example here will start with Slack as the chat interface, although this walkthrough could easily be adapted for Facebook Messenger, Twitter, or even in-website chat widgets.

Later in the series, we'll use Node-RED to receive data from Slack and to pipe it over to IBM Watson Conversation. We’ll then save those conversations in Compose MongoDB and analyze them using Watson Personality Insights and Watson Tone to determine the mood and personality of our customers. Finally, we’ll connect our chatbot to a knowledge-base built in Compose JanusGraph so we can provide faster and more robust answers to their questions.

Let's get started...

Setting up the Backend

Let's start by setting up the Node-RED instance responsible for bringing all of the pieces of our application together. First, let’s head on over to IBM Bluemix and create or log into our blue mix account. Then, create a new app based on the Node-RED starter project.

When you create the instance, you’ll need to give it a unique name by changing the App name field. Aside from changing the name, the rest of the default settings should work fine. Once you’ve chosen a unique name, hit the Create button and a new Node-RED server will be provisioned.

You’ll need to do a small amount of setup with your Node-RED instance before you can use it. The most important thing in this step is to set a password so your Node-RED instance is fully secured.

Once your setup is complete, log into the flow editor by clicking on Go to your Node-RED flow editor.

Finally, let’s set up the URL that Slack will use to post messages to our chatbot. Find the http node in the palette and drag it onto the canvas. Double-click it to configure, setting the METHOD to POST and the URL to /slackbot.

Next, drag a http out node onto the cavnvas on the right side of the http node. For now, we won’t have our chatbot do much - just log messages out into the Node-RED debug editor. Drag a debug node onto the canvas and drag a wire between the http node and the other two nodes.

Now that we have somewhere to send our Slackbot messages, let’s set up the Slack bot application itself.

Creating the Slack Chatbot

To set up the Slack Chatbot, you’ll first need to have a slack account that you can add apps to. You can do this by creating a slack team or logging into an existing team on the slack homepage.

Once you have a team created, you’ll need to log into the API Portal and create a new application. If you’re logged into a team account already, you can click on the Start Building button or Your Apps tab in the menu.

You’ll be redirected to the apps site where you can create a new Slack App. Click on the Create New App button and give your app a name.

We’d like Slack to send a message to the web service we created in Node-RED so that each time a message is sent to our app, the service receives a notification. We’ll do that from the Event Subscriptions menu. Select that menu item, and click on the Enable Events toggle so that it switches to the on position. Then, enter the URL for your Node-RED endpoint into the Request URL section. You can find the base URL for this in your Bluemix apps (ex: drwatson.bluemix.net) and the endpoint is what we entered into the Node-RED http node (/slackbot). The final result is something like the following:

https://<bluemix_application_id>.mybluemix.net/slackbot

Scroll down to the Subscribe To Bot Events section and, in the text input, type messages.

Select the message.channels and message.im options, which will tell Slack to send a request to our application when it receives a direct message to a bot user OR if a message is posted in a channel that our bot user is listening in.

Now we’re ready to create our bot user. Bot users show up in Slack like any other user, but when you send messages to or interact with the bot user, Slack will send those messages to an HTTP url that we’re going to specify. Select your newly-created application from the Your Apps page, click on the Bot Users menu item, and click the Add a Bot User button.

You’ll then be asked to choose a name for the bot and given a few options. You can choose any name you like, and turn on the toggle that says Always Show My Bot as Online. Click the Add Bot User button to finish.

We now have a slack bot user that’s ready to be added to our Slack team. Head back to the Basic Information tab in the settings page for your app and click on the accordion menu that says Install your app to your team. Then, click the Install App to Team button.

When the slack app prompts you to add the user, click Authorize and select the team in which you’d like to install the app. Once the app is installed, you will now see a new user in your application under the apps section with the name of your bot. Double-clicking the bot user will open a direct message dialog and we can test our integration by typing a message to the bot. We should see the message being received in the debug panel of our Node-RED flow editor.

Saving the Conversation

At this point, the Slackbot isn’t really talking back to our users. We’ll tackle that in Part 2 using the Watson Conversation API. For now, let’s just make sure that what our users’ are typing to us is saved in a database.

We can use Compose MongoDB to easily store the messages directly as they come in from Slack. We'll do this by heading over to Compose and creating a new MongoDB deployment.

Once you've created the deployment, click on the Add Database button and create a new database called chatbot.

Once the database is created, you'll be dropped into the create collections interface. Let's create a collection to store our users' conversations.

Next, we'll create a database user so we can connect to our database from Node-RED. Click on the Users tab and click the Add User button. Fill in the username and password field and click on the run button to execute the query.

We now have enough information to connect to our new database. Click on the Admin tab to show your connection string information and we'll drop it into Node-RED.

Now let's head back over to our Node-RED flow editor and wire in the MongoDB node. In the palette, type mongo in the search bar. Then, grab the mongodb out node (which has only an input port) and drag it to the right side of the http in node.

Double-click the node to configure it and click on the pencil icon next to the server field. Use the credentials you got from the admin section of your Compose MongoDB deployment and put those in the server configuration fields.

Fill in your credentials and put conversations in your collection field (if you named this something else in the previous step, make sure the name matches your collection). Choose the "insert" option and make sure the only store msg.payload object? option is checked and click done. Then click Deploy to deploy your latest changes.

That's it! You're now saving conversations into MongoDB.

You can read about connecting Node-RED to a Compose MongoDB database in our previous article on Power Prototyping with MongoDB and Node-RED.

Summing it Up

In this article, we tackled the first part of our smart business chatbot: creating a working Slack Chatbot on Bluemix. In the next part in this series, we’ll make our chatbot interactive by connecting it to the Watson Conversation API and gain insights into our users by passing our conversation data into Watson Personality Insights.


If you have any feedback about this or any other Compose article, drop the Compose Articles team a line at articles@compose.com. We're happy to hear from you.

attribution Pixabay user CyberRabbit

John O'Connor
John O'Connor is a code junky, educator, and amateur dad that loves letting the smoke out of gadgets, turning caffeine into code, and writing about it all. Love this article? Head over to John O'Connor’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.