Creating your own Slack Bot

Iccha Sethi
5 min readJul 16, 2016

--

Slack has become increasingly popular at several companies as a means of communication. One of the benefits of Slack is its ease of integration with apps. Several teams use slack bots/apps to increase developer productivity — for example the github integration updates your team about any new github pull requests. Some teams have also written their own slack bots which can query production environments or tie in with your existing monitoring system.

This post is about going through some of the basics of building a slackbot based off of hubot and have some fun while doing it. Once you realize how simple it is, you can use it to build your own productivity bot.

Installing Dependencies

Any time I do an experimental project, I prefer doing it on a cloud server. So I spun up a new linux based cloud server, and did the basic updates and upgrades.

sudo apt-get update; sudo apt-get upgrade; sudo apt-get install vim

Hubot is writtten in coffeescript, which compiles in javascript. So next I installed nodejs package, which has npm the package manager built in. I used this link to follow instructions, but realized that the links were out of date. Below I have the updated instructions.

curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
sudo apt-get install nodejs

You can run a simple `node -v` to check if node was installed.

It was recommended that I check for any npm updates using `npm install -g npm`. Then I used the node package manager(npm) to install the other dependencies — hubot, yemon, coffeescript and generator-hubot. Yeoman is a very neat scaffolding tool if you are not familiar with it.

npm install -g npm
npm install -g hubot coffee-script yo generator-hubot

Getting Started and some troubleshooting

Next I created a folder in which my code was going to live.

mkdir your-bot-folder
cd your-bot-folder

Then I attempted to use some scaffolding magic provided by yeoman to generate my example hubot.

yo hubot

But unfortunately I ran into a permission error.

It turns out that the npm permissions were not set correctly by default. Some troubleshooting docs came in handy, and I attempted `yo hubot` again and this time it ran!

To test the sample hubot was very simple, all I had to do was run `bin/hubot`. But it threw some exceptions because by default the hubot is expected to be configured to be connected to heroku-keepalive and redis-brain. Redis-Brain is an external integration which uses redis to persist data instead of in memory.

./bin/hubot

You can view all the external integrations in `external-scripts.json`.

I went ahead and removed “hubot-heroku-keepalive” and “hubot-redis-brain” and re-ran `bin/hubot` and it started loading asking me for details about my hubot.

Tumblr API KEY

Woo! Now that I had all my dependencies set up, and example bot working, it was time for some customizations. I decided to write a simple Programmer Gosling Meme generator based on the tumblr feed. In order to obtain an api key to hit the tumblr api, I had to create an account and register my application — https://www.tumblr.com/oauth/register.

Writing the code for your Bot

All the custom scripts for your bot go into `scripts` folder. There are several good blog posts which explain the code structure of hubot script, so I will not go into details of that. Instead of making an api call and parsing the response, I found a tumblr bot that I decided to leverage. Installed the node package for it.

npm install tumblrbot

I added the HUBOT_TUMBLR_API_KEY as an env variable.

export HUBOT_TUMBLR_API_KEY=my_api_key

To ensure it is added you could, print your environment variables and check if it is listed.

printenv | grep HUBOT

My code was pretty straightforward, which I have uploaded to my github repo.

Some of the recommended practices while writing a script include explicitly mentioning the environment variables and dependencies that another developer would have to download if they wanted to run your bot.

The robot.hear does regular expression matching for whenever the phrase is mentioned and performs the action described after res.send.

Then I tested my bot on the command line, and it returned the url of the image.

Integration with Slack

I installed the node package for hubot-slack integration and added hubot-slack to my dependencies list.

npm install hubot-slack --save

I also set up a temporary team and channel for testing purposes. And then clicked on team integrations and selected Hubot. Here I could customize the name and description of my custom Hubot and also obtain the Token I needed to complete my integration.

I added HUBOT_SLACK_TOKEN as an environment variable where my bot was running.

export HUBOT_SLACK_TOKEN=my_slack_token
printenv | grep HUBOT

I restarted my hubot with slack adapter.

/bin/hubot --adapter slack

And invited my bot to the #random slack channel.

Now anytime anyone types the phrase the bot is listening for, you get a tumblr image!

Future Optimizations

Some of the next steps I would like to try for the slack bot include, deploying it on heroku and using RedisToGo to persist information so that in case the bot is restarted it does not lose any information.

Source Code: https://github.com/isethi/gosling-hubot

--

--

Iccha Sethi
Iccha Sethi

Written by Iccha Sethi

Interests include technology, building team culture, books and food. Engineering Leader.

No responses yet