How to write a Slack bot in Ruby:
- First, make sure you have Ruby installed on your system. You can check if you have it installed by running
ruby -v
in a terminal. If you don’t have it installed, you can download it from the official Ruby website (https://www.ruby-lang.org/) or use a package manager likeapt
oryum
to install it. - Next, you’ll need to create a new Slack bot. Head to the Slack API website (https://api.slack.com/) and log in with your Slack account. Click on the “Start Building” button, give your bot a name, and select a development workspace. Click the “Create App” button to create your bot.
- On the next page, click the “Bots” tab and then click the “Add a Bot User” button. Give your bot a display name and default username, and click the “Add Bot User” button. This will create a new bot user for your app.
- Now it’s time to start writing some code. Create a new Ruby script and import the
slack-ruby-client
gem at the top of the file:
require 'slack-ruby-client'
- Next, create a new
Slack::Web::Client
object and use theauth_test
method to verify that your bot can authenticate with the Slack API:
client = Slack::Web::Client.new
client.auth_test
puts 'Successfully authenticated with Slack API!'
- To start listening for events, you’ll need to use the
rtm_start
method to start a Real Time Messaging (RTM) session and receive a WebSocket URL that you can use to connect to the Slack API:
rtm_client = client.rtm_start
puts "Connected to Slack RTM API! WebSocket URL: #{rtm_client.url}"
- Now it’s time to start listening for events and responding to them. You can use the
Slack::RealTime::Client
class to connect to the RTM API and handle events as they occur. For example, you can use theon :message
event to have your bot respond to messages in channels:
Slack::RealTime::Client.new.tap do |rtm|
rtm.on :message do |data|
case data['text']
when /^!hello/
rtm.send_message(channel: data['channel'], text: 'Hello!')
end
end
end
This code will have the bot respond with “Hello!” whenever it sees a message starting with “!hello”.
- You can also use the
Slack::Web::Client
to send messages to channels or users using thechat_postMessage
method. For example:
client.chat_postMessage(channel: '#general', text: 'Hello, world!')
This will send a message to the #general
channel saying “Hello, world!”.
- To deploy your bot, you’ll need to run your script on a server that is always online. You can use a hosting service like Heroku or AWS to run your bot 24/7. Alternatively, you can run your bot on a dedicated server or a virtual private server (VPS).
- To run your bot on a VPS, you’ll need to install Ruby and any required dependencies on the server, and then set up a process manager like
pm2
to keep the bot running in the background. For example:
# Install Ruby and dependencies
apt install ruby ruby-dev
gem install bundler
# Install pm2
npm install -g pm2
# Run the bot with pm2
pm2 start my_bot.rb
This will start the bot in the background using pm2, which will keep it running even if you close the terminal or log out of the server.
- Finally, you can use the
SLACK_APP_TOKEN
andSLACK_BOT_TOKEN
environment variables to store your Slack API tokens and use them in your Ruby script. To set these variables, you can use theexport
command in the terminal:
export SLACK_APP_TOKEN='YOUR_SLACK_APP_TOKEN_HERE'
export SLACK_BOT_TOKEN='YOUR_SLACK_BOT_TOKEN_HERE'
Then, in your Ruby script, you can access these variables using the ENV
object:
client = Slack::Web::Client.new(token: ENV['SLACK_BOT_TOKEN'])
This will allow you to easily manage your API tokens without hardcoding them into your script.
I hope this tutorial has been helpful and gives you a good starting point for building your own Slack bot in Ruby!