slack-ruby-bot-server 0.11.0 → 0.12.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop.yml +1 -1
  4. data/.rubocop_todo.yml +44 -7
  5. data/.travis.yml +7 -5
  6. data/CHANGELOG.md +49 -22
  7. data/Dangerfile +1 -0
  8. data/Gemfile +3 -2
  9. data/LICENSE +1 -1
  10. data/README.md +122 -16
  11. data/images/create-app.png +0 -0
  12. data/lib/slack-ruby-bot-server.rb +9 -11
  13. data/lib/slack-ruby-bot-server/api.rb +4 -4
  14. data/lib/slack-ruby-bot-server/api/endpoints.rb +3 -3
  15. data/lib/slack-ruby-bot-server/api/endpoints/teams_endpoint.rb +4 -0
  16. data/lib/slack-ruby-bot-server/api/helpers.rb +4 -4
  17. data/lib/slack-ruby-bot-server/api/helpers/cursor_helpers.rb +2 -0
  18. data/lib/slack-ruby-bot-server/api/helpers/sort_helpers.rb +1 -0
  19. data/lib/slack-ruby-bot-server/api/middleware.rb +1 -5
  20. data/lib/slack-ruby-bot-server/api/presenters.rb +5 -5
  21. data/lib/slack-ruby-bot-server/api/presenters/status_presenter.rb +1 -0
  22. data/lib/slack-ruby-bot-server/config.rb +8 -0
  23. data/lib/slack-ruby-bot-server/config/database_adapters/activerecord.rb +2 -2
  24. data/lib/slack-ruby-bot-server/config/database_adapters/mongoid.rb +4 -2
  25. data/lib/slack-ruby-bot-server/ext/activerecord/slack-ruby-bot/commands/base.rb +1 -1
  26. data/lib/slack-ruby-bot-server/ext/mongoid/slack-ruby-bot/commands/base.rb +2 -2
  27. data/lib/slack-ruby-bot-server/models/team/activerecord.rb +8 -5
  28. data/lib/slack-ruby-bot-server/models/team/methods.rb +12 -0
  29. data/lib/slack-ruby-bot-server/models/team/mongoid.rb +8 -4
  30. data/lib/slack-ruby-bot-server/server.rb +1 -0
  31. data/lib/slack-ruby-bot-server/service.rb +45 -3
  32. data/lib/slack-ruby-bot-server/version.rb +1 -1
  33. data/public/index.html.erb +1 -0
  34. data/public/scripts/messages.js +33 -0
  35. data/public/scripts/register.js +0 -31
  36. data/sample_apps/sample_app_activerecord/README.md +1 -0
  37. data/sample_apps/sample_app_activerecord/commands/help.rb +1 -1
  38. data/sample_apps/sample_app_activerecord/spec/commands/help_spec.rb +1 -1
  39. data/sample_apps/sample_app_mongoid/commands/help.rb +1 -1
  40. data/sample_apps/sample_app_mongoid/spec/commands/help_spec.rb +1 -1
  41. data/slack-ruby-bot-server.gemspec +2 -2
  42. metadata +14 -13
  43. data/images/new.png +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2e8d227e46164c37274bc2fc72c30074f7496835c08a20c5ed6da960336fdf9
4
- data.tar.gz: 35a54416b198e2fc372c1613922b28fd1a9f7a5bac39ab0229c3ea7afbd3ed82
3
+ metadata.gz: 5f3d74ad11628ec28dafca1d0ea2ac2b9119eb2d44aa5d8a9e55bbd07aad4bfa
4
+ data.tar.gz: d7cd58655aae8c5f75ca23d844cdf0820d4fcb874475aad221123473333eef25
5
5
  SHA512:
6
- metadata.gz: 1e2bcb076d7d28570316239a3584dbd2fd80386dd2468dd0c99038c832f08297c1cb71e94261e38a9e17858c375bed512fe2e5162e5994f4bef1c97faace8f5d
7
- data.tar.gz: 170b0bb0da5086f2c135ceda394ce0184394e0ff49b5a00e0942d45279f2410a002199ef46fcd23e25b241597689f42bb16025419d33cd8bfc8900dd3d3be3a8
6
+ metadata.gz: 7ec7123d4ab6f0d7a771e473fa7de24baf8ff8d273d136c08bb105ed7088377e650b1cf1bfed821f04cf023e5dcdc24951e33d954e7cdd95af61710cfe09b7fb
7
+ data.tar.gz: ea6c899f378bc4f336fcd06be80a39983ba0795371260c5262501e13dcc1c322565fdcd737acea8e433eaab56509e327ee4147c80d8d1bdc3973cd7f1b6e5bf2
@@ -0,0 +1 @@
1
+ github: [dblock]
@@ -1,7 +1,7 @@
1
1
  Metrics:
2
2
  Enabled: false
3
3
 
4
- Metrics/LineLength:
4
+ Layout/LineLength:
5
5
  Max: 500
6
6
  Enabled: false
7
7
 
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-03-23 14:16:02 -0400 using RuboCop version 0.58.2.
3
+ # on 2020-07-17 08:36:05 -0400 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
@@ -9,26 +9,63 @@
9
9
  # Offense count: 3
10
10
  # Cop supports --auto-correct.
11
11
  # Configuration parameters: EnforcedStyle.
12
- # SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent
13
- Layout/IndentHeredoc:
12
+ # SupportedStyles: squiggly, active_support, powerpack, unindent
13
+ Layout/HeredocIndentation:
14
14
  Exclude:
15
15
  - 'lib/slack-ruby-bot-server/info.rb'
16
16
  - 'sample_apps/sample_app_activerecord/commands/help.rb'
17
17
  - 'sample_apps/sample_app_mongoid/commands/help.rb'
18
18
 
19
+ # Offense count: 1
20
+ Lint/AmbiguousOperator:
21
+ Exclude:
22
+ - 'spec/slack-ruby-bot-server/service_spec.rb'
23
+
24
+ # Offense count: 2
25
+ # Cop supports --auto-correct.
26
+ Lint/NonDeterministicRequireOrder:
27
+ Exclude:
28
+ - 'spec/spec_helper.rb'
29
+
30
+ # Offense count: 2
31
+ # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
32
+ # 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
+ Naming/FileName:
34
+ Exclude:
35
+ - 'lib/slack-ruby-bot-server.rb'
36
+ - 'lib/slack-ruby-bot-server/ext/slack-ruby-bot.rb'
37
+
19
38
  # Offense count: 3
20
- # Configuration parameters: Blacklist.
21
- # Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
39
+ # Configuration parameters: ForbiddenDelimiters.
40
+ # ForbiddenDelimiters: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$))
22
41
  Naming/HeredocDelimiterNaming:
23
42
  Exclude:
24
43
  - 'lib/slack-ruby-bot-server/info.rb'
25
44
  - 'sample_apps/sample_app_activerecord/commands/help.rb'
26
45
  - 'sample_apps/sample_app_mongoid/commands/help.rb'
27
46
 
47
+ # Offense count: 3
48
+ # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
49
+ # AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp
50
+ Naming/MethodParameterName:
51
+ Exclude:
52
+ - 'lib/slack-ruby-bot-server/models/team/activerecord.rb'
53
+ - 'lib/slack-ruby-bot-server/models/team/mongoid.rb'
54
+ - 'lib/slack-ruby-bot-server/service.rb'
55
+
28
56
  # Offense count: 1
29
57
  # Cop supports --auto-correct.
30
- # Configuration parameters: EnforcedStyle.
31
- # SupportedStyles: module_function, extend_self
58
+ # Configuration parameters: EnforcedStyle, Autocorrect.
59
+ # SupportedStyles: module_function, extend_self, forbidden
32
60
  Style/ModuleFunction:
33
61
  Exclude:
34
62
  - 'lib/slack-ruby-bot-server/config.rb'
63
+
64
+ # Offense count: 1
65
+ # Cop supports --auto-correct.
66
+ # Configuration parameters: AutoCorrect, EnforcedStyle, IgnoredMethods.
67
+ # SupportedStyles: predicate, comparison
68
+ Style/NumericPredicate:
69
+ Exclude:
70
+ - 'spec/**/*'
71
+ - 'lib/slack-ruby-bot-server/service.rb'
@@ -4,24 +4,26 @@ cache: bundler
4
4
 
5
5
  matrix:
6
6
  include:
7
- - rvm: 2.3.1
7
+ - rvm: 2.6.2
8
8
  script:
9
9
  - bundle exec danger
10
- - rvm: 2.3.1
10
+ services:
11
+ - xvfb
12
+ - rvm: 2.6.2
11
13
  env: DATABASE_ADAPTER=activerecord
12
14
  services:
15
+ - xvfb
13
16
  - postgresql
14
- - rvm: 2.3.1
17
+ - rvm: 2.6.2
15
18
  env: DATABASE_ADAPTER=mongoid
16
19
  services:
20
+ - xvfb
17
21
  - mongodb
18
22
 
19
23
  addons:
20
24
  firefox: 54.0
21
25
 
22
26
  before_install:
23
- - export DISPLAY=:99.0
24
- - sh -e /etc/init.d/xvfb start
25
27
  - wget https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.18.0-linux64.tar.gz
26
28
  - mkdir geckodriver
27
29
  - tar -xzf geckodriver-v0.18.0-linux64.tar.gz -C geckodriver
@@ -1,16 +1,43 @@
1
1
  ### Changelog
2
2
 
3
- #### 0.11.0 (Next)
3
+ #### 0.12.3 (2020/11/14)
4
+
5
+ * [#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).
6
+ * [#127](https://github.com/slack-ruby/slack-ruby-bot-server/pull/127): Added a Rails bot example link - [@CrazyOptimist](https://github.com/CrazyOptimist).
7
+
8
+ #### 0.12.2 (2020/7/26)
9
+
10
+ * [#120](https://github.com/slack-ruby/slack-ruby-bot-server/pull/120): Removed GIFs - [@dblock](https://github.com/dblock).
11
+ * [#121](https://github.com/slack-ruby/slack-ruby-bot-server/pull/121): Added TOC - [@dblock](https://github.com/dblock).
12
+
13
+ #### 0.12.1 (2020/5/16)
14
+
15
+ * [#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).
16
+
17
+ #### 0.12.0 (2020/4/26)
18
+
19
+ * [#113](https://github.com/slack-ruby/slack-ruby-bot-server/pull/113): Added support for intervals with `.every` - [@dblock](https://github.com/dblock).
20
+ * [#112](https://github.com/slack-ruby/slack-ruby-bot-server/pull/112): Added support for multiple `.on` events as an argument - [@dblock](https://github.com/dblock).
21
+ * [#111](https://github.com/slack-ruby/slack-ruby-bot-server/pull/111): Removed dependency on Virtus - [@dblock](https://github.com/dblock).
22
+ * [#110](https://github.com/slack-ruby/slack-ruby-bot-server/pull/110): Fixed ActiveRecord sample app - [@CeeBeeUK](https://github.com/CeeBeeUK).
23
+ * [#114](https://github.com/slack-ruby/slack-ruby-bot-server/pull/114): Used `require_relative` to prevent local collisions - [@dblock](https://github.com/dblock).
24
+ * [#115](https://github.com/slack-ruby/slack-ruby-bot-server/pull/115): Added `Config#view_paths` to be used by Rack middleware - [@dblock](https://github.com/dblock).
25
+
26
+ #### 0.11.1 (2019/5/17)
27
+
28
+ * [#104](https://github.com/slack-ruby/slack-ruby-bot-server/pull/104): Locked async-websocket at a compatible 0.8.0 version - [@dblock](https://github.com/dblock).
29
+
30
+ #### 0.11.0 (2019/4/12)
4
31
 
5
32
  * [#101](https://github.com/slack-ruby/slack-ruby-bot-server/pull/101): Removed legacy migrations, including `SLACK_API_TOKEN`, team `name`, `team_id` and `active` - [@dblock](https://github.com/dblock).
6
33
  * [#98](https://github.com/slack-ruby/slack-ruby-bot-server/pull/98): Removed `unicorn` from gem dependencies - [@dblock](https://github.com/dblock).
7
- * [#90](https://github.com/slack-ruby/slack-ruby-bot-server/pull/90): Update ActiveRecord sample app to support ENV variables in `postgresql.yml` - [@ashkan18](https://github.com/ashkan18).
34
+ * [#90](https://github.com/slack-ruby/slack-ruby-bot-server/pull/90): Updated ActiveRecord sample app to support ENV variables in `postgresql.yml` - [@ashkan18](https://github.com/ashkan18).
8
35
 
9
36
  #### 0.10.0 (2019/3/23)
10
37
 
11
38
  * [#97](https://github.com/slack-ruby/slack-ruby-bot-server/pull/97): Added `Config#service_class` to override the `SlackRubyBotServer::Service.instance` singleton - [@dblock](https://github.com/dblock).
12
39
  * [#96](https://github.com/slack-ruby/slack-ruby-bot-server/pull/96): Added `Team#bot_user_id`, `activated_user_id` and `activated_user_access_token` - [@dblock](https://github.com/dblock).
13
- * [#95](https://github.com/slack-ruby/slack-ruby-bot-server/pull/95): Expose the optional `state` parameter that is returned from the Add to Slack button - [@aok-solutions](https://github.com/aok-solutions).
40
+ * [#95](https://github.com/slack-ruby/slack-ruby-bot-server/pull/95): Exposed the optional `state` parameter that is returned from the Add to Slack button - [@aok-solutions](https://github.com/aok-solutions).
14
41
 
15
42
  #### 0.9.0 (2019/2/25)
16
43
 
@@ -18,31 +45,31 @@
18
45
 
19
46
  #### 0.8.3 (2019/1/17)
20
47
 
21
- * [#88](https://github.com/slack-ruby/slack-ruby-bot-server/issues/88): Fix: compatibility with Grape 1.2.x - [@dblock](https://github.com/dblock).
48
+ * [#88](https://github.com/slack-ruby/slack-ruby-bot-server/issues/88): Fixed compatibility with Grape 1.2.x - [@dblock](https://github.com/dblock).
22
49
 
23
50
  #### 0.8.2 (2018/10/11)
24
51
 
25
- * [#80](https://github.com/slack-ruby/slack-ruby-bot-server/pull/80): Fix: closed stream when closing connection in ping worker - [@dblock](https://github.com/dblock).
52
+ * [#80](https://github.com/slack-ruby/slack-ruby-bot-server/pull/80): Fixed closed stream when closing connection in ping worker - [@dblock](https://github.com/dblock).
26
53
 
27
54
  #### 0.8.1 (2018/9/20)
28
55
 
29
- * [#79](https://github.com/slack-ruby/slack-ruby-bot-server/pull/79): Fix: ping worker terminates on a failed restart - [@dblock](https://github.com/dblock).
56
+ * [#79](https://github.com/slack-ruby/slack-ruby-bot-server/pull/79): Fixed ping worker terminates on a failed restart - [@dblock](https://github.com/dblock).
30
57
 
31
58
  #### 0.8.0 (2018/9/8)
32
59
 
33
- * [#75](https://github.com/slack-ruby/slack-ruby-bot-server/pull/75): Default to `async-websocket` instead of `celluloid-io` - [@dblock](https://github.com/dblock).
34
- * [#76](https://github.com/slack-ruby/slack-ruby-bot-server/pull/76): Adjust wait time on restart on Slack::Web::Api::Errors::TooManyRequestsError - [@dblock](https://github.com/dblock).
60
+ * [#75](https://github.com/slack-ruby/slack-ruby-bot-server/pull/75): Defaulted to `async-websocket` instead of `celluloid-io` - [@dblock](https://github.com/dblock).
61
+ * [#76](https://github.com/slack-ruby/slack-ruby-bot-server/pull/76): Adjusted wait time on restart on Slack::Web::Api::Errors::TooManyRequestsError - [@dblock](https://github.com/dblock).
35
62
 
36
63
  #### 0.7.0 (2018/8/22)
37
64
 
38
- * [#60](https://github.com/slack-ruby/slack-ruby-bot-server/pull/60): Log caught Standard::Error backtrace at debug-level - [@alexagranov](https://github.com/alexagranov).
65
+ * [#60](https://github.com/slack-ruby/slack-ruby-bot-server/pull/60): Logged caught Standard::Error backtrace at debug-level - [@alexagranov](https://github.com/alexagranov).
39
66
  * [#65](https://github.com/slack-ruby/slack-ruby-bot-server/pull/65): Updated Capybara and selenium-webdriver - [@dblock](https://github.com/dblock).
40
67
  * [#67](https://github.com/slack-ruby/slack-ruby-bot-server/pull/67): Only load the OTR::ActiveRecord::ConnectionManagement middleware when the OTR module is included. This module isn't needed when using Rails - [@darbyfrey](https://github.com/darbyfrey).
41
68
  * [#74](https://github.com/slack-ruby/slack-ruby-bot-server/pull/74): Added ping worker, will restart offline bots - [@dblock](https://github.com/dblock).
42
69
 
43
70
  #### 0.6.1 (2017/3/29)
44
71
 
45
- * [#54](https://github.com/slack-ruby/slack-ruby-bot-server/pull/54): Removing one more Mongoid dependency when rescuing from _invoke - [@alexagranov](https://github.com/alexagranov).
72
+ * [#54](https://github.com/slack-ruby/slack-ruby-bot-server/pull/54): Removed one more Mongoid dependency when rescuing from _invoke - [@alexagranov](https://github.com/alexagranov).
46
73
  * [#53](https://github.com/slack-ruby/slack-ruby-bot-server/pull/53): No need for `otr-activerecord` if using activerecord under Rails - [@alexagranov](https://github.com/alexagranov).
47
74
 
48
75
  #### 0.6.0 (2017/3/12)
@@ -53,17 +80,17 @@
53
80
 
54
81
  #### 0.5.0 (2017/2/9)
55
82
 
56
- * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fix: `cannot load kaminari/grape` - [@dblock](https://github.com/dblock).
57
- * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fix: `undefined method '[]' for nil:NilClass` in APIs - [@dblock](https://github.com/dblock).
58
- * [#28](https://github.com/slack-ruby/slack-ruby-bot-server/pull/28): Use slack-ruby-danger gem - [@dblock](https://github.com/dblock).
59
- * [#31](https://github.com/slack-ruby/slack-ruby-bot-server/pull/31): Adds MONGODB_URI as environment variable for MongoDB - [@corprew](https://github.com/corprew).
83
+ * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fixed `cannot load kaminari/grape` - [@dblock](https://github.com/dblock).
84
+ * [#43](https://github.com/slack-ruby/slack-ruby-bot-server/pull/43): Fixed `undefined method '[]' for nil:NilClass` in APIs - [@dblock](https://github.com/dblock).
85
+ * [#28](https://github.com/slack-ruby/slack-ruby-bot-server/pull/28): Used `slack-ruby-danger` gem - [@dblock](https://github.com/dblock).
86
+ * [#31](https://github.com/slack-ruby/slack-ruby-bot-server/pull/31): Added `MONGODB_URI` as environment variable for MongoDB - [@corprew](https://github.com/corprew).
60
87
 
61
88
  #### 0.4.0 (2016/8/29)
62
89
 
63
90
  * [#25](https://github.com/slack-ruby/slack-ruby-bot-server/pull/25): Added [danger](http://danger.systems), PR linting - [@dblock](https://github.com/dblock).
64
91
  * The `reset` and `resetting` callbacks have been removed - [@dblock](https://github.com/dblock).
65
92
  * Callbacks receive `team` and `error`, but no longer `server` - [@dblock](https://github.com/dblock).
66
- * [#23](https://github.com/slack-ruby/slack-ruby-bot-server/issues/23): Fix: server-side removal of bot doesn't deactivate it - [@dblock](https://github.com/dblock).
93
+ * [#23](https://github.com/slack-ruby/slack-ruby-bot-server/issues/23): Fixed server-side removal of bot doesn't deactivate it - [@dblock](https://github.com/dblock).
67
94
 
68
95
  #### 0.3.1 (2016/7/10)
69
96
 
@@ -87,13 +114,13 @@
87
114
 
88
115
  * Initial public release - [@dblock](https://github.com/dblock).
89
116
  * Renamed slack-bot-server to slack-ruby-bot-server - [@dblock](https://github.com/dblock).
90
- * [#11](https://github.com/slack-ruby/slack-ruby-bot-server/pull/11): Turn project into gem - [@tmsrjs](https://github.com/tmsrjs).
91
- * Use `celluloid-io` instead of `faye-websocket`, upgrade to slack-ruby-bot 0.8.0 - [@dblock](https://github.com/dblock).
117
+ * [#11](https://github.com/slack-ruby/slack-ruby-bot-server/pull/11): Turned project into gem - [@tmsrjs](https://github.com/tmsrjs).
118
+ * Used `celluloid-io` instead of `faye-websocket`, upgrade to slack-ruby-bot 0.8.0 - [@dblock](https://github.com/dblock).
92
119
  * Fixed `SlackRubyBotServer#reset` - [@dblock](https://github.com/dblock).
93
- * Use Grape 0.15.x - [@dblock](https://github.com/dblock).
120
+ * Used Grape 0.15.x - [@dblock](https://github.com/dblock).
94
121
  * Removed OOB GC - [@dblock](https://github.com/dblock).
95
- * Use Unicorn instead of Puma - [@dblock](https://github.com/dblock).
96
- * Fix: wait for EventMachine reactor to start - [@dblock](https://github.com/dblock).
97
- * Use an OOB GC - [@dblock](https://github.com/dblock).
98
- * Defer start, much faster - [@dblock](https://github.com/dblock).
122
+ * Used Unicorn instead of Puma - [@dblock](https://github.com/dblock).
123
+ * Fixed wait for EventMachine reactor to start - [@dblock](https://github.com/dblock).
124
+ * Used an OOB GC - [@dblock](https://github.com/dblock).
125
+ * Deferred start, much faster - [@dblock](https://github.com/dblock).
99
126
  * Backported changes from slack-metabot and slack-shellbot - [@dblock](https://github.com/dblock).
data/Dangerfile CHANGED
@@ -1 +1,2 @@
1
1
  danger.import_dangerfile(gem: 'slack-ruby-danger')
2
+ toc.check!
data/Gemfile CHANGED
@@ -11,7 +11,7 @@ when 'activerecord' then
11
11
  gem 'otr-activerecord', '~> 1.2.1'
12
12
  gem 'cursor_pagination' # rubocop:disable Bundler/OrderedGems
13
13
  gem 'pg'
14
- when nil then
14
+ when nil
15
15
  warn "Missing ENV['DATABASE_ADAPTER']."
16
16
  else
17
17
  warn "Invalid ENV['DATABASE_ADAPTER']: #{ENV['DATABASE_ADAPTER']}."
@@ -31,12 +31,13 @@ group :development, :test do
31
31
  gem 'rack-test'
32
32
  gem 'rake'
33
33
  gem 'rspec'
34
- gem 'rubocop', '0.58.2'
34
+ gem 'rubocop', '0.81.0'
35
35
  gem 'selenium-webdriver', '~> 3.4.4'
36
36
  gem 'vcr'
37
37
  gem 'webmock'
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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-2019 Daniel Doubrovkine & Contributors
3
+ Copyright (c) 2015-2020 Daniel Doubrovkine & Contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -11,13 +11,36 @@ If you are not familiar with Slack bots or Slack API concepts, you might want to
11
11
 
12
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
13
 
14
+ # Table of Contents
15
+
16
+ - [What is this?](#what-is-this)
17
+ - [Stable Release](#stable-release)
18
+ - [Try Me](#try-me)
19
+ - [Run Your Own](#run-your-own)
20
+ - [MongoDB](#mongodb)
21
+ - [ActiveRecord](#activerecord)
22
+ - [Usage](#usage)
23
+ - [OAuth Code Grant](#oauth-code-grant)
24
+ - [API](#api)
25
+ - [App](#app)
26
+ - [Service Manager](#service-manager)
27
+ - [Lifecycle Callbacks](#lifecycle-callbacks)
28
+ - [Service Timers](#service-timers)
29
+ - [Extensions](#extensions)
30
+ - [Server Class](#server-class)
31
+ - [Service Class](#service-class)
32
+ - [HTML Templates](#html-templates)
33
+ - [Access Tokens](#access-tokens)
34
+ - [Example Bots Using Slack Ruby Bot Server](#example-bots-using-slack-ruby-bot-server)
35
+ - [Copyright & License](#copyright--license)
36
+
14
37
  ### What is this?
15
38
 
16
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.
17
40
 
18
41
  ### Stable Release
19
42
 
20
- You're reading the documentation for the **stable** release of slack-ruby-bot-server, v0.11.0. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
43
+ You're reading the documentation for the **stable** release of slack-ruby-bot-server, 0.12.3. See [UPGRADING](UPGRADING.md) when upgrading from an older version.
21
44
 
22
45
  ### Try Me
23
46
 
@@ -62,19 +85,48 @@ See the [sample app using ActiveRecord](sample_apps/sample_app_activerecord) for
62
85
 
63
86
  ### Usage
64
87
 
65
- Start with one of the samples above, which contain a couple of custom commands, necessary dependencies and tests, then [create a new application](https://api.slack.com/applications/new) on Slack.
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).
66
89
 
67
- ![](images/new.png)
90
+ ![](images/create-app.png)
68
91
 
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 `http://localhost:9292`.
92
+ 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.
70
93
 
71
94
  Within your application, edit your `.env` file and add `SLACK_CLIENT_ID=...` and `SLACK_CLIENT_SECRET=...` in it.
72
95
 
73
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.
74
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:
109
+
110
+ ```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']
123
+ ```
124
+
125
+ The token is stored in persistent storage and used each time a Slack client is instantiated for the specific team.
126
+
75
127
  ### API
76
128
 
77
- 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.
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.
78
130
 
79
131
  #### App
80
132
 
@@ -104,11 +156,17 @@ MyApp.instance.prepare!
104
156
 
105
157
  #### Service Manager
106
158
 
159
+ ##### Lifecycle Callbacks
160
+
107
161
  You can introduce custom behavior into the service lifecycle via callbacks. This can be useful when new team has been registered via the API or a team has been deactivated from Slack.
108
162
 
109
163
  ```ruby
110
164
  instance = SlackRubyBotServer::Service.instance
111
165
 
166
+ instance.on :started, :stopped do |team|
167
+ # team has been started or stopped
168
+ end
169
+
112
170
  instance.on :created do |team, error, options|
113
171
  # a new team has been registered
114
172
  end
@@ -138,7 +196,6 @@ The following callbacks are supported. All callbacks receive a `team`, except `e
138
196
  | deactivating | a team is being deactivated |
139
197
  | deactivated | a team has been deactivated |
140
198
 
141
-
142
199
  The [Add to Slack button](https://api.slack.com/docs/slack-button) also allows for an optional `state` parameter that will be returned on completion of the request. The `creating` and `created` callbacks include an options hash where this value can be accessed (to check for forgery attacks for instance).
143
200
  ```ruby
144
201
  auth = OpenSSL::HMAC.hexdigest("SHA256", "key", "data")
@@ -153,9 +210,44 @@ instance.on :creating do |team, error, options|
153
210
  end
154
211
  ```
155
212
 
156
- A number of extensions use service manager callbacks to implement useful functionality.
213
+ ##### Service Timers
214
+
215
+ You can introduce custom behavior into the service lifecycle on a timer. For example, check whether a team's trial has expired, or periodically cleanup data.
216
+
217
+ Note that unlike callbacks, timers are global for the entire service.
218
+
219
+ ```ruby
220
+ instance = SlackRubyBotServer::Service.instance
221
+
222
+ instance.every :hour do
223
+ Team.each do |team|
224
+ begin
225
+ # do something with every team once an hour
226
+ rescue StandardError
227
+ end
228
+ end
229
+ end
230
+
231
+ instance.every :minute do
232
+ # called every minute
233
+ end
234
+
235
+ instance.every :second do
236
+ # called every second
237
+ end
238
+
239
+ instance.every 30 do
240
+ # called every 30 seconds
241
+ end
242
+ ```
243
+
244
+ ##### Extensions
245
+
246
+ A number of extensions use service manager callbacks and service timers to implement useful functionality.
157
247
 
248
+ * [slack-ruby-bot-server-events](https://github.com/slack-ruby/slack-ruby-bot-server-events): Easily handle Slack slash commands, interactive buttons and events.
158
249
  * [slack-ruby-bot-server-mailchimp](https://github.com/slack-ruby/slack-ruby-bot-server-mailchimp): Subscribes new bot users to a Mailchimp mailing list.
250
+ * [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.
159
251
 
160
252
  #### Server Class
161
253
 
@@ -196,6 +288,18 @@ SlackRubyBotServer::Service.instance # MyService
196
288
  SlackRubyBotServer::Service.instance.url # https://www.example.com
197
289
  ```
198
290
 
291
+ ### HTML Templates
292
+
293
+ This library provides a [default HTML template and JS scripts](public) that implement the "Add to Slack" button workflow. Customize your pages by adding a `public` directory in your application and starting with a [index.html.erb](public/index.html.erb) template. The application's `views` and `public` folders are [loaded by default](lib/slack-ruby-bot-server/api/middleware.rb#L32).
294
+
295
+ You can add to or override template paths as follows.
296
+
297
+ ```ruby
298
+ SlackRubyBotServer.configure do |config|
299
+ config.view_paths << File.expand_path(File.join(__dir__, 'public'))
300
+ end
301
+ ```
302
+
199
303
  ### Access Tokens
200
304
 
201
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.
@@ -206,18 +310,20 @@ By default the implementation of [Team](lib/slack-ruby-bot-server/models/team) s
206
310
 
207
311
  You can see a sample implementation in [slack-sup#3a497b](https://github.com/dblock/slack-sup/commit/3a497b436d25d3a7738562655cda64b180ae0096).
208
312
 
209
- ### Examples Using Slack Ruby Bot Server
313
+ ### Example Bots Using Slack Ruby Bot Server
210
314
 
211
- * [slack-sup](https://github.com/dblock/slack-sup), free service at [sup.playplay.io](https://sup.playplay.io)
212
- * [slack-gamebot](https://github.com/dblock/slack-gamebot), free service at [www.playplay.io](https://www.playplay.io)
213
- * [slack-market](https://github.com/dblock/slack-market), free service at [market.playplay.io](https://market.playplay.io)
214
- * [slack-shellbot](https://github.com/slack-ruby/slack-shellbot), free service at [shell.playplay.io](https://shell.playplay.io)
215
- * [slack-api-explorer](https://github.com/slack-ruby/slack-api-explorer), free service at [api-explorer.playplay.io](https://shell.playplay.io)
216
- * [slack-strava](https://github.com/dblock/slack-strava), free service at [slava.playplay.io](https://slava.playplay.io)
217
- * [slack-arena](https://github.com/dblock/slack-arena), free service at [arena.playplay.io](https://arena.playplay.io)
315
+ * [slack-ruby-bot-server-sample](https://github.com/slack-ruby/slack-ruby-bot-server-sample), a generic sample
316
+ * [slack-sup](https://github.com/dblock/slack-sup), see [sup.playplay.io](https://sup.playplay.io)
317
+ * [slack-gamebot](https://github.com/dblock/slack-gamebot), see [www.playplay.io](https://www.playplay.io)
318
+ * [slack-market](https://github.com/dblock/slack-market), see [market.playplay.io](https://market.playplay.io)
319
+ * [slack-shellbot](https://github.com/slack-ruby/slack-shellbot), see [shell.playplay.io](https://shell.playplay.io)
320
+ * [slack-api-explorer](https://github.com/slack-ruby/slack-api-explorer), see [api-explorer.playplay.io](https://shell.playplay.io)
321
+ * [slack-strava](https://github.com/dblock/slack-strava), see [slava.playplay.io](https://slava.playplay.io)
322
+ * [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
218
324
 
219
325
  ### Copyright & License
220
326
 
221
- Copyright [Daniel Doubrovkine](http://code.dblock.org) and Contributors, 2015-2019
327
+ Copyright [Daniel Doubrovkine](http://code.dblock.org) and Contributors, 2015-2020
222
328
 
223
329
  [MIT License](LICENSE)
Binary file
@@ -2,15 +2,13 @@ require 'async/websocket'
2
2
 
3
3
  require 'grape-swagger'
4
4
  require 'slack-ruby-bot'
5
- require 'slack-ruby-bot-server/service'
6
- require 'slack-ruby-bot-server/server'
7
- require 'slack-ruby-bot-server/config'
8
5
 
9
- require 'slack-ruby-bot-server/ext'
10
- require 'slack-ruby-bot-server/version'
11
- require 'slack-ruby-bot-server/info'
12
-
13
- require "slack-ruby-bot-server/config/database_adapters/#{SlackRubyBotServer::Config.database_adapter}.rb"
14
-
15
- require 'slack-ruby-bot-server/api'
16
- require 'slack-ruby-bot-server/app'
6
+ require_relative 'slack-ruby-bot-server/service'
7
+ require_relative 'slack-ruby-bot-server/server'
8
+ require_relative 'slack-ruby-bot-server/config'
9
+ require_relative 'slack-ruby-bot-server/ext'
10
+ require_relative 'slack-ruby-bot-server/version'
11
+ require_relative 'slack-ruby-bot-server/info'
12
+ require_relative "slack-ruby-bot-server/config/database_adapters/#{SlackRubyBotServer::Config.database_adapter}.rb"
13
+ require_relative 'slack-ruby-bot-server/api'
14
+ require_relative 'slack-ruby-bot-server/app'
@@ -3,7 +3,7 @@ require 'roar'
3
3
  require 'grape-roar'
4
4
 
5
5
  require_relative 'ext/grape/sort_extension'
6
- require 'slack-ruby-bot-server/api/helpers'
7
- require 'slack-ruby-bot-server/api/presenters'
8
- require 'slack-ruby-bot-server/api/endpoints'
9
- require 'slack-ruby-bot-server/api/middleware'
6
+ require_relative 'api/helpers'
7
+ require_relative 'api/presenters'
8
+ require_relative 'api/endpoints'
9
+ require_relative 'api/middleware'
@@ -1,3 +1,3 @@
1
- require 'slack-ruby-bot-server/api/endpoints/teams_endpoint'
2
- require 'slack-ruby-bot-server/api/endpoints/status_endpoint'
3
- require 'slack-ruby-bot-server/api/endpoints/root_endpoint'
1
+ require_relative 'endpoints/teams_endpoint'
2
+ require_relative 'endpoints/status_endpoint'
3
+ require_relative 'endpoints/root_endpoint'
@@ -54,12 +54,16 @@ module SlackRubyBotServer
54
54
  team ||= Team.where(team_id: rc['team_id']).first
55
55
 
56
56
  if team
57
+ team.ping_if_active!
58
+
57
59
  team.update_attributes!(
58
60
  activated_user_id: user_id,
59
61
  activated_user_access_token: access_token,
60
62
  bot_user_id: bot_user_id
61
63
  )
64
+
62
65
  raise "Team #{team.name} is already registered." if team.active?
66
+
63
67
  team.activate!(token)
64
68
  else
65
69
  team = Team.create!(
@@ -1,4 +1,4 @@
1
- require 'slack-ruby-bot-server/api/helpers/cursor_helpers'
2
- require 'slack-ruby-bot-server/api/helpers/pagination_parameters'
3
- require 'slack-ruby-bot-server/api/helpers/sort_helpers'
4
- require 'slack-ruby-bot-server/api/helpers/error_helpers'
1
+ require_relative 'helpers/cursor_helpers'
2
+ require_relative 'helpers/pagination_parameters'
3
+ require_relative 'helpers/sort_helpers'
4
+ require_relative 'helpers/error_helpers'
@@ -11,6 +11,7 @@ module SlackRubyBotServer
11
11
  if SlackRubyBotServer::Config.mongoid?
12
12
  def paginate_by_cursor(coll, _options)
13
13
  raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
14
+
14
15
  results = { results: [], next: nil }
15
16
  coll = coll.skip(params[:offset].to_i) if params.key?(:offset)
16
17
  size = (params[:size] || 10).to_i
@@ -26,6 +27,7 @@ module SlackRubyBotServer
26
27
  elsif SlackRubyBotServer::Config.activerecord?
27
28
  def paginate_by_cursor(coll, options)
28
29
  raise 'Both cursor and offset parameters are present, these are mutually exclusive.' if params.key?(:offset) && params.key?(:cursor)
30
+
29
31
  results = { results: [], next: nil }
30
32
  size = (params[:size] || 10).to_i
31
33
  results[:total_count] = coll.count(:all) if params[:total_count]
@@ -7,6 +7,7 @@ module SlackRubyBotServer
7
7
  def sort_order(options = {})
8
8
  params[:sort] = options[:default_sort_order] unless params[:sort]
9
9
  return [] unless params[:sort]
10
+
10
11
  sort_order = params[:sort].to_s
11
12
  unless options[:default_sort_order] == sort_order
12
13
  supported_sort_orders = route_sort
@@ -30,11 +30,7 @@ module SlackRubyBotServer
30
30
  end
31
31
 
32
32
  use Rack::ServerPages do |config|
33
- config.view_path = [
34
- 'views', # relative to Dir.pwd
35
- 'public', # relative to Dir.pwd
36
- File.expand_path(File.join(__dir__, '../../../public')) # built-in fallback
37
- ]
33
+ config.view_path = SlackRubyBotServer::Config.view_paths
38
34
  end
39
35
 
40
36
  run Middleware.new
@@ -2,8 +2,8 @@ require 'roar/representer'
2
2
  require 'roar/json'
3
3
  require 'roar/json/hal'
4
4
 
5
- require 'slack-ruby-bot-server/api/presenters/paginated_presenter'
6
- require 'slack-ruby-bot-server/api/presenters/status_presenter'
7
- require 'slack-ruby-bot-server/api/presenters/team_presenter'
8
- require 'slack-ruby-bot-server/api/presenters/teams_presenter'
9
- require 'slack-ruby-bot-server/api/presenters/root_presenter'
5
+ require_relative 'presenters/paginated_presenter'
6
+ require_relative 'presenters/status_presenter'
7
+ require_relative 'presenters/team_presenter'
8
+ require_relative 'presenters/teams_presenter'
9
+ require_relative 'presenters/root_presenter'
@@ -23,6 +23,7 @@ module SlackRubyBotServer
23
23
  raise 'Unsupported database driver.'
24
24
  end
25
25
  return unless team
26
+
26
27
  team.ping!
27
28
  end
28
29
 
@@ -5,10 +5,18 @@ module SlackRubyBotServer
5
5
  attr_accessor :server_class
6
6
  attr_accessor :service_class
7
7
  attr_accessor :database_adapter
8
+ attr_accessor :view_paths
8
9
 
9
10
  def reset!
10
11
  self.server_class = SlackRubyBotServer::Server
11
12
  self.service_class = SlackRubyBotServer::Service
13
+
14
+ self.view_paths = [
15
+ 'views',
16
+ 'public',
17
+ File.expand_path(File.join(__dir__, '../../public'))
18
+ ]
19
+
12
20
  self.database_adapter = if defined?(::Mongoid)
13
21
  :mongoid
14
22
  elsif defined?(::ActiveRecord)
@@ -1,4 +1,4 @@
1
- require 'slack-ruby-bot-server/models/team/activerecord.rb'
1
+ require_relative '../../models/team/activerecord.rb'
2
2
 
3
3
  module SlackRubyBotServer
4
4
  module DatabaseAdapter
@@ -28,4 +28,4 @@ module SlackRubyBotServer
28
28
  end
29
29
  end
30
30
 
31
- ::Boolean = Virtus::Attribute::Boolean
31
+ ::Boolean = Grape::API::Boolean
@@ -1,4 +1,5 @@
1
- require 'slack-ruby-bot-server/models/team/mongoid.rb'
1
+ require_relative '../../models/team/mongoid.rb'
2
+
2
3
  require 'kaminari/grape'
3
4
  require 'mongoid-scroll'
4
5
 
@@ -6,7 +7,8 @@ module SlackRubyBotServer
6
7
  module DatabaseAdapter
7
8
  def self.check!
8
9
  rc = Mongoid.default_client.command(ping: 1)
9
- return if rc && rc.ok?
10
+ return if rc&.ok?
11
+
10
12
  raise rc.documents.first['error'] || 'Unexpected error.'
11
13
  rescue StandardError => e
12
14
  warn "Error connecting to MongoDB: #{e.message}"
@@ -9,7 +9,7 @@ module SlackRubyBot
9
9
  rescue StandardError => e
10
10
  logger.info "#{name.demodulize.upcase}: #{client.owner}, #{e.class}: #{e}"
11
11
  logger.debug e.backtrace.join("\n")
12
- client.say(channel: data.channel, text: e.message, gif: 'error')
12
+ client.say(channel: data.channel, text: e.message)
13
13
  true
14
14
  end
15
15
  end
@@ -8,12 +8,12 @@ module SlackRubyBot
8
8
  _invoke client, data
9
9
  rescue Mongoid::Errors::Validations => e
10
10
  logger.info "#{name.demodulize.upcase}: #{client.owner}, error - #{e.document.class}, #{e.document.errors.to_hash}"
11
- client.say(channel: data.channel, text: e.document.errors.first[1], gif: 'error')
11
+ client.say(channel: data.channel, text: e.document.errors.first[1])
12
12
  true
13
13
  rescue StandardError => e
14
14
  logger.info "#{name.demodulize.upcase}: #{client.owner}, #{e.class}: #{e}"
15
15
  logger.debug e.backtrace.join("\n")
16
- client.say(channel: data.channel, text: e.message, gif: 'error')
16
+ client.say(channel: data.channel, text: e.message)
17
17
  true
18
18
  end
19
19
  end
@@ -3,11 +3,14 @@ require_relative 'methods'
3
3
  class Team < ActiveRecord::Base
4
4
  include Methods
5
5
 
6
- def self.purge!
7
- # destroy teams inactive for two weeks
8
- Team.where(active: false).where('updated_at <= ?', 2.weeks.ago).each do |team|
9
- puts "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
10
- team.destroy
6
+ def self.purge!(dt = 2.weeks.ago)
7
+ Team.where(active: false).where('updated_at <= ?', dt).each do |team|
8
+ begin
9
+ logger.info "Destroying #{team}, inactive since #{team.updated_at}."
10
+ team.destroy
11
+ rescue StandardError => e
12
+ logger.warn "Error destroying #{team}, #{e.message}."
13
+ end
11
14
  end
12
15
  end
13
16
  end
@@ -38,5 +38,17 @@ module Methods
38
38
  presence: client.users_getPresence(user: auth['user_id'])
39
39
  }
40
40
  end
41
+
42
+ def ping_if_active!
43
+ return unless active?
44
+
45
+ ping!
46
+ rescue Slack::Web::Api::Errors::SlackError => e
47
+ logger.warn "Active team #{self} ping, #{e.message}."
48
+ case e.message
49
+ when 'account_inactive', 'invalid_auth'
50
+ deactivate!
51
+ end
52
+ end
41
53
  end
42
54
  end
@@ -15,11 +15,15 @@ class Team
15
15
 
16
16
  include Methods
17
17
 
18
- def self.purge!
18
+ def self.purge!(dt = 2.weeks.ago)
19
19
  # destroy teams inactive for two weeks
20
- Team.where(active: false, :updated_at.lte => 2.weeks.ago).each do |team|
21
- Mongoid.logger.info "Destroying #{team}, inactive since #{team.updated_at}, over two weeks ago."
22
- team.destroy
20
+ Team.where(active: false, :updated_at.lte => dt).each do |team|
21
+ begin
22
+ logger.info "Destroying #{team}, inactive since #{team.updated_at}."
23
+ team.destroy
24
+ rescue StandardError => e
25
+ logger.warn "Error destroying #{team}, #{e.message}."
26
+ end
23
27
  end
24
28
  end
25
29
  end
@@ -8,6 +8,7 @@ module SlackRubyBotServer
8
8
  attrs = attrs.dup
9
9
  @team = attrs.delete(:team)
10
10
  raise 'Missing team' unless @team
11
+
11
12
  attrs[:token] = @team.token
12
13
  super(attrs)
13
14
  open!
@@ -15,10 +15,29 @@ module SlackRubyBotServer
15
15
 
16
16
  def initialize
17
17
  @callbacks = Hash.new { |h, k| h[k] = [] }
18
+ @intervals = Hash.new { |h, k| h[k] = [] }
18
19
  end
19
20
 
20
- def on(type, &block)
21
- @callbacks[type.to_s] << block
21
+ def on(*types, &block)
22
+ Array(types).each do |type|
23
+ @callbacks[type.to_s] << block
24
+ end
25
+ end
26
+
27
+ def every(*intervals, &block)
28
+ Array(intervals).each do |interval|
29
+ case interval
30
+ when :minute
31
+ interval = 60
32
+ when :hour
33
+ interval = 60 * 60
34
+ when :day
35
+ interval = 60 * 60 * 24
36
+ end
37
+ raise "Invalid interval \"#{interval}\"." unless interval.is_a?(Integer) && interval > 0
38
+
39
+ @intervals[interval] << block
40
+ end
22
41
  end
23
42
 
24
43
  def create!(team, options = {})
@@ -49,7 +68,7 @@ module SlackRubyBotServer
49
68
  def stop!(team)
50
69
  logger.info "Stopping team #{team}."
51
70
  run_callbacks :stopping, team
52
- team.server.stop! if team.server
71
+ team.server&.stop!
53
72
  run_callbacks :stopped, team
54
73
  rescue StandardError => e
55
74
  run_callbacks :error, team, e
@@ -64,6 +83,15 @@ module SlackRubyBotServer
64
83
  start!(team)
65
84
  run_callbacks :booted, team
66
85
  end
86
+ start_intervals!
87
+ end
88
+
89
+ def start_intervals!
90
+ @intervals.each_pair do |period, calls|
91
+ _every period do
92
+ calls.each(&:call)
93
+ end
94
+ end
67
95
  end
68
96
 
69
97
  def deactivate!(team)
@@ -86,6 +114,19 @@ module SlackRubyBotServer
86
114
 
87
115
  private
88
116
 
117
+ def _every(tt, &_block)
118
+ ::Async::Reactor.run do |task|
119
+ loop do
120
+ begin
121
+ task.sleep tt
122
+ yield
123
+ rescue StandardError => e
124
+ logger.error e
125
+ end
126
+ end
127
+ end
128
+ end
129
+
89
130
  def start_server!(team, server, wait = 1)
90
131
  team.server = server
91
132
  server.start_async
@@ -106,6 +147,7 @@ module SlackRubyBotServer
106
147
  def run_callbacks(type, team = nil, error = nil, options = {})
107
148
  callbacks = @callbacks[type.to_s]
108
149
  return false unless callbacks
150
+
109
151
  callbacks.each do |c|
110
152
  c.call team, error, options
111
153
  end
@@ -1,3 +1,3 @@
1
1
  module SlackRubyBotServer
2
- VERSION = '0.11.0'.freeze
2
+ VERSION = '0.12.3'.freeze
3
3
  end
@@ -3,6 +3,7 @@
3
3
  <title>Slack Ruby Bot Server</title>
4
4
  <script src="/scripts/jquery-1.7.1.min.js"></script>
5
5
  <script src="/scripts/url.min.js"></script>
6
+ <script src="/scripts/messages.js"></script>
6
7
  <script src="/scripts/register.js"></script>
7
8
  <script src="/scripts/stats.js"></script>
8
9
  </head>
@@ -0,0 +1,33 @@
1
+ var SlackRubyBotServer = {};
2
+
3
+ $(document).ready(function() {
4
+
5
+ SlackRubyBotServer.message = function(text) {
6
+ $('#messages').fadeOut('slow', function() {
7
+ $('#messages').fadeIn('slow').html(text)
8
+ });
9
+ };
10
+
11
+ SlackRubyBotServer.error = function(xhr) {
12
+ try {
13
+ var message;
14
+ if (xhr.responseText) {
15
+ var rc = JSON.parse(xhr.responseText);
16
+ if (rc && rc.message) {
17
+ message = rc.message;
18
+ if (message == 'invalid_code') {
19
+ message = 'The code returned from the OAuth workflow was invalid.'
20
+ } else if (message == 'code_already_used') {
21
+ message = 'The code returned from the OAuth workflow has already been used.'
22
+ }
23
+ }
24
+ }
25
+
26
+ SlackRubyBotServer.message(message || xhr.statusText || xhr.responseText || 'Unexpected Error');
27
+
28
+ } catch(err) {
29
+ SlackRubyBotServer.message(err.message);
30
+ }
31
+ };
32
+
33
+ });
@@ -1,35 +1,4 @@
1
- var SlackRubyBotServer = {};
2
-
3
1
  $(document).ready(function() {
4
-
5
- SlackRubyBotServer.message = function(text) {
6
- $('#messages').fadeOut('slow', function() {
7
- $('#messages').fadeIn('slow').html(text)
8
- });
9
- };
10
-
11
- SlackRubyBotServer.error = function(xhr) {
12
- try {
13
- var message;
14
- if (xhr.responseText) {
15
- var rc = JSON.parse(xhr.responseText);
16
- if (rc && rc.message) {
17
- message = rc.message;
18
- if (message == 'invalid_code') {
19
- message = 'The code returned from the OAuth workflow was invalid.'
20
- } else if (message == 'code_already_used') {
21
- message = 'The code returned from the OAuth workflow has already been used.'
22
- }
23
- }
24
- }
25
-
26
- SlackRubyBotServer.message(message || xhr.statusText || xhr.responseText || 'Unexpected Error');
27
-
28
- } catch(err) {
29
- SlackRubyBotServer.message(err.message);
30
- }
31
- };
32
-
33
2
  // Slack OAuth
34
3
  var code = $.url('?code')
35
4
  var state = $.url('?state')
@@ -7,5 +7,6 @@ This is a sample slack-ruby-bot-server with ActiveRecord.
7
7
  ```
8
8
  bundle install
9
9
  rake db:create db:migrate
10
+ RAILS_ENV=test rake db:migrate
10
11
  rackup
11
12
  ```
@@ -13,7 +13,7 @@ whoami - print your username
13
13
  EOS
14
14
  def self.call(client, data, _match)
15
15
  client.say(channel: data.channel, text: [HELP, SlackRubyBotServer::INFO].join("\n"))
16
- client.say(channel: data.channel, gif: 'help')
16
+ client.say(channel: data.channel)
17
17
  logger.info "HELP: #{client.owner}, user=#{data.user}"
18
18
  end
19
19
  end
@@ -8,7 +8,7 @@ describe Help do
8
8
  let(:message_hook) { SlackRubyBot::Hooks::Message.new }
9
9
  it 'default' do
10
10
  expect(client).to receive(:say).with(channel: 'channel', text: [Help::HELP, SlackRubyBotServer::INFO].join("\n"))
11
- expect(client).to receive(:say).with(channel: 'channel', gif: 'help')
11
+ expect(client).to receive(:say).with(channel: 'channel')
12
12
  message_hook.call(client, Hashie::Mash.new(channel: 'channel', text: "#{SlackRubyBot.config.user} help"))
13
13
  end
14
14
  end
@@ -13,7 +13,7 @@ whoami - print your username
13
13
  EOS
14
14
  def self.call(client, data, _match)
15
15
  client.say(channel: data.channel, text: [HELP, SlackRubyBotServer::INFO].join("\n"))
16
- client.say(channel: data.channel, gif: 'help')
16
+ client.say(channel: data.channel)
17
17
  logger.info "HELP: #{client.owner}, user=#{data.user}"
18
18
  end
19
19
  end
@@ -8,7 +8,7 @@ describe Help do
8
8
  let(:message_hook) { SlackRubyBot::Hooks::Message.new }
9
9
  it 'default' do
10
10
  expect(client).to receive(:say).with(channel: 'channel', text: [Help::HELP, SlackRubyBotServer::INFO].join("\n"))
11
- expect(client).to receive(:say).with(channel: 'channel', gif: 'help')
11
+ expect(client).to receive(:say).with(channel: 'channel')
12
12
  message_hook.call(client, Hashie::Mash.new(channel: 'channel', text: "#{SlackRubyBot.config.user} help"))
13
13
  end
14
14
  end
@@ -1,6 +1,6 @@
1
1
  lib = File.expand_path('lib', __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'slack-ruby-bot-server/version'
3
+ require_relative 'lib/slack-ruby-bot-server/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'slack-ruby-bot-server'
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
15
15
  spec.require_paths = ['lib']
16
16
 
17
- spec.add_dependency 'async-websocket'
17
+ spec.add_dependency 'async-websocket', '~> 0.8.0'
18
18
  spec.add_dependency 'foreman'
19
19
  spec.add_dependency 'grape'
20
20
  spec.add_dependency 'grape-roar', '>= 0.4.0'
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-ruby-bot-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Doubrovkine
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-12 00:00:00.000000000 Z
11
+ date: 2020-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-websocket
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.8.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.8.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: foreman
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -150,13 +150,14 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: 0.12.0
153
- description:
153
+ description:
154
154
  email:
155
155
  - dblock@dblock.org
156
156
  executables: []
157
157
  extensions: []
158
158
  extra_rdoc_files: []
159
159
  files:
160
+ - ".github/FUNDING.yml"
160
161
  - ".gitignore"
161
162
  - ".rspec"
162
163
  - ".rubocop.yml"
@@ -173,7 +174,7 @@ files:
173
174
  - RELEASING.md
174
175
  - Rakefile
175
176
  - UPGRADING.md
176
- - images/new.png
177
+ - images/create-app.png
177
178
  - images/slackbotserver.gif
178
179
  - images/slackbutton.gif
179
180
  - lib/slack-ruby-bot-server.rb
@@ -219,6 +220,7 @@ files:
219
220
  - public/index.html.erb
220
221
  - public/robots.txt
221
222
  - public/scripts/jquery-1.7.1.min.js
223
+ - public/scripts/messages.js
222
224
  - public/scripts/register.js
223
225
  - public/scripts/stats.js
224
226
  - public/scripts/url.min.js
@@ -263,7 +265,7 @@ files:
263
265
  homepage: https://github.com/slack-ruby/slack-ruby-bot-server
264
266
  licenses: []
265
267
  metadata: {}
266
- post_install_message:
268
+ post_install_message:
267
269
  rdoc_options: []
268
270
  require_paths:
269
271
  - lib
@@ -278,9 +280,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
278
280
  - !ruby/object:Gem::Version
279
281
  version: '0'
280
282
  requirements: []
281
- rubyforge_project:
282
- rubygems_version: 2.7.6
283
- signing_key:
283
+ rubygems_version: 3.1.3
284
+ signing_key:
284
285
  specification_version: 4
285
286
  summary: A Grape API serving a Slack bot to multiple teams.
286
287
  test_files: []
Binary file