slack-ruby-bot-server 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3999b9e5a783666fcf1cebfe6d07d50e0228d7a1e3c17ca52217c7677705a1c2
4
- data.tar.gz: 6294712aae7ca330fa3c13764e369084ed7f097772e22b54e5a8c1964ec2e130
3
+ metadata.gz: d96988aebe8c1dadd75c95e086ad63f5aaa81a8c93ecb1a92ae2f6a1908f6b6d
4
+ data.tar.gz: a1519cf75e0afadc610fc92b975613d7817c5a8cf243f849c6ecb9587e504df0
5
5
  SHA512:
6
- metadata.gz: 17c1e6474f1a45c964d67210d96669dc4bbb5910c3571d1720916f9a13b469741d08462e6feb57185a9745ffb373164f68c88f91bbf71691313215a23264a0a5
7
- data.tar.gz: 43c5fde5d25f993be6da99758303026ff2870b26e50435cda7bc10218ae0fea1c7a9ab321d946f62485fc59f37da845ee425804327b1a5c7a4c2575d1f9ccfd3
6
+ metadata.gz: e52881e9b3faa4754a4c582eb9269a6b9eacf0f3f80f4dc20d7332e0d9304d2580b47185409ace983422f6e800bd2ae9bed00d3829eb457c4299eee5294630c3
7
+ data.tar.gz: 91a903e372af38353a914d61f04d1e7016e1e25edf3f9b41afeee523279c1b5c29fefb23ec8d1acc1bb6fdaaf8b6cd0ef4d1c6a2b2f1ac9707ac036a22f4881e
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2020-11-15 11:38:22 -0500 using RuboCop version 0.81.0.
3
+ # on 2020-11-16 09:56:54 -0500 using RuboCop version 0.81.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -1,5 +1,9 @@
1
1
  ### Changelog
2
2
 
3
+ #### 1.1.0 (2020/11/17)
4
+
5
+ * [#132](https://github.com/slack-ruby/slack-ruby-bot-server/pull/132): Add support for OAuth v2 - [@dblock](https://github.com/dblock).
6
+
3
7
  #### 1.0.0 (2020/11/15)
4
8
 
5
9
  * [#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).
data/README.md CHANGED
@@ -7,46 +7,50 @@ Slack Ruby Bot Server
7
7
 
8
8
  Build a complete Slack bot service with Slack button integration, in Ruby.
9
9
 
10
- # Table of Contents
10
+ ## Table of Contents
11
11
 
12
12
  - [What is this?](#what-is-this)
13
13
  - [Stable Release](#stable-release)
14
14
  - [Make Your Own](#make-your-own)
15
- - [Storage](#storage)
16
- - [MongoDB](#mongodb)
17
- - [ActiveRecord](#activerecord)
18
15
  - [Usage](#usage)
19
- - [API](#api)
20
- - [App](#app)
21
- - [Service Manager](#service-manager)
22
- - [Lifecycle Callbacks](#lifecycle-callbacks)
23
- - [Service Timers](#service-timers)
24
- - [Extensions](#extensions)
25
- - [Service Class](#service-class)
26
- - [HTML Templates](#html-templates)
27
- - [Access Tokens](#access-tokens)
16
+ - [Storage](#storage)
17
+ - [MongoDB](#mongodb)
18
+ - [ActiveRecord](#activerecord)
19
+ - [OAuth Version and Scopes](#oauth-version-and-scopes)
20
+ - [Slack App](#slack-app)
21
+ - [API](#api)
22
+ - [App](#app)
23
+ - [Service Manager](#service-manager)
24
+ - [Lifecycle Callbacks](#lifecycle-callbacks)
25
+ - [Service Timers](#service-timers)
26
+ - [Extensions](#extensions)
27
+ - [Service Class](#service-class)
28
+ - [HTML Templates](#html-templates)
29
+ - [Access Tokens](#access-tokens)
28
30
  - [Sample Bots Using Slack Ruby Bot Server](#sample-bots-using-slack-ruby-bot-server)
29
31
  - [Slack Bots with Granular Permissions](#slack-bots-with-granular-permissions)
30
32
  - [Legacy Slack Bots](#legacy-slack-bots)
31
33
  - [Copyright & License](#copyright--license)
32
34
 
33
- ### What is this?
35
+ ## What is this?
34
36
 
35
37
  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.
36
38
 
37
- ### Stable Release
39
+ ## Stable Release
38
40
 
39
- You're reading the documentation for the **stable** release of slack-ruby-bot-server, v1.0.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
41
+ You're reading the documentation for the **stable** release of slack-ruby-bot-server. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
40
42
 
41
- ### Make Your Own
43
+ ## Make Your Own
42
44
 
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.
45
+ This library alone will only register a new bot, but will not include any bot functionality. To make something useful, 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.
46
+
47
+ ## Usage
44
48
 
45
49
  ### Storage
46
50
 
47
51
  A database is required to store teams.
48
52
 
49
- ### MongoDB
53
+ #### MongoDB
50
54
 
51
55
  Use MongoDB with [Mongoid](https://github.com/mongodb/mongoid) as ODM. Configure the database connection in `mongoid.yml`. Add the `mongoid` gem in your Gemfile.
52
56
 
@@ -57,7 +61,7 @@ gem 'mongoid-scroll'
57
61
  gem 'slack-ruby-bot-server'
58
62
  ```
59
63
 
60
- ### ActiveRecord
64
+ #### ActiveRecord
61
65
 
62
66
  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.
63
67
 
@@ -69,27 +73,45 @@ gem 'otr-activerecord'
69
73
  gem 'cursor_pagination'
70
74
  ```
71
75
 
72
- ### Usage
76
+ ### OAuth Version and Scopes
77
+
78
+ Configure your app's [OAuth version](https://api.slack.com/authentication/oauth-v2) and [scopes](https://api.slack.com/legacy/oauth-scopes) as needed by your application.
79
+
80
+ ```ruby
81
+ SlackRubyBotServer.configure do |config|
82
+ config.oauth_version = :v2
83
+ config.oauth_scope = ['channels:read', 'chat:write']
84
+ end
85
+ ```
86
+
87
+ 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.
88
+
89
+ ### Slack App
73
90
 
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).
91
+ Create a new Slack App [here](https://api.slack.com/applications/new).
75
92
 
76
93
  ![](images/create-app.png)
77
94
 
78
- 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.
95
+ Follow Slack's instructions, note the app client ID and secret, give the bot a default name, etc.
79
96
 
80
97
  Within your application, edit your `.env` file and add `SLACK_CLIENT_ID=...` and `SLACK_CLIENT_SECRET=...` in it.
81
98
 
82
- Configure your app's [OAuth scopes](https://api.slack.com/legacy/oauth-scopes) as needed by your application.
99
+ Run `bundle install` and `foreman start` to boot the app.
83
100
 
84
- ```ruby
85
- SlackRubyBotServer.configure do |config|
86
- config.oauth_scope = ['channels:read', 'chat:write:user']
87
- end
101
+ ```
102
+ $ foreman start
103
+ 07:44:47 web.1 | started with pid 59258
104
+ 07:44:50 web.1 | * Listening on tcp://0.0.0.0:5000
88
105
  ```
89
106
 
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.
107
+ Set the redirect URL in "OAuth & Permissions" be the location of your app. Since you cannot receive notifications on localhost from Slack use a public tunneling service such as [ngrok](https://ngrok.com/) to expose local port 9292 for testing.
108
+
109
+ ```
110
+ $ ngrok http 5000
111
+ Forwarding https://ddfd97f80615.ngrok.io -> http://localhost:5000
112
+ ```
91
113
 
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.
114
+ Navigate to either [localhost:9292](http://localhost:9292) or the ngrok URL above. You should see an "Add to Slack" button. Use it to install the app into your own Slack team.
93
115
 
94
116
  ### API
95
117
 
@@ -168,7 +190,7 @@ The [Add to Slack button](https://api.slack.com/docs/slack-button) also allows f
168
190
  auth = OpenSSL::HMAC.hexdigest("SHA256", "key", "data")
169
191
  ```
170
192
  ```html
171
- <a href="https://slack.com/oauth/authorize?scope=<%= SlackRubyBotServer::Config.oauth_scope_s %>&client_id=<%= ENV['SLACK_CLIENT_ID'] %>&state=#{auth)"> ... </a>
193
+ <a href="<%= SlackRubyBotServer::Config.oauth_authorize_url %>?scope=<%= SlackRubyBotServer::Config.oauth_scope_s %>&client_id=<%= ENV['SLACK_CLIENT_ID'] %>&state=#{auth)"> ... </a>
172
194
  ```
173
195
  ```ruby
174
196
  instance = SlackRubyBotServer::Service.instance
@@ -252,14 +274,14 @@ end
252
274
 
253
275
  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
276
 
255
- ### Sample Bots Using Slack Ruby Bot Server
277
+ ## Sample Bots Using Slack Ruby Bot Server
256
278
 
257
- #### Slack Bots with Granular Permissions
279
+ ### Slack Bots with Granular Permissions
258
280
 
259
281
  * [slack-ruby-bot-server-events-sample](https://github.com/slack-ruby/slack-ruby-bot-server-events-sample), a generic sample
260
282
  * [slack-rails-bot-starter](https://github.com/CrazyOptimist/slack-rails-bot-starter), an all-in-one Rails starter kit
261
283
 
262
- #### Legacy Slack Bots
284
+ ### Legacy Slack Bots
263
285
 
264
286
  * [slack-ruby-bot-server-sample](https://github.com/slack-ruby/slack-ruby-bot-server-sample), a generic sample
265
287
  * [slack-sup](https://github.com/dblock/slack-sup), see [sup.playplay.io](https://sup.playplay.io)
@@ -270,7 +292,7 @@ By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) s
270
292
  * [slack-strava](https://github.com/dblock/slack-strava), see [slava.playplay.io](https://slava.playplay.io)
271
293
  * [slack-arena](https://github.com/dblock/slack-arena), see [arena.playplay.io](https://arena.playplay.io)
272
294
 
273
- ### Copyright & License
295
+ ## Copyright & License
274
296
 
275
297
  Copyright [Daniel Doubrovkine](http://code.dblock.org) and Contributors, 2015-2020
276
298
 
@@ -1,7 +1,7 @@
1
1
  Upgrading Slack-Ruby-Bot-Server
2
2
  ===============================
3
3
 
4
- ### Upgrading to >= 1.0.0
4
+ ### Upgrading to >= 1.1.0
5
5
 
6
6
  #### Extracted RealTime (Legacy) Support
7
7
 
@@ -12,6 +12,13 @@ To upgrade an existing classic Slack app that uses slack-ruby-bot-server do the
12
12
  1. Add `slack-ruby-bot-server-rtm` as an additional dependency.
13
13
  2. Replace any reference to `SlackRubyBotServer::Server` to `SlackRubyBotServer::RealTime::Server`.
14
14
  3. Replace any `require 'slack-ruby-bot-server/rspec'` with `require 'slack-ruby-bot-server-rtm/rspec'`.
15
+ 4. Use Slack OAuth 1.0 and configure scopes.
16
+ ```ruby
17
+ SlackRubyBotServer.configure do |config|
18
+ config.oauth_version = :v1
19
+ config.oauth_scope = ['bot']
20
+ end
21
+ ```
15
22
 
16
23
  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
24
 
@@ -40,22 +40,41 @@ module SlackRubyBotServer
40
40
 
41
41
  raise 'Missing SLACK_CLIENT_ID or SLACK_CLIENT_SECRET.' unless ENV.key?('SLACK_CLIENT_ID') && ENV.key?('SLACK_CLIENT_SECRET')
42
42
 
43
- rc = client.oauth_access(
43
+ options = {
44
44
  client_id: ENV['SLACK_CLIENT_ID'],
45
45
  client_secret: ENV['SLACK_CLIENT_SECRET'],
46
46
  code: params[:code]
47
- )
47
+ }
48
48
 
49
- access_token = rc['access_token']
50
- user_id = rc['user_id']
49
+ rc = client.send(SlackRubyBotServer.config.oauth_access_method, options)
51
50
 
52
- bot = rc['bot']
51
+ token = nil
52
+ access_token = nil
53
+ user_id = nil
54
+ bot_user_id = nil
55
+ team_id = nil
56
+ team_name = nil
53
57
 
54
- token = bot ? bot['bot_access_token'] : access_token
55
- bot_user_id = bot['bot_user_id'] if bot
58
+ case SlackRubyBotServer::Config.oauth_version
59
+ when :v2
60
+ access_token = rc.access_token
61
+ token = rc.access_token
62
+ user_id = rc.authed_user&.id
63
+ bot_user_id = rc.bot_user_id
64
+ team_id = rc.team&.id
65
+ team_name = rc.team&.name
66
+ when :v1
67
+ access_token = rc.access_token
68
+ bot = rc.bot if rc.key?(:bot)
69
+ token = bot ? bot.bot_access_token : access_token
70
+ user_id = rc.user_id
71
+ bot_user_id = bot ? bot.bot_user_id : nil
72
+ team_id = rc.team_id
73
+ team_name = rc.team_name
74
+ end
56
75
 
57
76
  team = Team.where(token: token).first
58
- team ||= Team.where(team_id: rc['team_id']).first
77
+ team ||= Team.where(team_id: team_id).first
59
78
 
60
79
  if team
61
80
  team.ping_if_active!
@@ -72,8 +91,8 @@ module SlackRubyBotServer
72
91
  else
73
92
  team = Team.create!(
74
93
  token: token,
75
- team_id: rc['team_id'],
76
- name: rc['team_name'],
94
+ team_id: team_id,
95
+ name: team_name,
77
96
  activated_user_id: user_id,
78
97
  activated_user_access_token: access_token,
79
98
  bot_user_id: bot_user_id
@@ -1,10 +1,11 @@
1
1
  module SlackRubyBotServer
2
2
  class App
3
+ include SlackRubyBotServer::Loggable
4
+
3
5
  def prepare!
4
6
  check_database!
5
7
  init_database!
6
8
  purge_inactive_teams!
7
- configure_global_aliases!
8
9
  end
9
10
 
10
11
  def self.instance
@@ -13,13 +14,6 @@ module SlackRubyBotServer
13
14
 
14
15
  private
15
16
 
16
- def logger
17
- @logger ||= begin
18
- STDOUT.sync = true
19
- Logger.new(STDOUT)
20
- end
21
- end
22
-
23
17
  def check_database!
24
18
  SlackRubyBotServer::DatabaseAdapter.check!
25
19
  end
@@ -31,11 +25,5 @@ module SlackRubyBotServer
31
25
  def purge_inactive_teams!
32
26
  Team.purge!
33
27
  end
34
-
35
- def configure_global_aliases!
36
- SlackRubyBot.configure do |config|
37
- config.aliases = ENV['SLACK_RUBY_BOT_ALIASES'].split(' ') if ENV['SLACK_RUBY_BOT_ALIASES']
38
- end
39
- end
40
28
  end
41
29
  end
@@ -7,11 +7,13 @@ module SlackRubyBotServer
7
7
  attr_accessor :database_adapter
8
8
  attr_accessor :view_paths
9
9
  attr_accessor :oauth_scope
10
+ attr_accessor :oauth_version
10
11
 
11
12
  def reset!
12
13
  self.logger = nil
13
14
  self.service_class = SlackRubyBotServer::Service
14
15
  self.oauth_scope = nil
16
+ self.oauth_version = :v2
15
17
 
16
18
  self.view_paths = [
17
19
  'views',
@@ -28,6 +30,28 @@ module SlackRubyBotServer
28
30
  end
29
31
  end
30
32
 
33
+ def oauth_authorize_url
34
+ case oauth_version
35
+ when :v2
36
+ 'https://slack.com/oauth/v2/authorize'
37
+ when :v1
38
+ 'https://slack.com/oauth/authorize'
39
+ else
40
+ raise ArgumentError, 'Invalid oauth_version, must be one of :v1 or v2.'
41
+ end
42
+ end
43
+
44
+ def oauth_access_method
45
+ case oauth_version
46
+ when :v2
47
+ :oauth_v2_access
48
+ when :v1
49
+ :oauth_access
50
+ else
51
+ raise ArgumentError, 'Invalid oauth_version, must be one of :v1 or v2.'
52
+ end
53
+ end
54
+
31
55
  def oauth_scope_s
32
56
  oauth_scope&.join('+')
33
57
  end
@@ -32,10 +32,18 @@ module Methods
32
32
 
33
33
  def ping!
34
34
  client = Slack::Web::Client.new(token: token)
35
+
35
36
  auth = client.auth_test
37
+
38
+ presence = begin
39
+ client.users_getPresence(user: auth['user_id'])
40
+ rescue Slack::Web::Api::Errors::MissingScope
41
+ nil
42
+ end
43
+
36
44
  {
37
45
  auth: auth,
38
- presence: client.users_getPresence(user: auth['user_id'])
46
+ presence: presence
39
47
  }
40
48
  end
41
49
 
@@ -1,3 +1,3 @@
1
1
  module SlackRubyBotServer
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -16,7 +16,7 @@
16
16
  </p>
17
17
  <p id='messages' />
18
18
  <p id='register'>
19
- <a href="https://slack.com/oauth/authorize?scope=<%= SlackRubyBotServer::Config.oauth_scope_s %>&client_id=<%= ENV['SLACK_CLIENT_ID'] %>"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x"></a>
19
+ <a href="<%= SlackRubyBotServer::Config.oauth_authorize_url %>?scope=<%= SlackRubyBotServer::Config.oauth_scope_s %>&client_id=<%= ENV['SLACK_CLIENT_ID'] %>"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x"></a>
20
20
  </p>
21
21
  <p id='active_teams_count'>&nbsp;</p>
22
22
  <p>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-ruby-bot-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-15 00:00:00.000000000 Z
11
+ date: 2020-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async