Building an IBM Watson bot with Node.js + Botkit
Bots are fun and useful with new businesses being built daily around bot interaction models. We recently launched our new Team Messaging API — Glip - at Enterprise Connect and we’re excited to show off more of the capabilities of the platform we’re building at RingCentral, alongside with IBM Watson Bluemix and Botkit!
A step-by-step tutorial walking through the Github code is available here!
Prerequisites
- Two free RingCentral Developer accounts
- A free IBM Bluemix Developer account
Creating a new RC App
Login to the RingCentral developer account to create a new app. Click on “Create App” which will create your app and auto-create a new sandbox account if you don’t already have one.
Enter the application name and description.
There are two paths for development on the RingCentral platform: making a private app which is just for your team, or a public app for public distribution through our App Gallery that others can use.
In the OAuth settings, make sure the permission selected is Glip. This permission is required to build chatbots.
Once the account has been created, you will be redirected to the dashboard. The App Key, App Secret, and the phone number will be used to play with the bot in the team. Save it for the next step.
Botkit Setup
Botkit is one of the most popular open-source toolkits for building bots, providing the essential building blocks . Botkit supports multiple frameworks, but here we will be looking at Glip. Glip is currently in review process with Botkit but it can be install directly from Github. Bot can communicate with your team in three ways:
- Incoming webhooks: is a simple way to post messages from external sources in Glip. e.g JIRA bots which post into your team when a new ticket has been assigned to you.
- Slash Command: are custom commands written by developers like you. They start with a backslash (/) and act as a trigger event to perform some action. Slash commands are currently not available in Glip. They are scheduled to be available by end of year.
- Real Time Messaging API: allows you to send and receive messages from a Glip user.
With some theory behind us, let’s start building our sample application. This example will focus on getting post messages from a group or team in Glip and pass the message to IBM Watson to gather some personality insight.
What are Personality Insights ?
According to IBM, “Personality Insights extracts personality characteristics based on how a person writes. You can use the service to match individuals to other individuals, opportunities, and products, or tailor their experience with personalized messaging and recommendations. Characteristics include the Big 5 Personality Traits, Values, and Needs. At least 1200 words of input text are recommended when using this service.”
As a first step, clone the sample application from Github. Now create a copy of the .env.template file and rename it to .env. The file should look something like this:
GLIP_SERVER=https://platform.ringcentral.com
GLIP_APPKEY=
GLIP_APPSECRET=
GLIP_USERNAME=
GLIP_PASSWORD=
GLIP_EXTENSION=
WATSON_USERNAME=
WATSON_PASSWORD=
WATSON_URL=https://gateway.watsonplatform.net/personality-insights/api
WATSON_VERSION_DATE=2016-10-19
WATSON_VERSION=v3
Then use the below code to install all dependencies including botkit and watson-developer-cloud.
npm install --save
Now let’s connect Botkit with Glip using the following code snippet:
"use strict";
require('dotenv').config();
var Botkit= require('botkit'),
os = require('os'),
http = require('http'),
request = require('request'),
watson = require('watson-developer-cloud');
var controller = Botkit.glipbot({
debug: false,
});
var bot = controller.spawn({
server: process.env.GLIP_SERVER,
appKey: process.env.GLIP_APPKEY,
appSecret: process.env.GLIP_APPSECRET,
username: process.env.GLIP_USERNAME,
password: process.env.GLIP_PASSWORD,
extension: process.env.GLIP_EXTENSION,
}).startRTM();
controller.setupWebserver(process.env.port || 3000, function(err, webserver){
webserver.get('/', function (req ,res) {
res.send(':)');
});
controller.createWebhookEndpoints(webserver, bot);
});controller.hears(['watson'], 'message_received', function (bot, message) {
bot.reply(message, "hi, this is watson, How can I help you ? ");
});
In the above example, when someone mentions “watson” the bot responds back with “Hi, this is watson. How can I help you?”.
Now let’s make the bot intelligent by integrating it with IBM Watson’s Personality Insights.
IBM Watson
IBM Watson offers AI and Machine Learning services through its Bluemix cloud platform. To get started quickly with Watson and Bluemix, you can create a free account.
IBM Watson offers many services for AI and Machine Learning. For this example, we will use the Personality Insights services. Use this document to setup and gather credentials for Personality Insights.
Now update the .env file created above with the Personality Insights credentials.
GLIP_SERVER=https://platform.devtest.ringcentral.com
GLIP_APPKEY=
GLIP_APPSECRET=
GLIP_USERNAME=
GLIP_PASSWORD=
GLIP_EXTENSION=
WATSON_USERNAME=
WATSON_PASSWORD=
WATSON_URL=https://gateway.watsonplatform.net/personality-insights/api
WATSON_VERSION_DATE=2017-02-02
WATSON_VERSION=v3
Initiate Personality Insights API using the snippet below:
var watson = require('watson-developer-cloud');var personality_insight = watson.personality_insights({
username: process.env.WATSON_USERNAME,
password: process.env.WATSON_PASSWORD,
url: process.env.WATSON_URL,
version: process.env.WATSON_VERSION
});
Now let’s integrate it to the chat-bot logic:
controller.hears(['analyze'], 'message_received', function (bot,message) {
bot.api.posts().get({groupId: message.channel}).then(function(history) {
var messages = [];
for(var i=0; i < history.records.length; i++){
messages.push(history.records[i].text);
}
var corpus = messages.join("\n");
personality_insight.profile(
{
text: corpus,
language: 'en'
},
function (err, response) {
if(err){
console.log('error:', err)
}else{
controller.startTask(bot, message, function (task, convo) {
var top5 = response.personality;
for (var c = 0; c < top5.length; c++) {
convo.say('This channel has ' + Math.round(top5[c].percentile*100) + '% ' + top5[c].name);
}
})
}
}
)
});
});
As you can see, when someone says “analyze”, it will gather the history of the post messages in the group and send it to Watson to analyze. Watson analyzes the post messages and responds back with the top 5 personality insights based on the post messages history as shown below.
Conclusion
The goal of the post has been to get developers started with building bots in Glip with IBM Watson. Personality Insights is just one of the several API’s available in IBM Watson cloud. You could use other APIs like Tone Analyzer, Text to Speech, Conversation … etc. to build more advanced bots that are fun and interesting. The imagination is the limit!
A step-by-step tutorial is also available here!
Give it a try, and I more than welcome any kind of feedback!