Skip to content

Commit bb3d048

Browse files
authored
Merge pull request #129 from dblock/extract-rtm
Extract real-time legacy bots into slack-ruby-bot-server-rtm.
2 parents 34035e6 + e6eea3e commit bb3d048

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+267
-1242
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-07-17 08:36:05 -0400 using RuboCop version 0.81.0.
3+
# on 2020-11-15 11:38:22 -0500 using RuboCop version 0.81.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 3
9+
# Offense count: 1
1010
# Cop supports --auto-correct.
1111
# Configuration parameters: EnforcedStyle.
1212
# SupportedStyles: squiggly, active_support, powerpack, unindent
1313
Layout/HeredocIndentation:
1414
Exclude:
1515
- 'lib/slack-ruby-bot-server/info.rb'
16-
- 'sample_apps/sample_app_activerecord/commands/help.rb'
17-
- 'sample_apps/sample_app_mongoid/commands/help.rb'
1816

1917
# Offense count: 1
2018
Lint/AmbiguousOperator:
@@ -27,22 +25,19 @@ Lint/NonDeterministicRequireOrder:
2725
Exclude:
2826
- 'spec/spec_helper.rb'
2927

30-
# Offense count: 2
28+
# Offense count: 1
3129
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
3230
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
3331
Naming/FileName:
3432
Exclude:
3533
- 'lib/slack-ruby-bot-server.rb'
36-
- 'lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb'
3734

38-
# Offense count: 3
35+
# Offense count: 1
3936
# Configuration parameters: ForbiddenDelimiters.
4037
# ForbiddenDelimiters: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
4138
Naming/HeredocDelimiterNaming:
4239
Exclude:
4340
- 'lib/slack-ruby-bot-server/info.rb'
44-
- 'sample_apps/sample_app_activerecord/commands/help.rb'
45-
- 'sample_apps/sample_app_mongoid/commands/help.rb'
4641

4742
# Offense count: 3
4843
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ before_install:
3131

3232
script:
3333
- bundle exec rake
34-
- BUNDLE_GEMFILE=Gemfile && cd sample_apps/sample_app_$DATABASE_ADAPTER && bundle && bundle exec rake

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
### Changelog
22

3-
#### 0.12.4 (Next)
3+
#### 0.1.0 (Next)
44

5+
* [#129](https://github.com/slack-ruby/slack-ruby-bot-server/pull/129): Extracted RealTime components into [slack-ruby-bot-server-rtm](https://github.com/slack-ruby/slack-ruby-bot-server-rtm) - [@dblock](https://github.com/dblock).
56
* Your contribution here.
67

78
#### 0.12.3 (2020/11/14)

DEBUGGING.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,3 @@
33
### Locally
44

55
You can debug your instance of slack-ruby-bot-server with a built-in `script/console`.
6-
7-
### Silence Mongoid Logger
8-
9-
If Mongoid logging is annoying you.
10-
11-
```ruby
12-
Mongoid.logger.level = Logger::INFO
13-
Mongo::Logger.logger.level = Logger::INFO
14-
```
15-
16-
### Heroku
17-
18-
```
19-
heroku run script/console --app=...
20-
21-
Running `script/console` attached to terminal... up, run.7593
22-
23-
2.2.1 > Team.count
24-
=> 3
25-
```

README.md

Lines changed: 28 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,44 @@ Slack Ruby Bot Server
77

88
Build a complete Slack bot service with Slack button integration, in Ruby.
99

10-
If you are not familiar with Slack bots or Slack API concepts, you might want to watch [this video](http://code.dblock.org/2016/03/11/your-first-slack-bot-service-video.html).
11-
12-
A good [open-source demo](https://github.com/dblock/slack-strava) of a service built on top of this library is [Strava integration with Slack](https://slava.playplay.io).
13-
1410
# Table of Contents
1511

1612
- [What is this?](#what-is-this)
1713
- [Stable Release](#stable-release)
18-
- [Try Me](#try-me)
19-
- [Run Your Own](#run-your-own)
14+
- [Make Your Own](#make-your-own)
15+
- [Storage](#storage)
2016
- [MongoDB](#mongodb)
2117
- [ActiveRecord](#activerecord)
2218
- [Usage](#usage)
23-
- [OAuth Code Grant](#oauth-code-grant)
2419
- [API](#api)
2520
- [App](#app)
2621
- [Service Manager](#service-manager)
2722
- [Lifecycle Callbacks](#lifecycle-callbacks)
2823
- [Service Timers](#service-timers)
2924
- [Extensions](#extensions)
30-
- [Server Class](#server-class)
3125
- [Service Class](#service-class)
3226
- [HTML Templates](#html-templates)
3327
- [Access Tokens](#access-tokens)
34-
- [Example Bots Using Slack Ruby Bot Server](#example-bots-using-slack-ruby-bot-server)
28+
- [Sample Bots Using Slack Ruby Bot Server](#sample-bots-using-slack-ruby-bot-server)
29+
- [Slack Bots with Granular Permissions](#slack-bots-with-granular-permissions)
30+
- [Legacy Slack Bots](#legacy-slack-bots)
3531
- [Copyright & License](#copyright--license)
3632

3733
### What is this?
3834

39-
A library that contains a [Grape](http://github.com/ruby-grape/grape) API serving a [Slack Ruby Bot](https://github.com/slack-ruby/slack-ruby-bot) to multiple teams. This gem combines a web server, a RESTful API and multiple instances of [slack-ruby-bot](https://github.com/slack-ruby/slack-ruby-bot). It integrates with the [Slack Platform API](https://medium.com/slack-developer-blog/launch-platform-114754258b91#.od3y71dyo). Your customers can use a Slack button to install the bot.
35+
A library that contains a web server and a RESTful [Grape](http://github.com/ruby-grape/grape) API serving a Slack bot to multiple teams. Use in conjunction with [slack-ruby-bot-server-events](https://github.com/slack-ruby/slack-ruby-bot-server-events) to build a complete Slack bot service, or [slack-ruby-bot-server-rtm](https://github.com/slack-ruby/slack-ruby-bot-server-rtm) to build a Class RealTime Slack bot. Your customers can use a Slack button to install the bot.
4036

4137
### Stable Release
4238

4339
You're reading the documentation for the **next** release of slack-ruby-bot-server. Please see the documentation for the [last stable release, v0.12.3](https://github.com/slack-ruby/slack-ruby-bot-server/blob/v0.12.3/README.md) unless you're integrating with HEAD. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
4440

45-
### Try Me
46-
47-
A demo version of the [sample app with mongoid](sample_apps/sample_app_mongoid) is running on Heroku at [slack-ruby-bot-server.herokuapp.com](https://slack-ruby-bot-server.herokuapp.com). Use the _Add to Slack_ button. The bot will join your team as _@slackbotserver_.
48-
49-
![](images/slackbutton.gif)
41+
### Make Your Own
5042

51-
Once a bot is registered, you can invite to a channel with `/invite @slackbotserver` interact with it. DM "hi" to it, or say "@slackbotserver hi".
43+
We recommend you get started from a [slack-ruby-bot-events-sample](https://github.com/slack-ruby/slack-ruby-bot-server-events-sample) app to bootstrap your project.
5244

53-
![](images/slackbotserver.gif)
45+
### Storage
5446

55-
### Run Your Own
56-
57-
You can use one of the [sample applications](sample_apps) to bootstrap your project and start adding slack command handlers on top of this code. A database is required to store teams.
47+
A database is required to store teams.
5848

5949
### MongoDB
6050

@@ -67,8 +57,6 @@ gem 'mongoid-scroll'
6757
gem 'slack-ruby-bot-server'
6858
```
6959

70-
See the [sample app using Mongoid](sample_apps/sample_app_mongoid) for more information.
71-
7260
### ActiveRecord
7361

7462
Use ActiveRecord with, for example, PostgreSQL via [pg](https://github.com/ged/ruby-pg). Configure the database connection in `postgresql.yml`. Add the `activerecord`, `pg`, `otr-activerecord` and `cursor_pagination` gems to your Gemfile.
@@ -81,52 +69,31 @@ gem 'otr-activerecord'
8169
gem 'cursor_pagination'
8270
```
8371

84-
See the [sample app using ActiveRecord](sample_apps/sample_app_activerecord) for more information.
85-
8672
### Usage
8773

88-
Start with one of the samples above, which contain a couple of custom commands, necessary dependencies and tests, then [create a new Slack App](https://api.slack.com/applications/new).
74+
Start with the [slack-ruby-bot-events-sample](https://github.com/slack-ruby/slack-ruby-bot-server-events-sample) sample, which contain a couple of custom commands, necessary dependencies and tests, then [create a new Slack App](https://api.slack.com/applications/new).
8975

9076
![](images/create-app.png)
9177

9278
Follow Slack's instructions, note the app client ID and secret, give the bot a default name, etc. The redirect URL should be the location of your app. For local testing purposes use a public tunneling service such as [ngrok](https://ngrok.com/) to expose local port 9292.
9379

9480
Within your application, edit your `.env` file and add `SLACK_CLIENT_ID=...` and `SLACK_CLIENT_SECRET=...` in it.
9581

96-
Run `bundle install` and `foreman start` to boot the app. Navigate to [localhost:9292](http://localhost:9292). You should see an "Add to Slack" button. Use it to install the app into your own Slack team.
97-
98-
### OAuth Code Grant
99-
100-
The "Add to Slack" button uses the standard OAuth code grant flow as described in the [Slack docs](https://api.slack.com/docs/oauth#flow).
101-
102-
The button itself contains a link that looks like this:
103-
104-
```
105-
https://slack.com/oauth/authorize?scope=bot&client_id=<%= ENV['SLACK_CLIENT_ID'] %>
106-
```
107-
108-
Once clicked, the user is taken through the authorization process at Slack's site. Upon successful completion, a callback containing a temporary code is sent to the redirect URL you specified. The endpoint at that URL contains code that looks like this:
82+
Configure your app's [OAuth scopes](https://api.slack.com/legacy/oauth-scopes) as needed by your application.
10983

11084
```ruby
111-
# Instantiate a web client
112-
client = Slack::Web::Client.new
113-
114-
# Request a token using the temporary code
115-
rc = client.oauth_access(
116-
client_id: ENV['SLACK_CLIENT_ID'],
117-
client_secret: ENV['SLACK_CLIENT_SECRET'],
118-
code: params[:code]
119-
)
120-
121-
# Pluck the token from the response
122-
token = rc['bot']['bot_access_token']
85+
SlackRubyBotServer.configure do |config|
86+
config.oauth_scope = ['channels:read', 'chat:write:user']
87+
end
12388
```
12489

125-
The token is stored in persistent storage and used each time a Slack client is instantiated for the specific team.
90+
The "Add to Slack" button uses the standard OAuth code grant flow as described in the [Slack docs](https://api.slack.com/docs/oauth#flow). Once clicked, the user is taken through the authorization process at Slack's site. Upon successful completion, a callback containing a temporary code is sent to the redirect URL you specified. The endpoint at that URL contains code that persists the bot token each time a Slack client is instantiated for the specific team.
91+
92+
Run `bundle install` and `foreman start` to boot the app. Navigate to [localhost:9292](http://localhost:9292). You should see an "Add to Slack" button. Use it to install the app into your own Slack team.
12693

12794
### API
12895

129-
This library implements an app, [SlackRubyBotServer::App](lib/slack-ruby-bot-server/app.rb), a service manager, [SlackRubyBotServer::Service](lib/slack-ruby-bot-server/service.rb) that creates multiple instances of a bot server class, [SlackRubyBotServer::Server](lib/slack-ruby-bot-server/server.rb), one per team. It also provides [default HTML templates and JS scripts](public) for Slack integration.
96+
This library implements an app, [SlackRubyBotServer::App](lib/slack-ruby-bot-server/app.rb) and a service manager, [SlackRubyBotServer::Service](lib/slack-ruby-bot-server/service.rb). It also provides [default HTML templates and JS scripts](public) for Slack integration.
13097

13198
#### App
13299

@@ -201,7 +168,7 @@ The [Add to Slack button](https://api.slack.com/docs/slack-button) also allows f
201168
auth = OpenSSL::HMAC.hexdigest("SHA256", "key", "data")
202169
```
203170
```html
204-
<a href="https://slack.com/oauth/authorize?scope=bot&client_id=<%= ENV['SLACK_CLIENT_ID'] %>&state=#{auth)"> ... </a>
171+
<a href="https://slack.com/oauth/authorize?scope=<%= SlackRubyBotServer::Config.oauth_scope_s %>&client_id=<%= ENV['SLACK_CLIENT_ID'] %>&state=#{auth)"> ... </a>
205172
```
206173
```ruby
207174
instance = SlackRubyBotServer::Service.instance
@@ -248,26 +215,7 @@ A number of extensions use service manager callbacks and service timers to imple
248215
* [slack-ruby-bot-server-events](https://github.com/slack-ruby/slack-ruby-bot-server-events): Easily handle Slack slash commands, interactive buttons and events.
249216
* [slack-ruby-bot-server-mailchimp](https://github.com/slack-ruby/slack-ruby-bot-server-mailchimp): Subscribes new bot users to a Mailchimp mailing list.
250217
* [slack-ruby-bot-server-stripe](https://github.com/slack-ruby/slack-ruby-bot-server-stripe): Enables paid bots with trial periods and commerce through Stripe.
251-
252-
#### Server Class
253-
254-
You can override the server class to handle additional events, and configure the service to use it.
255-
256-
```ruby
257-
class MyServer < SlackRubyBotServer::Server
258-
on :hello do |client, data|
259-
# connected to Slack
260-
end
261-
262-
on :channel_joined do |client, data|
263-
# the bot joined a channel in data.channel['id']
264-
end
265-
end
266-
267-
SlackRubyBotServer.configure do |config|
268-
config.server_class = MyServer
269-
end
270-
```
218+
* [slack-ruby-bot-server-rtm](https://github.com/slack-ruby/slack-ruby-bot-server-rtm): Create RTM Slack bots.
271219

272220
#### Service Class
273221

@@ -302,15 +250,16 @@ end
302250

303251
### Access Tokens
304252

305-
By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores a `bot_access_token` as `token` that grants a certain amount of privileges to the bot user as described in [Slack OAuth Docs](https://api.slack.com/docs/oauth) along with `activated_user_access_token` that represents the token of the installing user. You may not want a bot user at all, or may require different auth scopes, such as `users.profile:read` to access user profile information via `Slack::Web::Client#users_profile_get`. To change required scopes make the following changes.
253+
By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) stores the value of the token with all the requested OAuth scopes in both `token` and `activated_user_access_token` (for backwards compatibility). If a legacy Slack bot integration `bot_access_token` is present, it is stored as `token`, and `activated_user_access_token`is the token that has all the requested OAuth scopes.
254+
255+
### Sample Bots Using Slack Ruby Bot Server
306256

307-
1) Configure your app to require additional scopes in Slack API under _OAuth_, _Permissions_
308-
2) Change the _Add to Slack_ buttons to require the additional scope, eg. `https://slack.com/oauth/authorize?scope=bot,users.profile:read&client_id=...`
309-
3) The access token with the requested scopes will be stored as `activated_user_access_token`.
257+
#### Slack Bots with Granular Permissions
310258

311-
You can see a sample implementation in [slack-sup#3a497b](https://github.com/dblock/slack-sup/commit/3a497b436d25d3a7738562655cda64b180ae0096).
259+
* [slack-ruby-bot-server-events-sample](https://github.com/slack-ruby/slack-ruby-bot-server-events-sample), a generic sample
260+
* [slack-rails-bot-starter](https://github.com/CrazyOptimist/slack-rails-bot-starter), an all-in-one Rails starter kit
312261

313-
### Example Bots Using Slack Ruby Bot Server
262+
#### Legacy Slack Bots
314263

315264
* [slack-ruby-bot-server-sample](https://github.com/slack-ruby/slack-ruby-bot-server-sample), a generic sample
316265
* [slack-sup](https://github.com/dblock/slack-sup), see [sup.playplay.io](https://sup.playplay.io)
@@ -320,7 +269,6 @@ You can see a sample implementation in [slack-sup#3a497b](https://github.com/dbl
320269
* [slack-api-explorer](https://github.com/slack-ruby/slack-api-explorer), see [api-explorer.playplay.io](https://shell.playplay.io)
321270
* [slack-strava](https://github.com/dblock/slack-strava), see [slava.playplay.io](https://slava.playplay.io)
322271
* [slack-arena](https://github.com/dblock/slack-arena), see [arena.playplay.io](https://arena.playplay.io)
323-
* [slack-rails-bot-starter](https://github.com/CrazyOptimist/slack-rails-bot-starter), an all-in-one rails starter kit built on top of the slack-ruby-bot-server and the slack-ruby-bot-server-events
324272

325273
### Copyright & License
326274

UPGRADING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
Upgrading Slack-Ruby-Bot-Server
22
===============================
33

4+
### Upgrading to >= 1.0.0
5+
6+
#### Extracted RealTime (Legacy) Support
7+
8+
New slack apps may no longer access RTM. Classic Slack apps can no longer be submitted to the app directory as of December 4th, 2020. In preparation for these changes slack-ruby-bot-server no longer includes RTM components by default. These have been extracted to a new gem [slack-ruby-bot-server-rtm](https://github.com/slack-ruby/slack-ruby-bot-server-rtm).
9+
10+
To upgrade an existing classic Slack app that uses slack-ruby-bot-server do the following.
11+
12+
1. Add `slack-ruby-bot-server-rtm` as an additional dependency.
13+
2. Replace any reference to `SlackRubyBotServer::Server` to `SlackRubyBotServer::RealTime::Server`.
14+
3. Replace any `require 'slack-ruby-bot-server/rspec'` with `require 'slack-ruby-bot-server-rtm/rspec'`.
15+
16+
Existing RTM Slack bots will continue working and be listed in the Slack App Directory. On December 4th, 2020 Slack will no longer accept resubmissions from apps that are not using granular permissions. On November 18, 2021 Slack will start delisting apps that have not migrated to use granular permissions. Use [slack-ruby-bot-server-events](https://github.com/slack-ruby/slack-ruby-bot-server-events) to create a Slack bot with granular permissions. See [migration](https://api.slack.com/authentication/migration) for more details.
17+
418
### Upgrading to >= 0.11.0
519

620
#### Removed Legacy Migrations

images/slackbotserver.gif

-600 KB
Binary file not shown.

images/slackbutton.gif

-103 KB
Binary file not shown.

lib/slack-ruby-bot-server.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
require 'async/websocket'
1+
require 'async'
22

3+
require 'slack-ruby-client'
34
require 'grape-swagger'
4-
require 'slack-ruby-bot'
55

6+
require_relative 'slack-ruby-bot-server/loggable'
67
require_relative 'slack-ruby-bot-server/service'
7-
require_relative 'slack-ruby-bot-server/server'
88
require_relative 'slack-ruby-bot-server/config'
99
require_relative 'slack-ruby-bot-server/ext'
1010
require_relative 'slack-ruby-bot-server/version'

lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ class TeamsEndpoint < Grape::API
4646
code: params[:code]
4747
)
4848

49-
token = rc['bot']['bot_access_token']
50-
bot_user_id = rc['bot']['bot_user_id']
51-
user_id = rc['user_id']
5249
access_token = rc['access_token']
50+
user_id = rc['user_id']
51+
52+
bot = rc['bot']
53+
54+
token = bot ? bot['bot_access_token'] : access_token
55+
bot_user_id = bot['bot_user_id'] if bot
56+
5357
team = Team.where(token: token).first
5458
team ||= Team.where(team_id: rc['team_id']).first
5559

0 commit comments

Comments
 (0)