How to Create a Chat Bot in Ruby Using Slack API

Photo by Franck V. on Unsplash

When I first started out at a coding bootcamp a few months ago, classmate Dana Simmons and I decided it would be great fun to create a bot for our cohort’s Slack channel. Since we were learning Ruby at the time, I searched across the web on how to get started and came across this article which describes how to interact with Slack API using a couple of gems. Although this proved to be a great entry point, much more work was required to get everything running smoothly. I thought it would be beneficial to share our experience and create a simple new bot from scratch.

First, you will need a Slack workspace in which you can develop your bot. Go here to create a new workspace if you do not have access to a space in which you can do ample testing. The next step is to head here and click on Create an App. Give the app a descriptive name, and choose the development workspace from the previous step. Under the Add features and functionality header, choose the Bots option. Follow the instructions to add a bot user, and then on the left hand side under settings choose Install App and then Install App to Workspace. You will be prompted to authorize the bot user at this point, and then will be presented with two authorization tokens: a general access token and a token for the bot user. Make sure to hold onto the bot user token, prefixed with xoxb — we will need that in a later step.

Next, create a new local project directory and add a Gemfile by typing mkdir my-bot && cd $_ && touch Gemfile into the terminal. Add the following to the Gemfile:

source ""
gem 'slack-ruby-bot'
gem 'celluloid-io'

Create a project skeleton with mkdir bin config lib && touch config/.token. Copy and paste your bot user token into config/.token. If you plan to host your code online, it is important to make sure you do not check config/.token into your repository — you will receive an email almost immediately from Slack stating that security has been compromised and you will need to generate a new token. In order to avoid this, simply run the following in the root project directory:

git init
echo ".token" >> .gitignore

Let’s get started building out our bot. We will define a command for the bot using regular expression matching. Create a new file lib/bot.rb and add the following:

require_relative '../config/environment'
class MyBot < SlackRubyBot::Bot
command /.*\bhello\b.*$/i do |client, data, _match|
client.say(channel:, text: "hi")

Although config/environment.rb does not yet exist, we will create it in the next step. For now, let’s focus on what’s happening in the command. The regular expression /.*\bhello\b.*$/i is doing some matching magic: .* matches any text before and after the word hello, defined by the word boundary \b. The $ signifies the end of the string, and i at the tail of the expression ignores case sensitivity for matches. We are passing the command method a block, which instructs the bot to reply to the command in the channel with the text hi.

Time for the final setup. In config/environment.rb add the following code:

require 'bundler/setup'
require_relative '../lib/bot'
SlackRubyBot::Client.logger.level = Logger::WARN

The last line enables console logging while the bot is deployed for debugging purposes. Create a new file bin/run.rb and add the following code to start the bot:

require_relative '../config/environment'

The final piece of the puzzle is creating some tasks to set an environment variable for our authorization token and deploy the bot. For this, we will use Rake, a popular automation tool. In the root project directory, create a new file Rakefile and add the following tasks:

task :environment do
ENV['SLACK_API_TOKEN'] ='config/.token').chomp
namespace :deploy do
desc 'start mybot'
task :run => :environment do
require_relative 'bin/run'

Now, in the console, simply type rake deploy:run to deploy the bot. If everything has been set up correctly, you should see a message indicating the bot has been successfully connected to your Slack workspace. Create a new channel in the workspace and add the bot user to the channel. Address the bot by name — @mybot Hello! — and you should be greeted with a response! Note that due to the regular expression matching, the bot will recognize the command as long as the word hello is included somewhere in the string. This is what allows for parsing regular language and results in a level of perceived intelligence of the bot.

Where To Next?

Although getting the bot up and running required a bit of setup, it’s easy to see that it should be simple to program a variety of commands the bot should recognize in chat and respond accordingly. Ideally, you would like to deploy your bot to a service like Heroku instead of deploying the bot locally.

If you’d like to see the bot we built, head here to check out the repository.