Nand Kishor Contributor

Nand Kishor is the Product Manager of House of Bots. After finishing his studies in computer science, he ideated & re-launched Real Estate Business Intelligence Tool, where he created one of the leading Business Intelligence Tool for property price analysis in 2012. He also writes, research and sharing knowledge about Artificial Intelligence (AI), Machine Learning (ML), Data Science, Big Data, Python Language etc... ...

Full Bio 
Follow on

Nand Kishor is the Product Manager of House of Bots. After finishing his studies in computer science, he ideated & re-launched Real Estate Business Intelligence Tool, where he created one of the leading Business Intelligence Tool for property price analysis in 2012. He also writes, research and sharing knowledge about Artificial Intelligence (AI), Machine Learning (ML), Data Science, Big Data, Python Language etc...

3 Best Programming Languages For Internet of Things Development In 2018
921 days ago

Data science is the big draw in business schools
1094 days ago

7 Effective Methods for Fitting a Liner
1104 days ago

3 Thoughts on Why Deep Learning Works So Well
1104 days ago

3 million at risk from the rise of robots
1104 days ago

Top 10 Hot Artificial Intelligence (AI) Technologies

2018 Data Science Interview Questions for Top Tech Companies

Want to be a millionaire before you turn 25? Study artificial intelligence or machine learning

Here's why so many data scientists are leaving their jobs

Google announces scholarship program to train 1.3 lakh Indian developers in emerging technologies

Building a sentiment analysis bot with IBM Watson and Raspberry Pi

By Nand Kishor |Email | Jun 14, 2017 | 12480 Views

Do you ever wonder how your teammates perceive your Slack chat messages? Maybe you sound heroic! Or unpleasant. Perhaps you project harsh undertones you haven't even noticed!

Imagine a helpful robot that analyzes your written communications. Now‚??-‚??stop imagining, because I created this (physical!) bot that analyzes message tone using IBM Watson, "reading" the emotion of each posted message!

In this article, I'll share how I created the bot.

First I'll show you how to use the Events API to capture messages, send them to IBM Watson for analysis, and then post the result using chat.postMessage.

Then I will present a totally optional but fun exercise! We will port our bot to a Raspberry Pi and reflect emotions using colors produced by an RGB LED.

The source code is available on GitHub.

Before Starting This Project

This project uses Node.js, so make sure Node.js is installed on your machine. You'll also need an IBM Bluemix account and add the Watson Tone Analyzer. Your service credentials will be required later.

In addition to the software bot, if you want to build a physical Raspberry Pi bot that shows the results with color LEDs, you'll need some affordable hack-friendly hardware. You can either buy the IBM TJBot Kit that includes the cardboard robot, or prepare separately:

1 Raspberry Pi 3 (If you are using Pi 2, you need a WiFi dongle too)

1 NeoPixel RGB LED

3 Female/female jumper wires

You need to install the latest Raspbian OS, connect to WiFi, and update and upgrade the system. See for additional instruction.

Building a Sentiment Analysis Bot

OK, let's build a Slack bot that reads messages and analyzes for emotional content!

Our bot's workflow:

1. Read each message on a channel

2. Send the message to IBM Watson for examination

3. If the likelihood of an emotion is above the given confidence threshold, post the most prominent emotion to the Slack channel

Creating and Configuring Your Slack App

First, let's create a new app at Fill out the (1) App Name and (2) choose a slack team that you are allowed to develop apps on:

At Add features and functionality, enable "Bots", "Event Subscription", and "Permissions". We will walk through them soon.

Also, you will need your API credentials you can find at the App Credentials section when you code. They identify your app to the Slack platform.

Setting up a Bot User

Click the Bots button under Add features and functionality, or the (1) Bot Users menu from the left navigation to set up a bot user. Then (2) give your bot a username you want:

Make sure to click the Save Changes every time you make a change in this configuration process.

Setting OAuth & Permissions

Click OAuth & Permissions from the left menu.

You will need the pre-generated OAuth Access Token, beginning with xoxb- during development. (Note: To distribute your bot to the rest of the world, you will need to set up an OAuth button, etc, however, this tutorial does not cover the process.)

Scroll down to Permission Scopes section to add the scopes you need to use for the bot. At the pulldown menu, choose bot:

Setting Event Subscriptions

To receive channel messages, we are going to use the Events API. Click Event Subscriptions. You need to enable the feature by setting the Request URL, but let's skip this for now.

Scroll down to Subscribe to Bot Events. Click the Add Bot User Event button, and select message.channels:

Setting up your Request URL

During the development, I am using a temporary URL from ngrok, which serves my localhost to a public URL. Download ngrok for your OS, unzip it, and install it by moving the file to the appropriate directory, for example, if you are using Mac, use the command, mv ngrok /usr/local/bin. You can learn more about ngrok at Using ngrok to develop locally for Slack.

Once you install it, run it on terminal:

$ ngrok http 5000

The ngrok tool generates a URL looking like for your local server at localhost:5000. Copy the ngrok URL and paste it into the configuration setup, but not so fast! The URL must be validated, and in order to do that you'll need to finally write some code. We will come back here later.

Running an Express Server

Express is a simple, modular web framework for node.js. We'll use it to quickly create our Request URL webhook receiver.

First, set up your node application:

$ npm init

And configure your application. Then install Express.JS and body-parser (for POST):

$ npm install express body-parser --save

Once finished installing these modules, create a index.js file, and instantiate the express server, listening on port 5000. Since you've set ngrok to localhost:5000, you must use the same port!

'use strict';

const express = require('express');

const bodyParser = require('body-parser');

const app = express();


app.use(bodyParser.urlencoded({ extended: true }));

const server = app.listen(5000, () => {

console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);});

Now, create HTTP POST route to handle the validation code. Let's create an /event route. This will be triggered every time an event arrives, like the message.channels event we want from public Slack channels.

When you enter the webhook URL on your configuration page, Slack will send a request with a challenge parameter, so your endpoint must respond with the challenge value to complete the handshake.'/event', (req, res) => {

if (req.body.type === 'url_verification') {



// will implement the bot here ...


Now, let's run this node script.

Then (1) go back to the Event Subscriptions, and (2) enter your ngrok URL with the route, for instance, If everything works fine, the Request URL will verify, and you should see a green checkmark like this:

Handling event subscriptions

Let's start coding your bot!

Go back to your index.js file and at the /events POST route you defined, we'll receive and handle the message payload.

Within the request handler, you need to check the payload to see (1) if the token value matches with your verification token (which you can find at the App Credentials section of the Basic Information), and (2) if the type value is the event_callback, extract the message text to be analyzed by Watson later:'/events', (req, res) => {

let q = req.body;

// 1. To see if the request is coming from Slack

if (q.token !== process.env.SLACK_VERIFICATION_TOKEN) {




// 2. Events - get the message text

else if (q.type === 'event_callback') {

if(!q.event.text) return;

analyzeTone(q.event); // sentiment analysis



If you just want to run the code locally, you may hard-code your token here. There are multiple ways to set your environment variables, but I usually set a .env file to include the variables. In the source code, you can take a look at the .env-test file to see how it is set.

Now, create a function, analyzeTone, which take the message text to be analyzed and some other info from the payload.

Using the Sentiment Analysis with Watson

Now, let's use IBM Watson for the sentiment analysis, so get your Service credentials from your IBM Bluemix account ready!

Go to your Bluemix console, and choose Tone Analyzer from the catalog under Watson

At the next screen, click Create.

Once you activate the Tone Analyzer, (1) click the Service Credentials from the left menu, and you should be able to obtain your username and password for this specific service by (2) clicking the little triangle by "View credentials". Continue Learning>>

Source: Medium