Learn How to make a bot for Slack with node.js

0
3952
Learn-How-to-make-a-bot-for-Slack-with-nodejs-740X296

Learn-How-to-make-a-bot-for-Slack-with-nodejs-740X296
Why did the chicken cross the road?
Image
Slack is a very popular communication and collaboration tool for teams that’s used by many well known companies. Slack is a bit like IRC, but improved with better design and some very useful features, like file sharing, persistent history, and more. And much like IRC, Slack also supports bots.

Slack bots range from useful little helpers that make you more effective at your job, to bots that update you on sports and let you play silly little games. Visit the Slack App Directory to see more examples of bots that have been created for Slack.

In this tutorial, I will show you how to use node.js and Botkit to create your very own Slack bot. In the process, we are going to make a bot that knows a few jokes about chicken crossing the road 🙂

Getting set up
First, we will need to install node.js. You can follow the official download and installation guide for your particular operating system.

There is a few options when it comes to hosting your bot, but most of them are paid, starting at $5-7/month. This is not a bad price, and most services, like Heroku, have a limited free tier. But for this tutorial we are going to go with OpenShift, a cloud application development and hosting platform. OpenShift offers a limited free tier that puts your app to sleep if it gets no traffic for 24 hours, but you can upgrade to the free Bronze plan to avoid this limitation.

After you sign up for an OpenShift account, go ahead and create a new node.js app. You don’t need to change anything other than the Public URL for your project.

To be able to interact with your OpenShift project, you should also download the rhc command line tool.

With rhc installed, let’s run the following code to download our newly created node.js app:

rhc git-clone -a NAMEOFYOURNODEJSAPP
cd NAMEOFYOURNODEJSAPP

Let’s also install Botkit, a node.js library for building Slack (and Facebook Messenger) bots.

npm install botkit --save

Assuming you already created a new Slack team, log in as your team’s administrator and go to your Apps and integrations page. On the Custom Integrations tab click Bots, and then Add Configuration.

Image1

After you name your bot, you will receive your bot’s API Token. You will need to add this token to your OpenShift app like this:

rhc env set SLACK_TOKEN=YOURSLACKBOTTOKEN -a NAMEOFYOURNODEJSAPP

If you want to also try running the bot from your local machine (you can use the command node server.js once you’re done with the updates below), you will have to set the environment variable on your computer as well.

I recommend looking up the instructions for your particular operating system. For example, on Linux, you could run the following command:

export SLACK_TOKEN=YOURSLACKBOTTOKEN

You can run rhc env list -a NAMEOFYOURNODEJSAPP to make sure the token was added correctly to your app. Once you confirm that, open the file server.js and replace the content of the file with following code:

var Botkit = require('./node_modules/botkit/lib/Botkit.js');
function get_response(){
var responses = [
'There was a car coming.',
'To get to the other side.',
'To get the newspaper.',
'Because it wanted to find out what those jokes were about.',
'To boldly go where no chicken has gone before!',
'Because the light was green.',
'I could tell you, but then the Chicken Mafia would kill me.'
];
return responses[Math.floor(Math.random() * responses.length)];
}
var controller = Botkit.slackbot({
debug: false
});
var bot = controller.spawn({
token: process.env.SLACK_TOKEN
}).startRTM();
controller.hears(['why did the chicken cross the road'], 'direct_message,direct_mention,mention', function(bot, message) {
bot.reply(message, get_response());
});

Let me quickly go through the code and explain what each part does.

var Botkit = require('./node_modules/botkit/lib/Botkit.js');

Loading node.js modules typically looks like this:

var Botkit = require('botkit');

For some reason, this doesn’t seem to work with the current version of Botkit (0.2.0), hence the above solution.

function get_response(){
var responses = [
'There was a car coming.',
'To get to the other side.',
'To get the newspaper.',
'Because it wanted to find out what those jokes were about.',
'To boldly go where no chicken has gone before!',
'Because the light was green.',
'I could tell you, but then the Chicken Mafia would kill me.'
];
return responses[Math.floor(Math.random() * responses.length)];
}

The get_response() function returns a random element from an array of prepared responses. We are going to use these to reply to users asking it a question about chicken crossing the road.

You could also save these as a module and then require it, but for simplicity of this tutorial, I am doing it this way.

var controller = Botkit.slackbot({
debug: false
});
var bot = controller.spawn({
token: process.env.SLACK_TOKEN
}).startRTM();

Here we are initializing the bot, and finally —

controller.hears(['why did the chicken cross the road'], 'direct_message,direct_mention,mention', function(bot, message) {
bot.reply(message, get_response());
});
hears() is a function that watches what is being said to the bot. You could also do something like this:
controller.hears(['hi', 'hello', 'howdy'], 'direct_message,direct_mention,mention', function(bot, message) {
bot.reply(message, 'Hello there! :wave:');
});

Now, save the server.js file and deploy your updated app.

git commit -a -m "Uploading my Slack bot"
git push

And you’re done! Go and /invite your bot to a channel, and ask it why did the chicken cross the road 🙂

Your bot can do a lot more, of course! Check out the Botkit GitHub repo for documentation and relevant links, and also some examples of what other people made with Botkit.

One quick note: OpenShift seems to expect your app to run on the 8080 port and returns a failure otherwise. This is not really a big issue, though, as the bot will still run correctly.

LEAVE A REPLY

Please enter your comment!
Please enter your name here