Building a simple dice command in Slack

I was interested in implementing a simple command into Slack, and it turned out to be much easier than expected. Rather than outline the entire process, it’s probably better to just redirect you to the fantastic article that I used as a guide: Building a Slackbot with Ruby and Sinatra.

I did run into a couple differences, though. My Gemfile is a bit sparser, including only Sinatra and JSON:

And really, the JSON gem might not even be necessary. I also had to make a tiny change to the config.ru. I found that I needed to use “require_relative” rather than “require”.

And as you’ll see in the code below, I never wound up using HTTParty, because I didn’t need to. So naturally, it was excluded from the Gemfile.

Full code for my dice bot below:

It’s worth mentioning that this dice roller was designed specifically for Warrior, Rogue & Mage. So it defaults to an exploding 1d6.

I pushed it up to Heroku, and shockingly, it works!

You can add modifiers:

And here’s the exploding mechanic in action:

Pretty nifty. If I continue to work on this, I think I could just include additional POST route, and link that route up to whatever new command I want in Slack.

Show your support

Clapping shows how much you appreciated Caleb Cox’s story.