2016-02-29

Deploying a Slack bot on Heroku

Have you ever wished that you could get IRC, but with an enterprise pricing plan and fancy proprietary desktop and mobile clients? Then Slack is for you!

All jokes aside, the free version of Slack works well enough if you have a small group of people and just want to get some basic group chat features without having to mess around too much. Plus, the mobile clients are leaps and bounds ahead of any mobile IRC clients I’ve tried.

Sometimes you run out of interesting things to talk about, but you still want the chat to feel alive (especially during AU TZ…). Since an image is worth a thousand words, what could be better in a chat room than a bot that posts images?

All that is needed is to come up with a word or two, and the bot will reward the channel with a relevant image.

Bot? What?

I started out with slack-imgur bot, which you can add directly to your Slack group if you are satisfied with the existing feature set.

I had never used Heroku before, but since slack-imgur already came with a Procfile I just went with that. Setting up and deploying the bot was quite easy and straight forward, and Heroku is definitely on my shortlist if I ever need to prototype a web service. The fact that all of this is possible even on their free tier is kind of awesome as well.

Just getting some code to run on Basically, you set up a trigger (web hook) that listens for a certain word in the chat. When that word is found, a message is sent to the bot and the response is shown in the chat.

We decided that this wasn’t enough for our advanced image posting needs, so I hacked together a few changes:

  • Handles multiple word searches
  • Better searching and sorting
  • Support for subreddit search

Here’s an example of the subreddit image search:

The source code is available in my fork of slack-imgur. The rest of this post will attempt to explain how to configure and deploy everything.

Heroku setup

Make sure that you have your (free) Heroku account registered and setup, then install and configure the Heroku CLI tools.

While you’re at it, go ahead and register with imgur and get your own API keys. This is also completely free, but necessary to be able to use the imgur API.

Pull the modified slack-imgur code from GitHub, and cd to your local repo.

$ git pull https://github.com/pakesson/slack-imgur.git
$ cd slack-imgur

Run heroku create to create a new app. It’s going to print a whole bunch of stuff, but be sure note the URL (eg. http://falling-wind-1624.herokuapp.com/ or something similar). This will be used later when configuring the Slack webhook.

Set the imgur API key environment variables for the app in Heroku

$ heroku config:set IMGUR_CLIENT_ID=xxxxxxx
$ heroku config:set IMGUR_CLIENT_SECRET=xxxxxxx

Time to deploy! All you need to do is git push, and all the rest is taken care of automatically.

$ git push heroku master

Set the number of active dynos to make sure that the app is running,

$ heroku ps:scale web=1

Hooking it up and testing

Now the only thing left is to add a new webhook and test it.

Head to https://<group>.slack.com/apps/manage/custom-integrations and click Outgoing WebHooks.

Then choose Add Configuration, and add a trigger word and the URL noted previously (ending in herokuapp.com). You can optionally specify which channels to listen on, and also set a custom name that will be shown for all messages posted back to the chat.

And you should be set! If it doesn’t work, you can check the Heroku logs with

$ heroku logs