Slack Ruby Bot Server

repository·master·Indexed 42 days ago

https://github.com/slack-ruby/slack-ruby-bot-server

A Ruby library providing the web server and RESTful Grape API infrastructure needed to host a Slack bot serving multiple teams. It manages bot registration, OAuth configuration, and team lifecycles, supporting MongoDB (via Mongoid) and PostgreSQL (via ActiveRecord) storage backends. For full functionality, it is designed to be used with slack-ruby-bot-server-events.

Tokens
6.5K
Snippets
29
Records
34
Agent score
50%

What's inside Slack Ruby Bot Server

  1. Overview of Slack Ruby Bot Server

    master

    Slack Ruby Bot Server is a Ruby library that provides a web server and a RESTful Grape API designed to serve a Slack bot to multiple teams. It enables customers to install the bot via Slack buttons.

    To build a complete, functional Slack bot service, this library should be used in conjunction with slack-ruby-bot-server-events.

  2. Add OAuth fields to ActiveRecord Team model

    master

    When upgrading to version 1.2.0 or higher, new fields oauth_scope and oauth_version were added to the Team model. If you are using ActiveRecord, you must create a migration to add these columns. No action is required for Mongoid users.

    class AddOauthFields < ActiveRecord::Migration[5.0]
      def change
        add_column :teams, :oauth_scope, :string
        add_column :teams, :oauth_version, :string, default: 'v1', null: false
      end
    end
  3. Bootstrap a new Slack bot project

    master

    The slack-ruby-bot-server library handles the registration of a new bot but does not include built-in bot logic/functionality. To implement actual bot behavior, you should bootstrap your project using one of the following sample repositories:

    1. slack-ruby-bot-server-events-app-mentions-sample: Recommended if you want to handle a single specific kind of event (e.g., app mentions).
    2. slack-ruby-bot-server-events-sample: Recommended if you want to handle all kinds of events.
  4. Configure Mongoid or ActiveRecord dependencies

    master

    Starting from version 0.6.0, the mongoid gem is no longer a hard dependency. You must manually add either mongoid or activerecord (along with its driver) to your Gemfile. The order of declaration is important: the driver must be required before activerecord to avoid errors.

    # For Mongoid
    gem 'mongoid'
    gem 'slack-ruby-bot-server'
    
    # For ActiveRecord (with PostgreSQL)
    gem 'pg'
    gem 'activerecord', require: 'active_record'
    gem 'slack-ruby-bot-server'
  5. Configure ActiveRecord storage with PostgreSQL

    master

    To use ActiveRecord (e.g., with PostgreSQL), add pg, activerecord, otr-activerecord, and pagy_cursor to your Gemfile. This project supports ActiveRecord/Rails major versions 6.0, 6.1, and 7.0.

    1. Configure the connection in config/postgresql.yml.
    2. Establish the connection in your startup code using ActiveRecord::Base.establish_connection with the loaded YAML configuration.
    gem 'pg'
    gem 'activerecord', require: 'active_record'
    gem 'slack-ruby-bot-server'
    gem 'otr-activerecord'
    gem 'pagy_cursor'
    default: &default
      adapter: postgresql
      pool: 10
      timeout: 5000
      encoding: unicode
    
    development:
      <<: *default
      database: bot_development
    
    test:
      <<: *default
      database: bot_test
    
    production:
      <<: *default
      database: bot
    yml = ERB.new(File.read(File.expand_path('config/postgresql.yml', __dir__))).result
    db_config = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
                  ::YAML.safe_load(yml, aliases: true)[ENV['RACK_ENV']]
                else
                  ::YAML.safe_load(yml, [], [], true)[ENV['RACK_ENV']]
                end
    ActiveRecord::Base.establish_connection(db_config)
  6. Migrate from RTM to Granular Permissions (Events API)

    master

    Slack now requires new apps to use granular permissions. To migrate, you must replace the real-time implementation (slack-ruby-bot-server-rtm) with the events-based implementation (slack-ruby-bot-server-events).

    Because existing installations cannot be automatically migrated, users must reinstall the bot. This migration strategy allows you to run both the old (RTM) and new (Events) versions simultaneously using the same database. The slack-ruby-bot-server (v1.2.0+) tracks oauth_version and oauth_scope on the Team model, allowing the RTM server to ignore newer granular-permission installs and the Events server to ignore legacy RTM installs.

  7. Configure MongoDB storage with Mongoid

    master

    To use MongoDB as your storage backend, add the mongoid gem along with kaminari-mongoid and mongoid-scroll to your Gemfile. You must configure your database connection in a mongoid.yml file.

    gem 'mongoid'
    gem 'kaminari-mongoid'
    gem 'mongoid-scroll'
    gem 'slack-ruby-bot-server'
  8. Migrate a classic Slack app to use RTM via slack-ruby-bot-server-rtm

    master

    As of version 1.1.0, RTM (RealTime Messaging) components are no longer included in the core slack-ruby-bot-server gem. To upgrade an existing classic Slack app that requires RTM, follow these steps:

    1. Add slack-ruby-bot-server-rtm as a dependency in your Gemfile.
    2. Replace all references to SlackRubyBotServer::Server with SlackRubyBotServer::RealTime::Server.
    3. Replace require 'slack-ruby-bot-server/rspec' with require 'slack-ruby-bot-server-rtm/rspec'.
    4. Configure the bot to use Slack OAuth 1.0 and specify the required scopes.
    SlackRubyBotServer.configure do |config|
      config.oauth_version = :v1
      config.oauth_scope = ['bot']
    end
  9. Add activated user fields to ActiveRecord Team model

    master

    When upgrading to version 0.10.0 or higher, new fields were added to the Team model: bot_user_id, activated_user_id, and activated_user_access_token. If you are using ActiveRecord, you must create a migration to add these fields. No action is required for Mongoid users.

    class AddActivatedFields < ActiveRecord::Migration[5.0]
      def change
        add_column :teams, :bot_user_id, :string
        add_column :teams, :activated_user_id, :string
        add_column :teams, :activated_user_access_token, :string
      end
    end
  10. Set up a Slack App and local development

    master
    1. Create a Slack App at api.slack.com.
    2. Add SLACK_CLIENT_ID and SLACK_CLIENT_SECRET to your .env file.
    3. Set your Redirect URL in the Slack App settings under 'OAuth & Permissions'.
    4. For local testing, use a tool like ngrok to expose your local port (e.g., 5000) to a public URL, and use that public URL as the Redirect URL in Slack.
    5. Run the app using foreman start.
    $ foreman start
    $ ngrok http 5000
  11. Deploy a dual-version bot architecture

    master

    To migrate without downtime, deploy the new granular-permission app alongside the old RTM app using the same production database.

    1. New App Setup: Create a new Slack App with granular permissions. Use its SLACK_CLIENT_ID, SLACK_CLIENT_SECRET, and SLACK_SIGNING_SECRET for the new deployment.
    2. Shared Database: Ensure both the legacy RTM bot and the new Events bot point to the same database instance.
    3. DNS/Event URLs: Once deployed, update your DNS and ensure Slack Event URLs are correctly configured to point to the new service.
    4. Team Migration: Existing teams can be migrated by having them uninstall the old bot and reinstall the new one. The data remains intact in the shared database.