arun thampi  /  writing  / 

is it still time to build?

A Scalable Slack Bot App In 5 Minutes

Originally posted on Medium

2016 has been declared as the “year of the bots” and with bots declared as the next frontier in software distribution, creating scalable bot apps has become an increasingly important skill to have in the developer’s toolkit. This post goes through how you can write a massively scalable Slack Bot App in 5 minutes.

But what exactly is a Bot App?

Very simply, a Slack Bot App is a web application that is installed in multiple teams and thus is a multi-tenant bot application. Traditionally there have been open-source projects such as Hubot — the key difference being that a Hubot install works for only one team and not many teams.

Ok great! Are there any existing tools I can use?

There are lots of great libraries in a variety of languages that let you create simple bot applications. There are two main problems with these approaches:

  • They are not intended for multi-tenant or bot-as-a-service apps; but rather for single-tenant bot applications (a bot you write for your team, for example)
  • With multi-tenancy being a requirement; you have to be able to serve hundreds if not thousands of teams and a lot of these libraries won’t be able to serve them in a memory-efficient manner. The benchmark of memory efficiency is: can a 512MB Heroku dyno be able to serve 1000’s of teams.

Enter Stage Left — Relax

When I first started engineering work for Nestor (a programmable Slack bot platform), I looked for existing solutions that would help me write a Bot-As-A-Service but there weren’t that many forthcoming. What I wanted was a “message broker” — it would listen for Slack events for multiple teams, send them via a channel that my trusty ol’ Rails app can listen to and take action. My design goals for this broker were:

  • De-Coupled from the Web App: The broker should be de-coupled from the web app; thus enabling admins to scale the broker independently from the main web app.
  • Multiplexed Single Stream of Events: The web app would only have to listen to a single stream of events that will be multiplexed on to the channel by the broker.
  • Low Memory Footprint: The broker should be able to serve many thousands of connections from a single Heroku dyno.
  • Highly Available: Spawning multiple processes of this process should mean that it would tolerate failures *but* more importantly, also guarantee that events and messages sent on the stream are not duplicated.

With these goals in mind, Relax was created and today powers many thousands of teams on Nestor.

Ok great — show me the app

The entire source for the Rails App including installation instructions can be found at Here’s a summary of the most interesting concepts:

Data Models

The app uses Devise and Omniauth to authenticate via OAuth2 on Slack. User represents the currently logged in user, who can belong to multiple Teams via TeamMemberships. A user can also have “Identitites” which contain OAuth2 information such as the token, secret and metadata returned by the OAuth2 callback. A Team also has a single Bot which stores the bot_access_token returned by the OAuth2 callback.

OAuth Scopes for Slack

A couple of things to note while setting up OAuth2 scopes for your Rails app.

  • Set up a “Bot User” while creating your Slack Application and set a default username for that bot.

  • Remember to set the “bot” scope in your requested scopes. In this sample application, the scopes requested are team:read, users:read and bot.

Responding to Messages

The core of the bot logic is in config/initializers/relax.rb. The example provided in this application is very simple and only responds if the message text contains the word “hello” but it can be extended to perform more complicated tasks.

What’s Next?

The eventual goal would be to take this Rails app and create a Rails engine, making it super simple to create bot apps in Rails — watch this space!

In the meantime, if you find bugs/feature requests, please raise an issue.

Don’t want to run your own Bot App?

If all this sounds complicated and you want to start programming your bot in a couple of seconds, take a look at Nestor, a programmable Slack Bot Platform.