grape-slack-bot 2.1.3 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +156 -4
- data/lib/slack_bot/api_client.rb +16 -2
- data/lib/slack_bot/callback.rb +12 -10
- data/lib/slack_bot/config.rb +33 -3
- data/lib/slack_bot/errors.rb +3 -0
- data/lib/slack_bot/grape_extension.rb +86 -17
- data/lib/slack_bot/grape_helpers/dispatch.rb +10 -5
- data/lib/slack_bot/grape_helpers/interactions.rb +67 -26
- data/lib/slack_bot/interaction.rb +11 -3
- data/lib/slack_bot/response_url.rb +43 -0
- data/lib/slack_bot/schema/block_actions.rb +48 -0
- data/lib/slack_bot/schema/handlers.rb +57 -0
- data/lib/slack_bot/schema/input_object.rb +35 -0
- data/lib/slack_bot/schema/registrar.rb +34 -0
- data/lib/slack_bot/schema/responses.rb +70 -0
- data/lib/slack_bot/schema/slack_responses.rb +80 -0
- data/lib/slack_bot/schema/slash_command_group.rb +16 -0
- data/lib/slack_bot/schema/view_owner.rb +55 -0
- data/lib/slack_bot/schema/view_submission.rb +50 -0
- data/lib/slack_bot/schema.rb +39 -0
- data/lib/slack_bot.rb +2 -1
- data/sig/slack_bot.rbs +3 -3
- data/sig/slack_bot_schema.rbs +126 -0
- metadata +14 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 10ab0a100f51e3a8567bc482bb415ef599b240f96855d75b6c73175dffc2360a
|
|
4
|
+
data.tar.gz: 9ef9129b1095492eaf6df6f1112fcc358b4319ddb1e1df29292ec96653cafe09
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f358aca3209d9811231508d1ee5d41d48e17fc24435a1cb439f8cd9f539f1906aabd9d5b265c2cb02cbdee4a386f27b210090ad6ebc71eba3a102e80b8ba50b9
|
|
7
|
+
data.tar.gz: 80410c0b845e92ed2394c0a9b4b41d16083e3a510164c6892967fa19f88d5d7d6cd7c7ab4b24fb978c155910ee2dd9681c44e9f2a82930e0757f5c8f5c41763a
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 3.0.1 (2026-11-19)
|
|
4
|
+
|
|
5
|
+
- Acknowledge Slack requests with a truly empty HTTP 200 body under Grape JSON format, so slash commands and interactions that open modals no longer show a blank ephemeral message.
|
|
6
|
+
|
|
7
|
+
## 3.0.0 (2026-11-18)
|
|
8
|
+
|
|
9
|
+
- Define modular Slack handlers with `SlackBot::Schema` and register them alongside existing Config handlers (RFC-0002).
|
|
10
|
+
- Accept Slack shortcuts, message actions, view closures, and POST requests to the Options Load URL. Keep GET options requests available.
|
|
11
|
+
- Declare modal and message replies with `respond_with`, including submission validation, modal pushes, and `response_url` replies.
|
|
12
|
+
- Add `ApiClient#views_push` and `ApiClient#response_url_post`.
|
|
13
|
+
- Read the interaction payload and workspace from the signed POST body.
|
|
14
|
+
- Keep request and callback contents out of development logs.
|
|
15
|
+
|
|
3
16
|
## 2.1.3 (2026-11-07)
|
|
4
17
|
|
|
5
18
|
- Memoize parsed JSON in `SlackBot::ApiResponse#data` so repeated reads do not re-parse the response body
|
data/README.md
CHANGED
|
@@ -105,6 +105,158 @@ In routes file `config/routes.rb` mount the API:
|
|
|
105
105
|
mount SlackBotApi => "/api/slack"
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
+
## Slack Schema
|
|
109
|
+
|
|
110
|
+
Use `SlackBot::Schema` to define handlers in separate classes and share
|
|
111
|
+
registrations across bots. Install it with `use` inside `Config.configure`.
|
|
112
|
+
Existing Config registrations and handlers using `call` can coexist with Schema
|
|
113
|
+
handlers.
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
class StartInput < SlackBot::Schema::InputObject
|
|
117
|
+
argument :name, String, required: true
|
|
118
|
+
argument :rounds, Integer, default: 1
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
class StartGame < SlackBot::Schema::SlashCommand
|
|
122
|
+
input StartInput
|
|
123
|
+
|
|
124
|
+
def resolve(name:, rounds:)
|
|
125
|
+
{response_type: "ephemeral", text: "#{name}: #{rounds} rounds"}
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
class GameMenu < SlackBot::Schema::SlashCommand
|
|
130
|
+
def resolve
|
|
131
|
+
{response_type: "ephemeral", text: "Try /game start name=quiz&rounds=3"}
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
class GameCommands < SlackBot::Schema::SlashCommandGroup
|
|
136
|
+
default_command GameMenu
|
|
137
|
+
command :start, StartGame
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
class GameSchema < SlackBot::Schema
|
|
141
|
+
slash_command :game, GameCommands
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
class BotSchema < SlackBot::Schema
|
|
145
|
+
mount GameSchema
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
SlackBot::Config.configure do
|
|
149
|
+
use BotSchema
|
|
150
|
+
end
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Input objects read the command's existing query-string format. Arguments
|
|
154
|
+
can use `String`, `Integer`, `Float`, or a callable that receives the raw
|
|
155
|
+
value. Declare `required: true` for required arguments and `default:` for a
|
|
156
|
+
typed default. Slash handlers inherit the existing user, workspace, and
|
|
157
|
+
direct-message checks.
|
|
158
|
+
|
|
159
|
+
### Supported Slack HTTP requests
|
|
160
|
+
|
|
161
|
+
```mermaid
|
|
162
|
+
flowchart TB
|
|
163
|
+
Commands["POST commands/:url_token"] --> Slash["Slash command"]
|
|
164
|
+
Events["POST events"] --> Event["event_callback / url_verification"]
|
|
165
|
+
Interactions["POST interactions"] --> Blocks["block_actions"]
|
|
166
|
+
Interactions --> Submission["view_submission"]
|
|
167
|
+
Interactions --> Closed["view_closed"]
|
|
168
|
+
Interactions --> Shortcut["shortcut"]
|
|
169
|
+
Interactions --> Message["message_actions / message_action"]
|
|
170
|
+
Options["POST menu_options"] --> Suggestion["block_suggestion"]
|
|
171
|
+
Legacy["GET menu_options"] --> Suggestion
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
| Payload | Registration | Schema handler |
|
|
175
|
+
| --- | --- | --- |
|
|
176
|
+
| Slash command | `slash_command :url_token, Handler` | `SlashCommand` or `SlashCommandGroup` |
|
|
177
|
+
| `event_callback` | `event :type, Handler` | `Event` |
|
|
178
|
+
| `url_verification` | Handled by the Events endpoint | |
|
|
179
|
+
| `block_actions` | `block_actions :action_id, Handler` for messages and unscoped App Home views. Declare `block_actions Handler` on a scoped view owner. | `BlockActions` |
|
|
180
|
+
| `view_submission` | Declare `view_submission Handler` on the view owner | `ViewSubmission` |
|
|
181
|
+
| `view_closed` | Declare `view_closed Handler` on the owner or register `view_closed :callback_id, Handler` for externally opened views | `ViewClosed` |
|
|
182
|
+
| `shortcut` | `shortcut :callback_id, Handler` | `Shortcut` |
|
|
183
|
+
| `message_action` or `message_actions` | `message_action :callback_id, Handler` | `MessageAction` |
|
|
184
|
+
| `block_suggestion` | `block_suggestion :action_id, Handler` or `options :action_id, Handler` | `Options` |
|
|
185
|
+
|
|
186
|
+
Set Slack's **Options Load URL** to the existing mounted `/menu_options`
|
|
187
|
+
endpoint. Slack sends a form-encoded POST with a `payload` containing
|
|
188
|
+
`type: "block_suggestion"`, `action_id`, and `value`. Return `{options: [...]}`
|
|
189
|
+
or `{option_groups: [...]}`. Existing `menu_options` registrations continue
|
|
190
|
+
to accept GET calls with `action_id` and `value` in the query string.
|
|
191
|
+
See the [Slack interaction payload reference](https://docs.slack.dev/reference/interaction-payloads/)
|
|
192
|
+
and [Options Load payload reference](https://docs.slack.dev/reference/interaction-payloads/block_suggestion-payload/).
|
|
193
|
+
|
|
194
|
+
### Actions and responses
|
|
195
|
+
|
|
196
|
+
Declare `view ViewClass` on a handler to render views. A slash command,
|
|
197
|
+
event, shortcut, or block action can declare the view's `block_actions`,
|
|
198
|
+
`view_submission`, and `view_closed` handlers. Set `notify_on_close: true`
|
|
199
|
+
in a modal to receive a close payload. Configure `callback_storage` and
|
|
200
|
+
`user_session_resolver` for scoped views, and declare named owner classes so
|
|
201
|
+
their callbacks can route after a process restart.
|
|
202
|
+
|
|
203
|
+
```ruby
|
|
204
|
+
class EditSubmission < SlackBot::Schema::ViewSubmission
|
|
205
|
+
respond_with response_action: :clear
|
|
206
|
+
|
|
207
|
+
def resolve
|
|
208
|
+
name = view_state(:name_block, :name_input)
|
|
209
|
+
return {errors: {name_block: "Enter a name"}} if name.to_s.empty?
|
|
210
|
+
|
|
211
|
+
nil
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
class EditShortcut < SlackBot::Schema::Shortcut
|
|
216
|
+
view TicketView
|
|
217
|
+
view_submission EditSubmission
|
|
218
|
+
respond_with open_modal: :edit_modal
|
|
219
|
+
end
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
| Declaration | Result |
|
|
223
|
+
| --- | --- |
|
|
224
|
+
| `respond_with open_modal: :edit_modal` | Call `views.open` using the trigger |
|
|
225
|
+
| `respond_with update_modal: :edit_modal` | Call `views.update` using the current view ID and its hash when supplied |
|
|
226
|
+
| `respond_with push_modal: :details_modal` | Call `views.push` using the trigger |
|
|
227
|
+
| `respond_with publish_view: :home` | Call `views.publish` for the Slack user |
|
|
228
|
+
| `respond_with response_action: :update, view: :edit_modal` | Return replacement view JSON on submission |
|
|
229
|
+
| `respond_with response_action: :push, view: :details_modal` | Return a new view on submission |
|
|
230
|
+
| `respond_with response_action: :clear` | Clear the modal stack on submission |
|
|
231
|
+
| `respond_with response_action: :errors` | Use the resolver's `{errors: {block_id => message}}` for field errors |
|
|
232
|
+
| `respond_with response_url: :ephemeral, text: "Saved", blocks: :confirmation` | Send text and rendered message blocks to Slack |
|
|
233
|
+
| `respond_with response_url: :in_channel, replace_original: true, text: "Saved"` | Replace the source message |
|
|
234
|
+
| `respond_with delete_original: true` | Delete the source message |
|
|
235
|
+
| `respond_with :ack` or `respond_with :none` | Acknowledge, or let the resolver return its own response |
|
|
236
|
+
|
|
237
|
+
A hash from `resolve` overrides a declaration. In a submission, an
|
|
238
|
+
`errors:` hash becomes a Slack validation response. Return `:halt` to
|
|
239
|
+
acknowledge immediately and skip the declared response. A submission with
|
|
240
|
+
one `response_urls` entry can use it for declarative replies. For multiple
|
|
241
|
+
entries, choose the destination in `resolve` and use
|
|
242
|
+
`SlackBot::ApiClient.response_url_post(url, **message)`. The instance
|
|
243
|
+
method of the same name is also available.
|
|
244
|
+
|
|
245
|
+
On a `BlockActions` handler, `action :pick, args: {user_id: :selected_user}`
|
|
246
|
+
calls `pick(user_id:)` when Slack sends that action ID. Use `with: :method`
|
|
247
|
+
to select another method, or `with: ServiceClass` to instantiate it with
|
|
248
|
+
the extracted keyword arguments and call it. Extractors can use Slack
|
|
249
|
+
field names, `:selected_option_value`, `:selected_option_values`, or a
|
|
250
|
+
callable that receives the action hash. Override `resolve` to control
|
|
251
|
+
dispatch explicitly and call `super` to run the declared actions.
|
|
252
|
+
On a submission, `view_state` returns the full state map, one block's
|
|
253
|
+
state, or the submitted value at a block and action ID.
|
|
254
|
+
|
|
255
|
+
See [Slack acknowledgements and message responses](https://docs.slack.dev/interactivity/handling-user-interaction/)
|
|
256
|
+
and [Slack modals](https://docs.slack.dev/surfaces/modals/) for response
|
|
257
|
+
mechanisms and timing. See [RFC-0002](rfcs/0002-slack-schema.md) for the
|
|
258
|
+
Schema contract and scope.
|
|
259
|
+
|
|
108
260
|
## Concepts
|
|
109
261
|
|
|
110
262
|
### Slash command
|
|
@@ -206,7 +358,7 @@ features:
|
|
|
206
358
|
always_online: true
|
|
207
359
|
slash_commands:
|
|
208
360
|
- command: /game
|
|
209
|
-
url: https://
|
|
361
|
+
url: https://bot.rails.fi/api/slack/commands/game
|
|
210
362
|
description: The game
|
|
211
363
|
should_escape: false
|
|
212
364
|
oauth_config:
|
|
@@ -229,7 +381,7 @@ oauth_config:
|
|
|
229
381
|
- commands
|
|
230
382
|
settings:
|
|
231
383
|
event_subscriptions:
|
|
232
|
-
request_url: https://
|
|
384
|
+
request_url: https://bot.rails.fi/api/slack/events
|
|
233
385
|
bot_events:
|
|
234
386
|
- app_home_opened
|
|
235
387
|
- app_mention
|
|
@@ -242,8 +394,8 @@ settings:
|
|
|
242
394
|
- reaction_removed
|
|
243
395
|
interactivity:
|
|
244
396
|
is_enabled: true
|
|
245
|
-
request_url: https://
|
|
246
|
-
message_menu_options_url: https://
|
|
397
|
+
request_url: https://bot.rails.fi/api/slack/interactions
|
|
398
|
+
message_menu_options_url: https://bot.rails.fi/api/slack/menu_options
|
|
247
399
|
org_deploy_enabled: false
|
|
248
400
|
socket_mode_enabled: false
|
|
249
401
|
token_rotation_enabled: false
|
data/lib/slack_bot/api_client.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require "faraday"
|
|
2
2
|
require "json"
|
|
3
|
+
require "slack_bot/response_url"
|
|
3
4
|
|
|
4
5
|
module SlackBot
|
|
5
6
|
class ApiResponse
|
|
@@ -112,8 +113,21 @@ module SlackBot
|
|
|
112
113
|
perform_request { client.post("views.open", JSON.generate(trigger_id: trigger_id, view: view)) }
|
|
113
114
|
end
|
|
114
115
|
|
|
115
|
-
def
|
|
116
|
-
perform_request { client.post("views.
|
|
116
|
+
def views_push(trigger_id:, view:)
|
|
117
|
+
perform_request { client.post("views.push", JSON.generate(trigger_id: trigger_id, view: view)) }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def self.response_url_post(url, **message)
|
|
121
|
+
ResponseUrl.post(url, message)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def response_url_post(url, **message)
|
|
125
|
+
self.class.response_url_post(url, **message)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def views_update(view_id:, view:, hash: nil)
|
|
129
|
+
payload = compact_payload(view_id: view_id, view: view, hash: hash)
|
|
130
|
+
perform_request { client.post("views.update", JSON.generate(payload)) }
|
|
117
131
|
end
|
|
118
132
|
|
|
119
133
|
def chat_post_message(channel:, text:, blocks:)
|
data/lib/slack_bot/callback.rb
CHANGED
|
@@ -41,6 +41,7 @@ module SlackBot
|
|
|
41
41
|
attr_accessor :id
|
|
42
42
|
def initialize(id: nil, class_name: nil, user: nil, channel_id: nil, payload: nil, config: nil, expires_in: nil, user_scope: nil, view_id: nil)
|
|
43
43
|
@id = id
|
|
44
|
+
@user = user
|
|
44
45
|
@data = {
|
|
45
46
|
class_name: class_name,
|
|
46
47
|
user_id: user&.id,
|
|
@@ -56,7 +57,7 @@ module SlackBot
|
|
|
56
57
|
|
|
57
58
|
def reload
|
|
58
59
|
cached_data = read_data
|
|
59
|
-
SlackBot::DevConsole.log_check("
|
|
60
|
+
SlackBot::DevConsole.log_check("Read Slack callback")
|
|
60
61
|
raise SlackBot::Errors::CallbackNotFound if cached_data.nil?
|
|
61
62
|
|
|
62
63
|
@data = cached_data
|
|
@@ -68,7 +69,7 @@ module SlackBot
|
|
|
68
69
|
@id = generate_id if id.blank?
|
|
69
70
|
serialize_args
|
|
70
71
|
|
|
71
|
-
SlackBot::DevConsole.log_check("
|
|
72
|
+
SlackBot::DevConsole.log_check("Write Slack callback")
|
|
72
73
|
write_data(data)
|
|
73
74
|
end
|
|
74
75
|
|
|
@@ -92,10 +93,10 @@ module SlackBot
|
|
|
92
93
|
end
|
|
93
94
|
|
|
94
95
|
def user
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
return if user_id.blank?
|
|
97
|
+
return @user if @user&.id == user_id
|
|
98
|
+
|
|
99
|
+
@user = config.callback_user_finder!.call(user_id)
|
|
99
100
|
end
|
|
100
101
|
|
|
101
102
|
def user=(user)
|
|
@@ -199,10 +200,11 @@ module SlackBot
|
|
|
199
200
|
config.callback_storage!.read(storage_key)
|
|
200
201
|
end
|
|
201
202
|
|
|
202
|
-
def write_data(data
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
def write_data(data)
|
|
204
|
+
storage = config.callback_storage!
|
|
205
|
+
saved = storage.write(storage_key, data, expires_in: expires_in)
|
|
206
|
+
storage.write(view_storage_key, id, expires_in: expires_in) if saved != false && view_id.present?
|
|
207
|
+
saved
|
|
206
208
|
end
|
|
207
209
|
|
|
208
210
|
def delete_data
|
data/lib/slack_bot/config.rb
CHANGED
|
@@ -56,6 +56,11 @@ module SlackBot
|
|
|
56
56
|
@event_dispatcher_method = method_lambda
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
def use(schema)
|
|
60
|
+
Schema::Registrar.new(self).register(schema)
|
|
61
|
+
end
|
|
62
|
+
alias_method :mount, :use
|
|
63
|
+
|
|
59
64
|
def interaction(interaction_klass, handler_name: nil)
|
|
60
65
|
handler_name ||= interaction_klass.name
|
|
61
66
|
handler_class(handler_name, interaction_klass)
|
|
@@ -99,7 +104,7 @@ module SlackBot
|
|
|
99
104
|
endpoint_config = slash_command_endpoints[url_token.to_sym]
|
|
100
105
|
return if endpoint_config.blank?
|
|
101
106
|
|
|
102
|
-
endpoint_config.find_command_config(text) || endpoint_config
|
|
107
|
+
endpoint_config.find_command_config(text.to_s) || endpoint_config
|
|
103
108
|
end
|
|
104
109
|
|
|
105
110
|
def menu_options(action_id, klass)
|
|
@@ -108,8 +113,25 @@ module SlackBot
|
|
|
108
113
|
end
|
|
109
114
|
|
|
110
115
|
def find_menu_options(action_id)
|
|
111
|
-
@menu_options
|
|
112
|
-
|
|
116
|
+
@menu_options&.[](action_id&.to_sym)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
alias_method :block_suggestion, :menu_options
|
|
120
|
+
|
|
121
|
+
def shortcut(callback_id, klass)
|
|
122
|
+
register_interaction_callback("shortcut", callback_id, klass)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def message_action(callback_id, klass)
|
|
126
|
+
register_interaction_callback("message_action", callback_id, klass)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def view_closed(callback_id, klass)
|
|
130
|
+
register_interaction_callback("view_closed", callback_id, klass)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def find_interaction_callback(type, callback_id)
|
|
134
|
+
@interaction_callbacks&.dig(type, callback_id.to_s)
|
|
113
135
|
end
|
|
114
136
|
|
|
115
137
|
def block_action(action_id, interaction_klass, handler_name: nil)
|
|
@@ -138,6 +160,14 @@ module SlackBot
|
|
|
138
160
|
rescue KeyError
|
|
139
161
|
raise SlackBot::Errors::HandlerClassNotFound.new(class_name, handler_classes: @handler_classes)
|
|
140
162
|
end
|
|
163
|
+
|
|
164
|
+
private
|
|
165
|
+
|
|
166
|
+
def register_interaction_callback(type, callback_id, klass)
|
|
167
|
+
@interaction_callbacks ||= {}
|
|
168
|
+
(@interaction_callbacks[type] ||= {})[callback_id.to_s] = klass
|
|
169
|
+
interaction(klass)
|
|
170
|
+
end
|
|
141
171
|
end
|
|
142
172
|
|
|
143
173
|
class SlashCommandEndpointConfig
|
data/lib/slack_bot/errors.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require "active_support"
|
|
2
4
|
require "active_support/core_ext/object"
|
|
3
5
|
require "active_support/security_utils"
|
|
@@ -14,6 +16,9 @@ module SlackBot
|
|
|
14
16
|
TIMESTAMP_TOLERANCE_SECONDS = 300
|
|
15
17
|
# Minimum length for Slack signing secret (Slack's requirement)
|
|
16
18
|
MIN_SIGNING_SECRET_LENGTH = 32
|
|
19
|
+
PAYLOAD_OBJECTS = %w[team user view].freeze
|
|
20
|
+
ROUTE_IDENTIFIERS = %w[callback_id action_id].freeze
|
|
21
|
+
NEW_INTERACTION_TYPES = %w[shortcut message_actions message_action view_closed].freeze
|
|
17
22
|
|
|
18
23
|
def fetch_team_id
|
|
19
24
|
params.dig("team_id") || params.dig("team", "id")
|
|
@@ -23,6 +28,10 @@ module SlackBot
|
|
|
23
28
|
params.dig("user_id") || params.dig("user", "id") || params.dig("event", "user")
|
|
24
29
|
end
|
|
25
30
|
|
|
31
|
+
def current_user
|
|
32
|
+
resolve_user_session(fetch_team_id, fetch_user_id)&.user
|
|
33
|
+
end
|
|
34
|
+
|
|
26
35
|
def verify_slack_signature!
|
|
27
36
|
remove_instance_variable(:@request_body_content) if instance_variable_defined?(:@request_body_content)
|
|
28
37
|
|
|
@@ -74,7 +83,7 @@ module SlackBot
|
|
|
74
83
|
end
|
|
75
84
|
|
|
76
85
|
def url_verification(params)
|
|
77
|
-
SlackBot::DevConsole.log_input "
|
|
86
|
+
SlackBot::DevConsole.log_input "Slack URL verification"
|
|
78
87
|
{challenge: params[:challenge]}
|
|
79
88
|
end
|
|
80
89
|
|
|
@@ -127,16 +136,68 @@ module SlackBot
|
|
|
127
136
|
end
|
|
128
137
|
|
|
129
138
|
def parse_interaction_payload!(raw_payload)
|
|
130
|
-
JSON.parse(raw_payload)
|
|
131
|
-
|
|
132
|
-
|
|
139
|
+
payload = JSON.parse(raw_payload) if raw_payload.is_a?(String)
|
|
140
|
+
raise SlackBot::Errors::InvalidPayloadError.new("Invalid JSON payload: expected an object") unless payload.is_a?(Hash)
|
|
141
|
+
|
|
142
|
+
validate_interaction_shape!(payload)
|
|
143
|
+
payload
|
|
144
|
+
rescue JSON::ParserError
|
|
145
|
+
raise SlackBot::Errors::InvalidPayloadError.new("Invalid JSON payload"), cause: nil
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def validate_interaction_shape!(payload)
|
|
149
|
+
return if PAYLOAD_OBJECTS.all? { |field| payload[field].nil? || payload[field].is_a?(Hash) } &&
|
|
150
|
+
ROUTE_IDENTIFIERS.all? { |field| payload[field].nil? || payload[field].is_a?(String) } &&
|
|
151
|
+
valid_action_list?(payload["actions"])
|
|
152
|
+
|
|
153
|
+
raise SlackBot::Errors::InvalidPayloadError.new("Invalid Slack payload fields")
|
|
133
154
|
end
|
|
134
155
|
|
|
135
|
-
def
|
|
136
|
-
|
|
156
|
+
def valid_action_list?(actions)
|
|
157
|
+
actions.nil? || (actions.is_a?(Array) && actions.all? do |action|
|
|
158
|
+
action.is_a?(Hash) && (action["action_id"].nil? || action["action_id"].is_a?(String))
|
|
159
|
+
end)
|
|
137
160
|
end
|
|
138
161
|
|
|
162
|
+
def authorize_interaction_team!(payload)
|
|
163
|
+
team_id = interaction_team_id(payload)
|
|
164
|
+
raise SlackBot::Errors::TeamAuthenticationError.new("Team is not authorized") if team_id.blank?
|
|
165
|
+
|
|
166
|
+
verify_slack_team!(team_id)
|
|
167
|
+
team_id
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def resolve_action_user(payload, team_id)
|
|
171
|
+
resolve_user_session(team_id, payload.dig("user", "id"))
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def load_menu_options(options_params, user)
|
|
175
|
+
klass = slack_bot_config.find_menu_options(options_params[:action_id])
|
|
176
|
+
raise SlackBot::Errors::MenuOptionsNotImplemented.new if klass.blank?
|
|
177
|
+
|
|
178
|
+
klass.new(current_user: user, params: options_params, config: slack_bot_config).call
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def empty_slack_ack!
|
|
182
|
+
blank_slack_response!
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def invoke_slack_handler(handler)
|
|
186
|
+
result = handler.call
|
|
187
|
+
if handler.is_a?(Schema::Responses)
|
|
188
|
+
return empty_slack_ack! if result.blank?
|
|
189
|
+
|
|
190
|
+
status 200
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
result
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Slack acknowledges with HTTP 200 and an empty body. Under `format :json`,
|
|
197
|
+
# Grape's `body false` becomes an empty string that the JSON formatter
|
|
198
|
+
# encodes as `""`, which Slack shows as an empty ephemeral message.
|
|
139
199
|
def blank_slack_response!
|
|
200
|
+
api_format :txt
|
|
140
201
|
body false
|
|
141
202
|
status 200
|
|
142
203
|
end
|
|
@@ -174,15 +235,15 @@ module SlackBot
|
|
|
174
235
|
command_klass = command_config&.command_klass
|
|
175
236
|
raise SlackBot::Errors::SlashCommandNotImplemented.new if command_klass.blank?
|
|
176
237
|
|
|
177
|
-
args = params[:text].
|
|
178
|
-
SlackBot::DevConsole.log_input "
|
|
238
|
+
args = params[:text].to_s.sub(/\A#{Regexp.escape(command_config.full_token)}\s?/, "")
|
|
239
|
+
SlackBot::DevConsole.log_input "Slack slash command"
|
|
179
240
|
|
|
180
241
|
action = command_klass.new(current_user: current_user, params: params, args: args, config: slack_bot_config)
|
|
181
242
|
verify_slack_team! if action.only_slack_team?
|
|
182
243
|
verify_direct_message_channel! if action.only_direct_message?
|
|
183
244
|
verify_current_user! if action.only_user?
|
|
184
245
|
|
|
185
|
-
result = action
|
|
246
|
+
result = invoke_slack_handler(action)
|
|
186
247
|
return blank_slack_response! unless result
|
|
187
248
|
|
|
188
249
|
result
|
|
@@ -193,12 +254,13 @@ module SlackBot
|
|
|
193
254
|
def self.add_interactions_resource!(base)
|
|
194
255
|
base.resource :interactions do
|
|
195
256
|
post do
|
|
196
|
-
payload = parse_interaction_payload!(
|
|
197
|
-
|
|
198
|
-
action_user = resolve_action_user(payload)&.user
|
|
257
|
+
payload = parse_interaction_payload!(request.POST["payload"])
|
|
258
|
+
team_id = authorize_interaction_team!(payload)
|
|
259
|
+
action_user = resolve_action_user(payload, team_id)&.user
|
|
199
260
|
|
|
200
261
|
result = route_interaction(payload: payload, user: action_user, params: params)
|
|
201
262
|
|
|
263
|
+
status 200 if GrapeHelpers::NEW_INTERACTION_TYPES.include?(payload["type"])
|
|
202
264
|
return blank_slack_response! if result.blank? || result == false
|
|
203
265
|
|
|
204
266
|
result
|
|
@@ -230,17 +292,24 @@ module SlackBot
|
|
|
230
292
|
def self.add_menu_options_resource!(base)
|
|
231
293
|
base.resource :menu_options do
|
|
232
294
|
get do
|
|
233
|
-
SlackBot::DevConsole.log_input "
|
|
295
|
+
SlackBot::DevConsole.log_input "Slack menu options"
|
|
234
296
|
|
|
235
297
|
verify_slack_team!
|
|
236
|
-
|
|
237
|
-
raise SlackBot::Errors::MenuOptionsNotImplemented.new if menu_options_klass.blank?
|
|
238
|
-
|
|
239
|
-
menu_options = menu_options_klass.new(current_user: current_user, params: params, config: slack_bot_config).call
|
|
298
|
+
menu_options = load_menu_options(params, current_user)
|
|
240
299
|
return blank_slack_response! if menu_options.blank?
|
|
241
300
|
|
|
242
301
|
menu_options
|
|
243
302
|
end
|
|
303
|
+
|
|
304
|
+
post do
|
|
305
|
+
payload = parse_interaction_payload!(request.POST["payload"])
|
|
306
|
+
team_id = authorize_interaction_team!(payload)
|
|
307
|
+
raise SlackBot::Errors::UnknownActionTypeError.new(payload["type"]) unless payload["type"] == "block_suggestion"
|
|
308
|
+
|
|
309
|
+
user = resolve_action_user(payload, team_id)&.user
|
|
310
|
+
status 200
|
|
311
|
+
load_menu_options(payload.with_indifferent_access, user).presence || {options: []}
|
|
312
|
+
end
|
|
244
313
|
end
|
|
245
314
|
end
|
|
246
315
|
end
|
|
@@ -8,7 +8,7 @@ module SlackBot
|
|
|
8
8
|
return false if event.blank?
|
|
9
9
|
return false if bot_message_event?(event)
|
|
10
10
|
|
|
11
|
-
SlackBot::DevConsole.log_input "
|
|
11
|
+
SlackBot::DevConsole.log_input "Slack event callback"
|
|
12
12
|
handler = event_handler_for(params)
|
|
13
13
|
return false if handler.blank?
|
|
14
14
|
|
|
@@ -40,14 +40,19 @@ module SlackBot
|
|
|
40
40
|
|
|
41
41
|
def event_handler_for(params)
|
|
42
42
|
event_type = params[:event][:type] || params[:event]["type"]
|
|
43
|
-
slack_bot_config.find_event_handler(event_type.
|
|
43
|
+
slack_bot_config.find_event_handler(event_type) if event_type.is_a?(String) || event_type.is_a?(Symbol)
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def run_event_handler(handler, params)
|
|
47
47
|
current_user = resolve_event_user(params)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
if slack_bot_config.event_dispatcher_method
|
|
49
|
+
result = dispatch_event(handler: handler, params: params, current_user: current_user)
|
|
50
|
+
return (handler <= Schema::Event) ? empty_slack_ack! : result
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
arguments = {params: params, current_user: current_user}
|
|
54
|
+
arguments[:config] = slack_bot_config if handler <= Schema::Event
|
|
55
|
+
invoke_slack_handler(handler.new(**arguments))
|
|
51
56
|
end
|
|
52
57
|
end
|
|
53
58
|
end
|