slack-ruby-bot-server 0.12.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop_todo.yml +4 -9
  4. data/.travis.yml +0 -1
  5. data/CHANGELOG.md +22 -0
  6. data/DEBUGGING.md +0 -20
  7. data/Dangerfile +1 -0
  8. data/Gemfile +1 -0
  9. data/README.md +69 -74
  10. data/UPGRADING.md +43 -0
  11. data/lib/slack-ruby-bot-server.rb +3 -3
  12. data/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb +44 -9
  13. data/lib/slack-ruby-bot-server/api/middleware.rb +4 -5
  14. data/lib/slack-ruby-bot-server/app.rb +2 -14
  15. data/lib/slack-ruby-bot-server/config.rb +32 -2
  16. data/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +2 -0
  17. data/lib/slack-ruby-bot-server/ext.rb +1 -1
  18. data/lib/slack-ruby-bot-server/ext/mongoid/slack-ruby-bot/commands/base.rb +2 -2
  19. data/lib/slack-ruby-bot-server/loggable.rb +25 -0
  20. data/lib/slack-ruby-bot-server/models/team/methods.rb +21 -1
  21. data/lib/slack-ruby-bot-server/models/team/mongoid.rb +2 -0
  22. data/lib/slack-ruby-bot-server/rspec.rb +0 -1
  23. data/lib/slack-ruby-bot-server/service.rb +7 -33
  24. data/lib/slack-ruby-bot-server/version.rb +1 -1
  25. data/public/index.html.erb +1 -1
  26. data/slack-ruby-bot-server.gemspec +2 -2
  27. data/tasks/db.rake +0 -36
  28. metadata +16 -55
  29. data/images/slackbotserver.gif +0 -0
  30. data/images/slackbutton.gif +0 -0
  31. data/lib/slack-ruby-bot-server/ext/activerecord/slack-ruby-bot/commands/base.rb +0 -18
  32. data/lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb +0 -2
  33. data/lib/slack-ruby-bot-server/ext/slack-ruby-bot/client.rb +0 -12
  34. data/lib/slack-ruby-bot-server/server.rb +0 -31
  35. data/sample_apps/README.md +0 -11
  36. data/sample_apps/sample_app_activerecord/.rspec +0 -3
  37. data/sample_apps/sample_app_activerecord/.standalone_migrations +0 -3
  38. data/sample_apps/sample_app_activerecord/Gemfile +0 -24
  39. data/sample_apps/sample_app_activerecord/Procfile +0 -1
  40. data/sample_apps/sample_app_activerecord/README.md +0 -12
  41. data/sample_apps/sample_app_activerecord/Rakefile +0 -14
  42. data/sample_apps/sample_app_activerecord/commands.rb +0 -2
  43. data/sample_apps/sample_app_activerecord/commands/help.rb +0 -19
  44. data/sample_apps/sample_app_activerecord/commands/whoami.rb +0 -6
  45. data/sample_apps/sample_app_activerecord/config.ru +0 -22
  46. data/sample_apps/sample_app_activerecord/config/newrelic.yml +0 -217
  47. data/sample_apps/sample_app_activerecord/config/postgresql.yml +0 -17
  48. data/sample_apps/sample_app_activerecord/db/migrate/20170307164946_create_teams_table.rb +0 -12
  49. data/sample_apps/sample_app_activerecord/db/migrate/20190323181453_add_activated_fields.rb +0 -7
  50. data/sample_apps/sample_app_activerecord/db/schema.rb +0 -29
  51. data/sample_apps/sample_app_activerecord/spec/api/root_spec.rb +0 -16
  52. data/sample_apps/sample_app_activerecord/spec/commands/help_spec.rb +0 -14
  53. data/sample_apps/sample_app_activerecord/spec/commands/whoami_spec.rb +0 -14
  54. data/sample_apps/sample_app_activerecord/spec/spec_helper.rb +0 -19
  55. data/sample_apps/sample_app_mongoid/.rspec +0 -3
  56. data/sample_apps/sample_app_mongoid/Gemfile +0 -20
  57. data/sample_apps/sample_app_mongoid/Procfile +0 -1
  58. data/sample_apps/sample_app_mongoid/README.md +0 -14
  59. data/sample_apps/sample_app_mongoid/Rakefile +0 -10
  60. data/sample_apps/sample_app_mongoid/commands.rb +0 -2
  61. data/sample_apps/sample_app_mongoid/commands/help.rb +0 -19
  62. data/sample_apps/sample_app_mongoid/commands/whoami.rb +0 -6
  63. data/sample_apps/sample_app_mongoid/config.ru +0 -14
  64. data/sample_apps/sample_app_mongoid/config/mongoid.yml +0 -27
  65. data/sample_apps/sample_app_mongoid/config/newrelic.yml +0 -217
  66. data/sample_apps/sample_app_mongoid/spec/api/root_spec.rb +0 -16
  67. data/sample_apps/sample_app_mongoid/spec/commands/help_spec.rb +0 -14
  68. data/sample_apps/sample_app_mongoid/spec/commands/whoami_spec.rb +0 -14
  69. data/sample_apps/sample_app_mongoid/spec/spec_helper.rb +0 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd95fc2a7806d66e49468401dec6f7598ba538d690f79c39a80723b0abd4afc8
4
- data.tar.gz: 57d542b3799078c57e29914395d20c92735c9f70025471f7ccb3e422a965c437
3
+ metadata.gz: 4f81d3aefa974e927adeaa66b98092695463ff4f4dc8d1c0294a0efb7c824c01
4
+ data.tar.gz: 17731a13443a9c2cdc4e8bfa0183501c07395c670a04b4fa45efdaec503cd30e
5
5
  SHA512:
6
- metadata.gz: 7725ccdcda4bc790c8f4a28de21797ee8aaefe698e937be0fb2bc665a487e28f3174960db32d772b75ea0137677a1049dc2ef05b8ea49f192dcd40d1b58502f0
7
- data.tar.gz: a661c32f29d0685428510700dfed5f3a76614adf924d972cad8bae14570422ab2d131f7db41a28ad378f0c5a0e8c283943ab674e4c70df6d80ed826e12bd126f
6
+ metadata.gz: d2f99691dda8529a626456196f0359fd0a16e5ee28c831e23f7fc061038fb95b34b9bb0dc10cba562490c2d2b8b84bf23530d6d2e489e7747100e360d7fef8f9
7
+ data.tar.gz: ecb8a80e8c191111daab48367d7f9d2afa0a9ee0618d9ad339484af25e6a80cf36af9c047295a626b43403e94514343b19a383b7ecdb97d3875133fec8ab669f
@@ -0,0 +1 @@
1
+ github: [dblock]
@@ -1,20 +1,18 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2020-06-16 16:54:39 -0400 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
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 3
9
+ # Offense count: 1
10
10
  # Cop supports --auto-correct.
11
11
  # Configuration parameters: EnforcedStyle.
12
12
  # SupportedStyles: squiggly, active_support, powerpack, unindent
13
13
  Layout/HeredocIndentation:
14
14
  Exclude:
15
15
  - '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'
18
16
 
19
17
  # Offense count: 1
20
18
  Lint/AmbiguousOperator:
@@ -27,22 +25,19 @@ Lint/NonDeterministicRequireOrder:
27
25
  Exclude:
28
26
  - 'spec/spec_helper.rb'
29
27
 
30
- # Offense count: 2
28
+ # Offense count: 1
31
29
  # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
32
30
  # 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
33
31
  Naming/FileName:
34
32
  Exclude:
35
33
  - 'lib/slack-ruby-bot-server.rb'
36
- - 'lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb'
37
34
 
38
- # Offense count: 3
35
+ # Offense count: 1
39
36
  # Configuration parameters: ForbiddenDelimiters.
40
37
  # ForbiddenDelimiters: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
41
38
  Naming/HeredocDelimiterNaming:
42
39
  Exclude:
43
40
  - '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'
46
41
 
47
42
  # Offense count: 3
48
43
  # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
@@ -31,4 +31,3 @@ before_install:
31
31
 
32
32
  script:
33
33
  - bundle exec rake
34
- - BUNDLE_GEMFILE=Gemfile && cd sample_apps/sample_app_$DATABASE_ADAPTER && bundle && bundle exec rake
@@ -1,5 +1,27 @@
1
1
  ### Changelog
2
2
 
3
+ #### 1.2.0 (2020/11/27)
4
+
5
+ * [#133](https://github.com/slack-ruby/slack-ruby-bot-server/pull/133): Added `Team#oauth_version` and `#scope` - [@dblock](https://github.com/dblock).
6
+
7
+ #### 1.1.0 (2020/11/17)
8
+
9
+ * [#132](https://github.com/slack-ruby/slack-ruby-bot-server/pull/132): Added support for OAuth v2 - [@dblock](https://github.com/dblock).
10
+
11
+ #### 1.0.0 (2020/11/15)
12
+
13
+ * [#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).
14
+
15
+ #### 0.12.3 (2020/11/14)
16
+
17
+ * [#123](https://github.com/slack-ruby/slack-ruby-bot-server/pull/123): Reactivate a team that was disabled from Slack - [@dblock](https://github.com/dblock).
18
+ * [#127](https://github.com/slack-ruby/slack-ruby-bot-server/pull/127): Added a Rails bot example link - [@CrazyOptimist](https://github.com/CrazyOptimist).
19
+
20
+ #### 0.12.2 (2020/7/26)
21
+
22
+ * [#120](https://github.com/slack-ruby/slack-ruby-bot-server/pull/120): Removed GIFs - [@dblock](https://github.com/dblock).
23
+ * [#121](https://github.com/slack-ruby/slack-ruby-bot-server/pull/121): Added TOC - [@dblock](https://github.com/dblock).
24
+
3
25
  #### 0.12.1 (2020/5/16)
4
26
 
5
27
  * [#118](https://github.com/slack-ruby/slack-ruby-bot-server/pull/118): Do not fail to start on errors in `Team#purge!` - [@dblock](https://github.com/dblock).
@@ -3,23 +3,3 @@
3
3
  ### Locally
4
4
 
5
5
  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
- ```
data/Dangerfile CHANGED
@@ -1 +1,2 @@
1
1
  danger.import_dangerfile(gem: 'slack-ruby-danger')
2
+ toc.check!
data/Gemfile CHANGED
@@ -38,5 +38,6 @@ group :development, :test do
38
38
  end
39
39
 
40
40
  group :test do
41
+ gem 'danger-toc', '~> 0.2.0', require: false
41
42
  gem 'slack-ruby-danger', '~> 0.1.0', require: false
42
43
  end
data/README.md CHANGED
@@ -7,33 +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
- 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).
10
+ ## Table of Contents
11
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).
12
+ - [What is this?](#what-is-this)
13
+ - [Stable Release](#stable-release)
14
+ - [Make Your Own](#make-your-own)
15
+ - [Usage](#usage)
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)
30
+ - [Sample Bots Using Slack Ruby Bot Server](#sample-bots-using-slack-ruby-bot-server)
31
+ - [Slack Bots with Granular Permissions](#slack-bots-with-granular-permissions)
32
+ - [Legacy Slack Bots](#legacy-slack-bots)
33
+ - [Copyright & License](#copyright--license)
13
34
 
14
- ### What is this?
35
+ ## What is this?
15
36
 
16
- 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.
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.
17
38
 
18
- ### Stable Release
39
+ ## Stable Release
19
40
 
20
- 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.0](https://github.com/slack-ruby/slack-ruby-bot-server/blob/v0.12.0/README.md) unless you're integrating with HEAD. 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, v1.2.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
21
42
 
22
- ### Try Me
43
+ ## Make Your Own
23
44
 
24
- 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_.
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.
25
46
 
26
- ![](images/slackbutton.gif)
47
+ ## Usage
27
48
 
28
- 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".
49
+ ### Storage
29
50
 
30
- ![](images/slackbotserver.gif)
51
+ A database is required to store teams.
31
52
 
32
- ### Run Your Own
33
-
34
- 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.
35
-
36
- ### MongoDB
53
+ #### MongoDB
37
54
 
38
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.
39
56
 
@@ -44,9 +61,7 @@ gem 'mongoid-scroll'
44
61
  gem 'slack-ruby-bot-server'
45
62
  ```
46
63
 
47
- See the [sample app using Mongoid](sample_apps/sample_app_mongoid) for more information.
48
-
49
- ### ActiveRecord
64
+ #### ActiveRecord
50
65
 
51
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.
52
67
 
@@ -58,52 +73,49 @@ gem 'otr-activerecord'
58
73
  gem 'cursor_pagination'
59
74
  ```
60
75
 
61
- See the [sample app using ActiveRecord](sample_apps/sample_app_activerecord) for more information.
76
+ ### OAuth Version and Scopes
62
77
 
63
- ### Usage
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.
64
79
 
65
- 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).
80
+ ```ruby
81
+ SlackRubyBotServer.configure do |config|
82
+ config.oauth_version = :v2
83
+ config.oauth_scope = ['channels:read', 'chat:write']
84
+ end
85
+ ```
66
86
 
67
- ![](images/create-app.png)
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.
68
88
 
69
- 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.
89
+ ### Slack App
70
90
 
71
- Within your application, edit your `.env` file and add `SLACK_CLIENT_ID=...` and `SLACK_CLIENT_SECRET=...` in it.
91
+ Create a new Slack App [here](https://api.slack.com/applications/new).
72
92
 
73
- 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.
93
+ ![](images/create-app.png)
74
94
 
75
- ### OAuth Code Grant
95
+ Follow Slack's instructions, note the app client ID and secret, give the bot a default name, etc.
76
96
 
77
- 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).
97
+ Within your application, edit your `.env` file and add `SLACK_CLIENT_ID=...` and `SLACK_CLIENT_SECRET=...` in it.
78
98
 
79
- The button itself contains a link that looks like this:
99
+ Run `bundle install` and `foreman start` to boot the app.
80
100
 
81
101
  ```
82
- https://slack.com/oauth/authorize?scope=bot&client_id=<%= ENV['SLACK_CLIENT_ID'] %>
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
83
105
  ```
84
106
 
85
- 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:
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.
86
108
 
87
- ```ruby
88
- # Instantiate a web client
89
- client = Slack::Web::Client.new
90
-
91
- # Request a token using the temporary code
92
- rc = client.oauth_access(
93
- client_id: ENV['SLACK_CLIENT_ID'],
94
- client_secret: ENV['SLACK_CLIENT_SECRET'],
95
- code: params[:code]
96
- )
97
-
98
- # Pluck the token from the response
99
- token = rc['bot']['bot_access_token']
109
+ ```
110
+ $ ngrok http 5000
111
+ Forwarding https://ddfd97f80615.ngrok.io -> http://localhost:5000
100
112
  ```
101
113
 
102
- The token is stored in persistent storage and used each time a Slack client is instantiated for the specific 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.
103
115
 
104
116
  ### API
105
117
 
106
- 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.
118
+ 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.
107
119
 
108
120
  #### App
109
121
 
@@ -178,7 +190,7 @@ The [Add to Slack button](https://api.slack.com/docs/slack-button) also allows f
178
190
  auth = OpenSSL::HMAC.hexdigest("SHA256", "key", "data")
179
191
  ```
180
192
  ```html
181
- <a href="https://slack.com/oauth/authorize?scope=bot&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>
182
194
  ```
183
195
  ```ruby
184
196
  instance = SlackRubyBotServer::Service.instance
@@ -222,28 +234,10 @@ end
222
234
 
223
235
  A number of extensions use service manager callbacks and service timers to implement useful functionality.
224
236
 
237
+ * [slack-ruby-bot-server-events](https://github.com/slack-ruby/slack-ruby-bot-server-events): Easily handle Slack slash commands, interactive buttons and events.
225
238
  * [slack-ruby-bot-server-mailchimp](https://github.com/slack-ruby/slack-ruby-bot-server-mailchimp): Subscribes new bot users to a Mailchimp mailing list.
226
239
  * [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.
227
-
228
- #### Server Class
229
-
230
- You can override the server class to handle additional events, and configure the service to use it.
231
-
232
- ```ruby
233
- class MyServer < SlackRubyBotServer::Server
234
- on :hello do |client, data|
235
- # connected to Slack
236
- end
237
-
238
- on :channel_joined do |client, data|
239
- # the bot joined a channel in data.channel['id']
240
- end
241
- end
242
-
243
- SlackRubyBotServer.configure do |config|
244
- config.server_class = MyServer
245
- end
246
- ```
240
+ * [slack-ruby-bot-server-rtm](https://github.com/slack-ruby/slack-ruby-bot-server-rtm): Create RTM Slack bots.
247
241
 
248
242
  #### Service Class
249
243
 
@@ -278,15 +272,16 @@ end
278
272
 
279
273
  ### Access Tokens
280
274
 
281
- 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.
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), along with `oauth_version` and `oauth_scope`. 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.
276
+
277
+ ## Sample Bots Using Slack Ruby Bot Server
282
278
 
283
- 1) Configure your app to require additional scopes in Slack API under _OAuth_, _Permissions_
284
- 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=...`
285
- 3) The access token with the requested scopes will be stored as `activated_user_access_token`.
279
+ ### Slack Bots with Granular Permissions
286
280
 
287
- You can see a sample implementation in [slack-sup#3a497b](https://github.com/dblock/slack-sup/commit/3a497b436d25d3a7738562655cda64b180ae0096).
281
+ * [slack-ruby-bot-server-events-sample](https://github.com/slack-ruby/slack-ruby-bot-server-events-sample), a generic sample
282
+ * [slack-rails-bot-starter](https://github.com/CrazyOptimist/slack-rails-bot-starter), an all-in-one Rails starter kit
288
283
 
289
- ### Example Bots Using Slack Ruby Bot Server
284
+ ### Legacy Slack Bots
290
285
 
291
286
  * [slack-ruby-bot-server-sample](https://github.com/slack-ruby/slack-ruby-bot-server-sample), a generic sample
292
287
  * [slack-sup](https://github.com/dblock/slack-sup), see [sup.playplay.io](https://sup.playplay.io)
@@ -297,7 +292,7 @@ You can see a sample implementation in [slack-sup#3a497b](https://github.com/dbl
297
292
  * [slack-strava](https://github.com/dblock/slack-strava), see [slava.playplay.io](https://slava.playplay.io)
298
293
  * [slack-arena](https://github.com/dblock/slack-arena), see [arena.playplay.io](https://arena.playplay.io)
299
294
 
300
- ### Copyright & License
295
+ ## Copyright & License
301
296
 
302
297
  Copyright [Daniel Doubrovkine](http://code.dblock.org) and Contributors, 2015-2020
303
298
 
@@ -1,6 +1,49 @@
1
1
  Upgrading Slack-Ruby-Bot-Server
2
2
  ===============================
3
3
 
4
+ ### Upgrading to >= 1.2.0
5
+
6
+ #### New Team Fields
7
+
8
+ The following fields have been added to `Team`.
9
+
10
+ * `oauth_scope`: Slack OAuth scope
11
+ * `oauth_version`: Slack OAuth version used
12
+
13
+ No action is required for Mongoid.
14
+
15
+ If you're using ActiveRecord, create a migration to add these fields.
16
+
17
+ ```ruby
18
+ class AddOauthFields < ActiveRecord::Migration[5.0]
19
+ def change
20
+ add_column :teams, :oauth_scope, :string
21
+ add_column :teams, :oauth_version, :string, default: 'v1', null: false
22
+ end
23
+ end
24
+ ```
25
+
26
+ ### Upgrading to >= 1.1.0
27
+
28
+ #### Extracted RealTime (Legacy) Support
29
+
30
+ 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).
31
+
32
+ To upgrade an existing classic Slack app that uses slack-ruby-bot-server do the following.
33
+
34
+ 1. Add `slack-ruby-bot-server-rtm` as an additional dependency.
35
+ 2. Replace any reference to `SlackRubyBotServer::Server` to `SlackRubyBotServer::RealTime::Server`.
36
+ 3. Replace any `require 'slack-ruby-bot-server/rspec'` with `require 'slack-ruby-bot-server-rtm/rspec'`.
37
+ 4. Use Slack OAuth 1.0 and configure scopes.
38
+ ```ruby
39
+ SlackRubyBotServer.configure do |config|
40
+ config.oauth_version = :v1
41
+ config.oauth_scope = ['bot']
42
+ end
43
+ ```
44
+
45
+ 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.
46
+
4
47
  ### Upgrading to >= 0.11.0
5
48
 
6
49
  #### Removed Legacy Migrations
@@ -1,10 +1,10 @@
1
- require 'async/websocket'
1
+ require 'async'
2
2
 
3
+ require 'slack-ruby-client'
3
4
  require 'grape-swagger'
4
- require 'slack-ruby-bot'
5
5
 
6
+ require_relative 'slack-ruby-bot-server/loggable'
6
7
  require_relative 'slack-ruby-bot-server/service'
7
- require_relative 'slack-ruby-bot-server/server'
8
8
  require_relative 'slack-ruby-bot-server/config'
9
9
  require_relative 'slack-ruby-bot-server/ext'
10
10
  require_relative 'slack-ruby-bot-server/version'
@@ -40,33 +40,68 @@ 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
+
49
+ rc = client.send(SlackRubyBotServer.config.oauth_access_method, options)
50
+
51
+ token = nil
52
+ access_token = nil
53
+ user_id = nil
54
+ bot_user_id = nil
55
+ team_id = nil
56
+ team_name = nil
57
+ oauth_scope = nil
58
+ oauth_version = SlackRubyBotServer::Config.oauth_version
59
+
60
+ case oauth_version
61
+ when :v2
62
+ access_token = rc.access_token
63
+ token = rc.access_token
64
+ user_id = rc.authed_user&.id
65
+ bot_user_id = rc.bot_user_id
66
+ team_id = rc.team&.id
67
+ team_name = rc.team&.name
68
+ oauth_scope = rc.scope
69
+ when :v1
70
+ access_token = rc.access_token
71
+ bot = rc.bot if rc.key?(:bot)
72
+ token = bot ? bot.bot_access_token : access_token
73
+ user_id = rc.user_id
74
+ bot_user_id = bot ? bot.bot_user_id : nil
75
+ team_id = rc.team_id
76
+ team_name = rc.team_name
77
+ oauth_scope = rc.scope
78
+ end
48
79
 
49
- token = rc['bot']['bot_access_token']
50
- bot_user_id = rc['bot']['bot_user_id']
51
- user_id = rc['user_id']
52
- access_token = rc['access_token']
53
80
  team = Team.where(token: token).first
54
- team ||= Team.where(team_id: rc['team_id']).first
81
+ team ||= Team.where(team_id: team_id, oauth_version: oauth_version).first
82
+ team ||= Team.where(team_id: team_id).first
55
83
 
56
84
  if team
85
+ team.ping_if_active!
86
+
57
87
  team.update_attributes!(
88
+ oauth_version: oauth_version,
89
+ oauth_scope: oauth_scope,
58
90
  activated_user_id: user_id,
59
91
  activated_user_access_token: access_token,
60
92
  bot_user_id: bot_user_id
61
93
  )
94
+
62
95
  raise "Team #{team.name} is already registered." if team.active?
63
96
 
64
97
  team.activate!(token)
65
98
  else
66
99
  team = Team.create!(
67
100
  token: token,
68
- team_id: rc['team_id'],
69
- name: rc['team_name'],
101
+ oauth_version: oauth_version,
102
+ oauth_scope: oauth_scope,
103
+ team_id: team_id,
104
+ name: team_name,
70
105
  activated_user_id: user_id,
71
106
  activated_user_access_token: access_token,
72
107
  bot_user_id: bot_user_id