This post was originally written on April 18, 2020 on my personal website. Original post here. At the end I give an update on how the company has responded to my slack bot changes.
Our company has been working from home for about 6 weeks now, and most all of our communication has been over slack. Any sort of personality you’ve worked hard to build in the office has to be converted into a steady stream of gifs. I figured it might be more enjoyable looking at slack if there were a fun and lighthearted bot responding and livening up the chat.
I want to build out a passive slack bot that quietly does things in the background without announcement. I would rather have an end user be surprised and pleased the bot did something rather than having to remember all the commands associated with it.
I figure I can achieve this with two different ideas. One, have the bot insert gifs for all one word messages, and two, create a secret password game like on Pee-wee’s playhouse. I figure anytime an employee uses the password in a message the bot let’s us know by sending a specific password gif.
Auto Gif Sending
Luckily, our company already has a slackbot called “Marvin” which does things like tell us about open pull requests and taking notes on Airtable. We have a heroku http server hosting our bot, and we also have StatusCake ping the instance every 5 mins to keep it alive without having to upgrade from the free tier. For our bot, we built it on ruby using the ruby slack bot gem.
Using the match feature, we can construct a regex which looks for just one word messages.
This block tells the bot to respond back the word just sent for all one word messages.
You can run your chatbot with this command.
$ SLACK_API_TOKEN=... bundle exec ruby chatbot.rb
That wasn’t so bad at all. And here’s the result:
The Password Game
Once again, with the password game we want to be searching all messages for a regex match, but this time we want to allow the match to happen anywhere in the message. We can use the gem’s scan method to look for a match in any part of the message. Let’s start with a regular expression like this.
Great. This code snippet will allow our bot to place a gif anytime we have a message with the hidden password. But what if we want to play too, and don’t want to have to deploy every time we change the password? Can we generate one? For that, I found this simple to use gem called Spicy Proton. Word generation is as easy as
secret_word = Spicy::Proton.noun
Finally, let’s supply a couple of commands to allow the user to guess the secret word, and another for giving up and asking what the secret word is.
That’s all folks, and it didn’t take too much work to make it happen. In the following example “chive” is the password.
It might be nice to work on a leader board of users who have guessed the secret word the most. Or a cron job that displays at the end of the day what the most commonly used emjoi’s were. Write me if you’d like to see these come to life.
After making these changes to our company slack bot I’m happy to say people have a special place in their hearts for marvin, although with some hitches. Some coworkers asked me to take the gif responses out of certain channels, and once that was done, it’s brought a lot of amusement. As for the password game, it’s a very rare occasion when someone says the word and I think people get a kick out of it.
I’m yet to implement the leaderboard feature, but it might be fun to have a “potty mouth” award where marvin shames the most vulgar in each channel every month.