Building a Slackbot with IBM Watson, Part 1

Building a Slackbot with IBM Watson, Part 1

Eliminate toxic language in your slack channel with Toxic Comment Bot

2020, Mar 01    

Welcome to Part 1 of Building a SlackBot with IBM Watson! IBM Watson is a powerful artificial intelligence platform. Luckily, IBM is very developer friendly, and offers some outstanding APIs which let devs take advantage of Watson’s power. I had been interested in using one of these APIs for a while when I discovered their Toxic Comment Classifier. This is a machine learning model that utilizes natural language processing analyze a piece of text and detect six different types of toxic language. I spend a lot of time on the internet, so I could see many ways in which this model might be useful. I had recently been toying with the idea of making a Slackbot, so I decided to combine the two and make a Slackbot that detects and responds to toxic messages. In this article I will walk through my experience building the Toxic Comment Bot!

Part 1: Building the SlackBot

The first thing we want to do is build a skeleton of the bot which we can later add the Watson API to. In order to do this we will use Node.js and an NPM package called SlackBots.js. We will be making requests to an external API from our Node file, so we will want to install axios as well. Create a new folder with an index.js file, and make sure you have all three packages installed with before moving on. Be sure to run npm init as well, and answer all the prompts that follow. We are also going to be handling API keys, so feel free to configure your preferred environmental variable setup. If you haven’t done this before, I recommend following Sylwia’s great guide to hiding your API keys, where she also explains some of the risks of not hiding them properly.

Next we need to take care of our Slack setup. We will need a workspace for the bot to exist in. If you don’t have one in mind, you can make a new one. Next we are going to register our Slack bot. The first step is to create a new app, which you can do here. Enter the name of the app, and the workspace it will live in. Next go to Settings > Basic Information and click on Add features and Functionality; under that field, select Bots. There will be an button to Add Bot User. Click on that, configure the settings to your linking, and save your changes. Next, return to the Basic Information page, and click Install App to Workspace. You bot should now exist in your workspace! Navigate back to the menu and click on the OAuth & Permissions tab under the Features header. You should see two API tokens, we want the Bot User OAuth Access Token. Ok, time to get coding!

Step 1: configure dependancies

In our index.js file, we want to add our dependancies like so:

require('dotenv').config()
const SlackBot = require('slackbots');
const axios = require('axios')

Step 2: Initialize SlackBot

Now we want to create a new Slackbot instance, which we need to provide an object containing a name and out API key. This will look something like this:

const bot = new SlackBot({
    token: process.env.API_TOKEN,
    name: 'Toxic Comment Bot'
});

We also want to create an object called params, where we can give the bot an Emoji icon! This can be changed to any emoji you want of course.

const params = {
    icon_emoji: ':rotating_light:'
};

Next, we can create a function that executes when the bot starts (when we run our node server), that will send a message that will let us know the bot is up and running. There are many ways to do this: you can post a message to the channel, in a DM to a user, etc. Check out the documentation for more! For now we will stick to sending a message to the general channel:

bot.on('start', function() {
    bot.postMessageToChannel('general', 'I am listening...', params);
});

Go ahead and run the file with node index.js. If you see a message from your bot, then success! Your slackbot is officially up and running.


Thanks For Reading!

That is it for part 1! In part 2 we will be adding the IBM Watson Toxic Comment Classifier to our slackbot and having it respond to messages, so keep an eye out!