Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

slack-ruby-bot

> 编程语言
Open source

The easiest way to write a Slack bot in Ruby.

1.1K stars0 likes1 views
WebsiteGitHub

About

The easiest way to write a Slack bot in Ruby.

Slack-Ruby-Bot


Warning: As of December 4th, 2020 Slack no longer accept resubmissions from apps that are not using granular permissions, or so-called "classic apps". On November 18, 2021 Slack will start delisting apps that have not migrated to use granular permissions. This library implements legacy, real-time support for classic apps. You should not be building a new bot with it and use slack-ruby-bot-server-events instead. For a rudimentary bot you can even start with slack-ruby-bot-server-events-app-mentions. See MIGRATION for migration help.


The slack-ruby-bot library is a generic Slack bot framework written in Ruby on top of slack-ruby-client. This library does all the heavy lifting, such as message parsing, so you can focus on implementing slack bot commands. It also attempts to introduce the bare minimum number of requirements or any sorts of limitations. It's a Slack bot boilerplate.

If you are not familiar with Slack bots or Slack API concepts, you might want to watch this video.

Table of Contents

  • Useful to Me?
  • Stable Release
  • Usage
    • A Minimal Bot
      • Gemfile
      • pongbot.rb
    • A Production Bot
    • More Involved Examples
    • Commands and Operators
    • Threaded Messages
    • Bot Aliases
    • Generic Routing
    • Matching text in message attachments
    • Providing description for your bot and commands
    • Customize your command help output
    • SlackRubyBot::Commands::Base
    • Authorization
    • Built-In Commands
      • [bot name]
      • [bot name] hi
      • [bot name] help
    • Hooks
      • Implementing and registering a Hook Handler
        • Hooks registration on SlackRubyBot::Server initialization
        • Hooks registration on a SlackRubyBot::Server instance
        • Hooks registration on SlackRubyBot::Server class
    • Bot Message Protection
    • Message Loop Protection
    • Logging
    • Advanced Integration
    • Proxy Configuration
    • Model-View-Controller Design
      • Controller
      • Model
      • View
    • Testing
      • RSpec Shared Behaviors
      • Testing Lower Level Messages
    • Useful Libraries
  • Contributing
  • Upgrading
  • Copyright and License

Useful to Me?

  • If you are just trying to send messages to Slack, use slack-ruby-client, which this library is built on top of.
  • If you're trying to roll out a full service with Slack button integration, check out slack-ruby-bot-server, which uses this library.
  • Otherwise, this piece of the puzzle will help you create a single bot instance for one team.

Stable Release

You're reading the documentation for the next release of slack-ruby-bot. Please see the documentation for the last stable release, v0.16.1 unless you're integrating with HEAD. See CHANGELOG for a history of changes and UPGRADING for how to upgrade to more recent versions.

Usage

A Minimal Bot

Gemfile

ruby
source 'https://rubygems.org'

gem 'slack-ruby-bot'
gem 'async-websocket', '~>0.8.0'

pongbot.rb

…

SLACK_RUBY_BOT_ALIASES=:pp: table-tennis


```ruby
SlackRubyBot.configure do |config|
  config.aliases = [':pong:', 'pongbot']
end

Bots will also respond to a direct message, with or without the bot name in the message itself.

Generic Routing

Commands and operators are generic versions of bot routes. You can respond to just about anything by defining a custom route.

ruby
class Weather \w*)\?$/ do |client, data, match|
    client.say(channel: data.channel, text: "The weather in #{match[:location]} is nice.")
  end
end

You can also capture multiple matchers with scan.

ruby
class Market ?' do
      desc 'Tells you the weather in a .'
      long_desc "Accurate 10 Day Weather Forecasts for thousands of places around the World.\n" \
        'Bot provides detailed Weather Forecasts over a 10 day period updated four times a day.'
    end
  end

  # commands implementation
end

in case of your own command:

ruby
class Deploy * where  is production or staging'
  end
end

Customize your command help output

If you've used the help block described above to document your commands, you can provide your own implementation of outputting help for commands like so:

ruby
class Market (client, data) {  }, ->(client, data) {}]
})
Hooks registration on a SlackRubyBot::Server instance
ruby
# Register an object that implements `call` method
class MyBot::Hooks::Hello
  def call(client, data)
    puts "Hello"
  end
end

server.on(:hello, MyBot::Hooks::Hello.new)

# or register a lambda function to handle the event
server.on(:hello, ->(client, data) { puts "Hello!" })

For example, the following hook handles user_change, an event sent when a team member updates their profile or data. This can be useful to update the local user cache when a user is renamed.

ruby
module MyBot
  module Hooks
    class UserChange
      def call(client, data)
        # data['user']['id'] contains the user ID
        # data['user']['name'] contains the new user name
        # ...
      end
    end
  end
end
Hooks registration on SlackRubyBot::Server class

Example:

ruby
module MyBot
  class MyServer (client, data) {
      # data['user']['id'] contains the user ID
      # data['user']['name'] contains the new user name
    }
  end
end

These will get pushed into the hook set on initialization.

Either by configuration, explicit assignment or hook blocks, multiple handlers can exist for the same event type.

Bot Message Protection

By default bots do not respond to self or other bots. If you wish to change that behavior globally, set allow_bot_messages to true.

ruby
SlackRubyBot.configure do |config|
  config.allow_bot_messages = true
end

Message Loop Protection

By default bots do not respond to their own messages. If you wish to change that behavior globally, set allow_message_loops to true.

ruby
SlackRubyBot.configure do |config|
  config.allow_message_loops = true
end

Logging

By default bots set a logger to $stdout with DEBUG level. The logger is used in both the RealTime and Web clients.

Silence logger as follows.

ruby
SlackRubyBot::Client.logger.level = Logger::WARN

If you wish to customize logger, set logger to your logger.

ruby
SlackRubyBot.configure do |config|
  config.logger = Logger.new("slack-ruby-bot.log", "daily")
end

Advanced Integration

You may want to integrate a bot or multiple bots into other systems, in which case a globally configured bot may not work for you. You may create instances of SlackRubyBot::Server which accepts token and aliases.

ruby
EM.run do
  bot1 = SlackRubyBot::Server.new(token: token1, aliases: ['bot1'])
  bot1.start_async

  bot2 = SlackRubyBot::Server.new(token: token2, aliases: ['bot2'])
  bot2.start_async
end

…

ruby
class Agent  resource)
      # ... do some expensive work
    end
  end

  private

  def sanitize_resource
    self._resource.downcase
    result = yield
    puts "After read, result is #{result.inspect}"
  end
end

…

ruby
class MyView < SlackRubyBot::MVC::View::Base
  define_callbacks :logit
  set_callbacks :logit, :around, :audit_trail

  def initialize
    @mailer = setup_mailer
    @ftp = setup_ftp_handler
  end

  def email_admin(message)
    run_callbacks :logit do
      @mailer.send(:administrator, message)
    end
  end

  def react_thumbsup
    client.web_client.reactions_add(
      name: :thumbsup,
      channel: data.channel,
      timestamp: data.ts,
      as_user: true)
  end

  def react_thumbsdown
    client.web_client.reactions_remove(
      name: :thumbsup,
      channel: data.channel,
      timestamp: data.ts,
      as_user: true)
  end

  private

  def audit_trail
    Logger.audit("Sending email at [#{Time.now}]")
    yield
    Logger.audit("Email sent by [#{Time.now}]")
  end
end

…

ruby
group :development, :test do
  gem 'rack-test'
  gem 'rspec'
  gem 'vcr'
  gem 'webmock'
end

Use the respond_with_slack_message matcher.

ruby
describe SlackRubyBot::Commands do
  it 'responds with any message' do
    expect(message: "#{SlackRubyBot.config.user} hi").to respond_with_slack_message
  end
  it 'says hi' do
    expect(message: "#{SlackRubyBot.config.user} hi").to respond_with_slack_message('hi')
  end
end

Use the respond_with_slack_messages matcher for multiple messages.

ruby
describe SlackRubyBot::Commands do
  it 'responds with more than one message' do
    expect(message: "#{SlackRubyBot.config.user} count").to respond_with_slack_messages
  end
  it 'says one and two' do
    expect(message: "#{SlackRubyBot.config.user} count").to respond_with_slack_messages(['one', 'two'])
  end
end

Message matchers support regular expressions.

ruby
describe SlackRubyBot::Commands do
  it 'says hi' do
    expect(message: "#{SlackRubyBot.config.user} hi").to respond_with_slack_message(/hi/)
  end
end

Check that the bot called client.start_typing(channel: 'channel').

ruby
describe SlackRubyBot::Commands do
 it 'starts typing on channel' do
    expect(message: "#{SlackRubyBot.config.user} hi").to start_typing(channel: 'channel')
  end
end

Testing Lower Level Messages

You can test client behavior at a lower level by fetching the message hook. The following example expects a bot command to call client.typing(channel: data.channel).

ruby
describe SlackRubyBot::Commands do
  let(:app) { Server.new }
  let(:client) { app.send(:client) }
  let(:message_hook) { SlackRubyBot::Hooks::Message.new }
  it 'receives a typing event' do
      expect(client).to receive(:typing)
      message_hook.call(
        client,
        Hashie::Mash.new(text: "#{SlackRubyBot.config.user} type something", channel: 'channel')
      )
    end
  end
end

Useful Libraries

  • newrelic-slack-ruby-bot: NewRelic instrumentation for slack-ruby-bot.

Contributing

See CONTRIBUTING.

Upgrading

See CHANGELOG for a history of changes and UPGRADING for how to upgrade to more recent versions.

Copyright and License

Copyright (c) 2015-2020, Daniel Doubrovkine, Artsy and Contributors.

This project is licensed under the MIT License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubybotsrubyslackbot

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言