How to write a Slack bot in Ruby:

  1. 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 like apt or yum to install it.
  2. 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.
  3. 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.
  4. 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'
  1. Next, create a new Slack::Web::Client object and use the auth_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!'
  1. 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}"
  1. 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 the on :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”.

  1. You can also use the Slack::Web::Client to send messages to channels or users using the chat_postMessage method. For example:
client.chat_postMessage(channel: '#general', text: 'Hello, world!')

This will send a message to the #general channel saying “Hello, world!”.

  1. 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).
  2. 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.

  1. Finally, you can use the SLACK_APP_TOKEN and SLACK_BOT_TOKEN environment variables to store your Slack API tokens and use them in your Ruby script. To set these variables, you can use the export 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!

By Tech Thompson

Tech Thompson is a software blogger and developer with over 10 years of experience in the tech industry. He has worked on a wide range of software projects for Fortune 500 companies and startups alike, and has gained a reputation as a leading expert in software development and design.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress Appliance - Powered by TurnKey Linux