Ellie Strejlau

Introduction to Developing a Slack Bot with Bolt

A high level explanation of how the bot I built with friends works behind the scenes.
885 words — Approx. 4 minutes
Photo by Eleventh Wave on Unsplash

In the Peopleware Slack group I’m in, a bunch of us came from the same company. At that company, we gave each other positive or negative karma to encourage or discourage certain behavior respectively. Reddit also uses karma and has a pretty good explanation of how it works. A lot of teams use a chat bot to keep track of karma and kudos. There are a few existing services that also provide this functionality for teams that are willing to pay for it, which you can get up and running pretty quickly:

However, we are a relatively young special interest group and have a bunch of programmers. We started to miss having a bot to track points for us, so decided to roll our own, as programmers do. Anyone who knows me personally knows that I weirdly love writing chat bots. I decided to recycle a bot persona that I had previously used in multiple other Slack workspaces. Enter Weebo1. She is a friendly bot, named and given the likeness of the robot assistant to Professor Philip Brainard (Robin Williams 💔) in the film Flubber.

A high-level explanation of the tech stack

Bolt is a great bot starter built by the Slack team. It’s got fairly thorough API documentation. Because it’s built by the Slack team, it works very well with Slack; the down side is that it only works with Slack. I’m not going to go into getting the bot set up, because the Slack team graciously already did that for us and will likely stay more relevant than this blog entry for longer. (There’s even a tutorial on how to build a bot using Bolt on Glitch!) I chose the JavaScript API version because I’m much more comfortable with that than Python or Java. (If you like working with the aforementioned languages and want to get started writing chat bots, I highly recommend checking Bolt out.) I used Knex.js and PostgreSQL for the database connection and storage, respectively.

The infrastructure

As for the hosting, we decided to go with an active Node.js droplet on DigitalOcean. (My friend Joe Still got this all set up because I’m much less DevOps-y. Don’t worry, I gave him a ton of karma. Thanks again Joe!) I wrote the bot so that it can run on as many workspaces we choose by storing connection information like secret keys encrypted in our database. From there, Slack asks our bot for a challenge response, which is a hash that Bolt provides to verify its identity. If Slack receives the token it expects, it sends the events that we subscribed our bot to. In our case, we want to watch for at least all messages sent in public or private chat rooms and mentions of the bot itself. Depending on what kind of event comes in, we tell the bot how to respond.

An explanation of how the bot responds

As an example, in order to track karma, this is the workflow that the bot follows:

  1. The bot receives a message event, the contents of which likely looks like this: @user ++.
  2. We use a regular expression to extract the user ID and the action. In this example, ++ increments @user’s karma. (The regex pattern looks like this in JavaScript: /\<\@(U\w+?)\>\s*(\+\+|\-\-)/.)
  • In order to uniquely track each user, Slack sends the unique user’s ID as the @ in this case, which shows up in the event as <@U1234567>.
  1. We use the Slack user ID as a unique identifier in the database for each user and either increment (++) or decrement (--) their karma value based on the action in the message.
  2. Weebo then sends <@U1234567> now has ## point(s)! back to Slack. Slack automatically updates the user tag back to the user’s display name.

Here’s an example of how that looks in Slack:

Weebo's response

Active development with Bolt

This was a major challenge I had early on with development of all of my bots. In order to work locally, you will need a real Slack workspace to test the bot, either a dummy one or the one you plan on using the bot in. There are a couple of ways you can set this up:

  • Sign up for Ngrok and buy a named subdomain. Then, set up a development version of your bot in the Slack app management interface.
  • Slack has also recently introduced Socket Mode.

I’ve found success with both tactics, but I recommend Socket Mode if you’re not familiar with Ngrok, can’t swing the $5/month cost, don’t have a domain with which Slack can communicate, or if you’re working behind a corporate firewall that would otherwise block the requests.

That’s all for now! I’m hoping to follow this up with more code samples of how to use Bolt and how we continue to use Weebo to keep us connected.

1 Because I don’t want to get sued by Disney (or anyone, for that matter), a disclaimer: I’m not claiming ownership of Weebo, Flubber, or any Disney properties. This is also not a widely distributed app that can be installed. If it ever becomes one, we will come up with an original name and avatar.

© 2021 Ellie Strejlau. All rights reserved, except disclaimers (below) and quotes (cited).

This site was built with GatsbyJS. The social media icons were made by Pixel perfect. Most other icons made by Smashicons. Both can be found at Flaticon. Patterned backgrounds are from SVG Backgrounds.