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
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:
- The bot receives a message event, the contents of which likely looks like this:
- We use a regular expression to extract the user ID and the action. In this example,
- 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
- 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.
- 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:
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.