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
|
@@ -1,18 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module SlackBot
|
|
2
4
|
module GrapeHelpers
|
|
3
5
|
module Interactions
|
|
4
6
|
def route_interaction(payload:, user:, params:)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
type = payload["type"]
|
|
8
|
+
case type
|
|
9
|
+
when "view_submission", "view_closed"
|
|
10
|
+
route_view(payload, user, params, type)
|
|
8
11
|
when "block_actions"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
route_block_actions(payload, user, params)
|
|
13
|
+
when "shortcut"
|
|
14
|
+
run_registered_interaction(type, payload["callback_id"], user, params, payload)
|
|
15
|
+
when "message_actions", "message_action"
|
|
16
|
+
run_registered_interaction("message_action", payload["callback_id"], user, params, payload)
|
|
14
17
|
else
|
|
15
|
-
raise SlackBot::Errors::UnknownActionTypeError.new(
|
|
18
|
+
raise SlackBot::Errors::UnknownActionTypeError.new(type)
|
|
16
19
|
end
|
|
17
20
|
end
|
|
18
21
|
|
|
@@ -23,42 +26,80 @@ module SlackBot
|
|
|
23
26
|
interaction_klass = slack_bot_config.find_block_action(action_id)
|
|
24
27
|
raise SlackBot::Errors::BlockActionNotImplemented.new if interaction_klass.blank?
|
|
25
28
|
|
|
26
|
-
interaction_klass.new(current_user: user, params: params, config: slack_bot_config)
|
|
29
|
+
handler = interaction_klass.new(current_user: user, params: params, config: slack_bot_config)
|
|
30
|
+
invoke_interaction(handler, payload)
|
|
27
31
|
end
|
|
28
32
|
|
|
29
33
|
def validate_callback_user!(callback, user)
|
|
30
|
-
if callback.user_id != user.id
|
|
34
|
+
if user.nil? || callback.user_id != user.id
|
|
31
35
|
raise SlackBot::Errors::CallbackUserMismatchError.new("Callback user is not equal to action user")
|
|
32
36
|
end
|
|
33
37
|
end
|
|
34
38
|
|
|
35
|
-
def handle_block_actions_view(view:, user:, params:)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
def handle_block_actions_view(view:, user:, params:, type: "block_actions", payload: nil)
|
|
40
|
+
callback_id = view&.dig("callback_id")
|
|
41
|
+
callback = find_view_callback(view, user, type)
|
|
42
|
+
if callback.nil?
|
|
43
|
+
return run_registered_interaction(type, callback_id, user, params, payload) if type == "view_closed"
|
|
44
|
+
|
|
45
|
+
raise SlackBot::Errors::CallbackNotFound
|
|
46
|
+
end
|
|
39
47
|
|
|
40
|
-
|
|
48
|
+
validate_callback_user!(callback, user)
|
|
49
|
+
interaction_klass = callback_interaction_klass(callback, type)
|
|
41
50
|
return false if interaction_klass.blank?
|
|
42
51
|
|
|
43
|
-
interaction_klass.new(current_user: user, params: params, callback: callback, config: slack_bot_config)
|
|
52
|
+
handler = interaction_klass.new(current_user: user, params: params, callback: callback, config: slack_bot_config)
|
|
53
|
+
invoke_interaction(handler, payload)
|
|
44
54
|
end
|
|
45
55
|
|
|
46
56
|
private
|
|
47
57
|
|
|
48
|
-
def
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
def find_view_callback(view, user, type)
|
|
59
|
+
return if user.nil?
|
|
60
|
+
return if type == "view_closed" && slack_bot_config.callback_storage_instance.nil?
|
|
61
|
+
|
|
62
|
+
callback_id = view&.dig("callback_id")
|
|
63
|
+
if callback_id.present?
|
|
64
|
+
SlackBot::Callback.find(callback_id, user: user, config: slack_bot_config)
|
|
65
|
+
elsif view&.dig("id").present?
|
|
66
|
+
SlackBot::Callback.find_by_view_id(view["id"], user: user, config: slack_bot_config)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def route_block_actions(payload, user, params)
|
|
71
|
+
view = payload["view"]
|
|
72
|
+
if view.blank? || (view["type"] == "home" && view["callback_id"].blank?)
|
|
73
|
+
handle_block_actions_message(payload: payload, user: user, params: params)
|
|
74
|
+
else
|
|
75
|
+
route_view(payload, user, params, "block_actions")
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def route_view(payload, user, params, type)
|
|
80
|
+
handle_block_actions_view(view: payload["view"], user: user, params: params, type: type, payload: payload)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def run_registered_interaction(type, callback_id, user, params, payload)
|
|
84
|
+
klass = slack_bot_config.find_interaction_callback(type, callback_id)
|
|
85
|
+
return empty_slack_ack! unless klass
|
|
51
86
|
|
|
52
|
-
|
|
87
|
+
handler = klass.new(current_user: user, params: params, config: slack_bot_config)
|
|
88
|
+
invoke_interaction(handler, payload).presence || empty_slack_ack!
|
|
53
89
|
end
|
|
54
90
|
|
|
55
|
-
def
|
|
56
|
-
|
|
91
|
+
def invoke_interaction(handler, payload)
|
|
92
|
+
handler.payload = payload if payload && handler.is_a?(SlackBot::Interaction)
|
|
93
|
+
invoke_slack_handler(handler)
|
|
57
94
|
end
|
|
58
95
|
|
|
59
|
-
def callback_interaction_klass(callback)
|
|
60
|
-
|
|
61
|
-
|
|
96
|
+
def callback_interaction_klass(callback, type)
|
|
97
|
+
owner = callback.handler_class
|
|
98
|
+
if owner.respond_to?(:interaction_handlers)
|
|
99
|
+
owner.interaction_handlers[type]
|
|
100
|
+
elsif type != "view_closed" && owner.respond_to?(:interaction_klass)
|
|
101
|
+
owner.interaction_klass
|
|
102
|
+
end
|
|
62
103
|
end
|
|
63
104
|
end
|
|
64
105
|
end
|
|
@@ -23,9 +23,15 @@ module SlackBot
|
|
|
23
23
|
build_view_reply(response: response, callback: callback, payload: view, error_class: SlackBot::Errors::OpenModalError)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def self.
|
|
26
|
+
def self.push_modal(callback:, trigger_id:, view:)
|
|
27
27
|
view = modal_payload(callback, view)
|
|
28
|
-
response = api_client.
|
|
28
|
+
response = api_client.views_push(trigger_id: trigger_id, view: view)
|
|
29
|
+
build_view_reply(response: response, callback: callback, payload: view, error_class: SlackBot::Errors::PushModalError)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.update_modal(callback:, view_id:, view:, **options)
|
|
33
|
+
view = modal_payload(callback, view)
|
|
34
|
+
response = api_client.views_update(view_id: view_id, view: view, **options)
|
|
29
35
|
build_view_reply(response: response, callback: callback, payload: view, error_class: SlackBot::Errors::UpdateModalError)
|
|
30
36
|
end
|
|
31
37
|
|
|
@@ -35,6 +41,7 @@ module SlackBot
|
|
|
35
41
|
end
|
|
36
42
|
|
|
37
43
|
attr_reader :current_user, :params, :callback, :config
|
|
44
|
+
attr_writer :payload
|
|
38
45
|
def initialize(current_user: nil, params: nil, callback: nil, config: nil)
|
|
39
46
|
@current_user = current_user
|
|
40
47
|
@params = params
|
|
@@ -47,7 +54,8 @@ module SlackBot
|
|
|
47
54
|
end
|
|
48
55
|
|
|
49
56
|
def self.modal_payload(callback, view)
|
|
50
|
-
view.merge(type: "modal"
|
|
57
|
+
modal = view.except("type").merge(type: "modal")
|
|
58
|
+
callback ? modal.except("callback_id").merge(callback_id: callback.id) : modal
|
|
51
59
|
end
|
|
52
60
|
|
|
53
61
|
def self.publish_payload(callback, metadata, view)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "uri"
|
|
2
|
+
|
|
3
|
+
module SlackBot
|
|
4
|
+
module ResponseUrl
|
|
5
|
+
HOSTS = %w[hooks.slack.com hooks.slack-gov.com].freeze
|
|
6
|
+
PATH = %r{\A/(?:actions|services|app(?:-actions)?)/[a-zA-Z0-9_/-]+\z}
|
|
7
|
+
|
|
8
|
+
Reply = Struct.new(:status) do
|
|
9
|
+
def ok?
|
|
10
|
+
(200...300).cover?(status)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def post(url, message)
|
|
17
|
+
destination = validate(url)
|
|
18
|
+
response = client.post(destination, JSON.generate(message))
|
|
19
|
+
Reply.new(response.status)
|
|
20
|
+
rescue Faraday::Error
|
|
21
|
+
raise Errors::SlackApiError.new("Slack response URL request failed"), cause: nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def validate(url)
|
|
25
|
+
uri = URI.parse(url) if url.is_a?(String)
|
|
26
|
+
return uri if uri.is_a?(URI::HTTPS) && valid_destination?(uri)
|
|
27
|
+
|
|
28
|
+
raise Errors::SlackApiError.new("Invalid Slack response URL")
|
|
29
|
+
rescue URI::InvalidURIError
|
|
30
|
+
raise Errors::SlackApiError.new("Invalid Slack response URL"), cause: nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def valid_destination?(uri)
|
|
34
|
+
HOSTS.include?(uri.host) && uri.port == 443 && PATH.match?(uri.path) &&
|
|
35
|
+
uri.userinfo.nil? && uri.query.nil? && uri.fragment.nil?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def client
|
|
39
|
+
Faraday.new(headers: {"Content-Type" => "application/json; charset=utf-8"},
|
|
40
|
+
request: {timeout: 2, open_timeout: 1})
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SlackBot
|
|
4
|
+
class Schema
|
|
5
|
+
class BlockActions < Interaction
|
|
6
|
+
class_attribute :action_handlers, instance_accessor: false, default: {}.freeze
|
|
7
|
+
|
|
8
|
+
def self.action(action_id, args: {}, with: action_id.to_sym)
|
|
9
|
+
self.action_handlers = action_handlers.merge(action_id.to_s => [with, args.dup.freeze].freeze).freeze
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def resolve
|
|
13
|
+
handlers = self.class.action_handlers
|
|
14
|
+
return if handlers.empty?
|
|
15
|
+
|
|
16
|
+
matched = false
|
|
17
|
+
actions&.each do |action|
|
|
18
|
+
destination, extractors = handlers[action["action_id"]]
|
|
19
|
+
next unless destination
|
|
20
|
+
|
|
21
|
+
matched = true
|
|
22
|
+
result = run_action(destination, extractors, action)
|
|
23
|
+
return result if result == :halt || result.is_a?(Hash)
|
|
24
|
+
end
|
|
25
|
+
:halt unless matched
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def run_action(destination, extractors, action)
|
|
31
|
+
arguments = extractors.transform_values { |source| extract_argument(source, action) }
|
|
32
|
+
if destination.is_a?(Class)
|
|
33
|
+
destination.new(**arguments).call
|
|
34
|
+
else
|
|
35
|
+
send(destination, **arguments)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def extract_argument(source, action)
|
|
40
|
+
case source
|
|
41
|
+
when :selected_option_value then action.dig("selected_option", "value")
|
|
42
|
+
when :selected_option_values then action["selected_options"]&.map { |option| option["value"] }
|
|
43
|
+
else source.respond_to?(:call) ? source.call(action) : action[source.to_s]
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
class SlashCommand < SlackBot::Command
|
|
4
|
+
include Responses
|
|
5
|
+
include ViewOwner
|
|
6
|
+
include SlackResponses
|
|
7
|
+
|
|
8
|
+
class_attribute :input_class, instance_accessor: false
|
|
9
|
+
|
|
10
|
+
def self.input(klass)
|
|
11
|
+
self.input_class = klass
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def resolve_request
|
|
17
|
+
input = self.class.input_class
|
|
18
|
+
input ? resolve(**input.coerce(args)) : super
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Event < SlackBot::Event
|
|
23
|
+
include Responses
|
|
24
|
+
include ViewOwner
|
|
25
|
+
include SlackResponses
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class Interaction < SlackBot::Interaction
|
|
29
|
+
include Responses
|
|
30
|
+
include ViewOwner
|
|
31
|
+
include SlackResponses
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def interaction_payload
|
|
36
|
+
payload
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class ViewClosed < Interaction
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class Shortcut < Interaction
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class MessageAction < Interaction
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class Options < SlackBot::MenuOptions
|
|
50
|
+
include Responses
|
|
51
|
+
|
|
52
|
+
def value
|
|
53
|
+
params[:value]
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
class InputObject
|
|
4
|
+
COERCERS = {
|
|
5
|
+
String => ->(value) { String(value) },
|
|
6
|
+
Integer => ->(value) { Integer(value, 10) },
|
|
7
|
+
Float => ->(value) { Float(value) }
|
|
8
|
+
}.freeze
|
|
9
|
+
|
|
10
|
+
class_attribute :arguments, instance_accessor: false, default: {}.freeze
|
|
11
|
+
|
|
12
|
+
def self.argument(name, type = String, required: false, default: nil)
|
|
13
|
+
self.arguments = arguments.merge(name.to_sym => [type, required, default].freeze).freeze
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.coerce(args)
|
|
17
|
+
arguments.each_with_object({}) do |(name, (type, required, default)), values|
|
|
18
|
+
raw = args[name]
|
|
19
|
+
value = raw.nil? ? default : coerce_argument(raw, type, name)
|
|
20
|
+
raise Errors::InvalidPayloadError.new("Missing argument #{name}") if required && value.nil?
|
|
21
|
+
|
|
22
|
+
values[name] = value
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.coerce_argument(value, type, name)
|
|
27
|
+
COERCERS.fetch(type, type).call(value)
|
|
28
|
+
rescue ArgumentError, TypeError, RangeError
|
|
29
|
+
raise Errors::InvalidPayloadError.new("Invalid argument #{name}"), cause: nil
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private_class_method :coerce_argument
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
class Registrar
|
|
4
|
+
def initialize(config)
|
|
5
|
+
@config = config
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def register(schema)
|
|
9
|
+
schema.registrations.each do |(route, _key), arguments|
|
|
10
|
+
case route
|
|
11
|
+
when :slash_command
|
|
12
|
+
register_slash(*arguments)
|
|
13
|
+
when :block_actions
|
|
14
|
+
@config.block_action(*arguments)
|
|
15
|
+
else
|
|
16
|
+
@config.public_send(route, *arguments)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
@config
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def register_slash(url_token, handler)
|
|
25
|
+
if handler <= SlashCommandGroup
|
|
26
|
+
endpoint = @config.slash_command_endpoint(url_token, handler.default_handler)
|
|
27
|
+
handler.commands.each { |token, command| endpoint.command(token, command) }
|
|
28
|
+
else
|
|
29
|
+
@config.slash_command_endpoint(url_token, handler)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
module Responses
|
|
4
|
+
OPERATIONS = %i[open_modal update_modal push_modal publish_view response_action response_url delete_original].freeze
|
|
5
|
+
|
|
6
|
+
module Declaration
|
|
7
|
+
def respond_with(mode = nil, **options)
|
|
8
|
+
if mode && (!%i[ack none].include?(mode) || options.any?)
|
|
9
|
+
raise Errors::ConfigurationError.new("Expected :ack or :none without options")
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
declaration = [mode || declared_operation(options), options.freeze].freeze
|
|
13
|
+
define_singleton_method(:response_declaration) { declaration }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def response_declaration
|
|
17
|
+
nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def declared_operation(options)
|
|
23
|
+
operations = OPERATIONS & options.keys
|
|
24
|
+
operations.delete(:delete_original) if options.key?(:response_url)
|
|
25
|
+
raise Errors::ConfigurationError.new("Declare exactly one response operation") unless operations.one?
|
|
26
|
+
|
|
27
|
+
operations.first
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.included(base)
|
|
32
|
+
base.extend Declaration
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def call
|
|
36
|
+
result = resolve_request
|
|
37
|
+
return if result == :halt
|
|
38
|
+
return submission_override(result) if result.is_a?(Hash)
|
|
39
|
+
|
|
40
|
+
declaration = self.class.response_declaration
|
|
41
|
+
return result unless declaration
|
|
42
|
+
|
|
43
|
+
mode, options = declaration
|
|
44
|
+
case mode
|
|
45
|
+
when :ack then nil
|
|
46
|
+
when :none then result
|
|
47
|
+
else apply_response(mode, options)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def resolve
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def resolve_request
|
|
58
|
+
resolve
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def submission_override(result)
|
|
62
|
+
result
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def apply_response(mode, _options)
|
|
66
|
+
raise Errors::ConfigurationError.new("#{mode} is unavailable for #{self.class.name}")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
module SlackResponses
|
|
4
|
+
private
|
|
5
|
+
|
|
6
|
+
def apply_response(mode, options)
|
|
7
|
+
case mode
|
|
8
|
+
when :open_modal, :push_modal, :update_modal, :publish_view
|
|
9
|
+
send_view(mode, render_view(options.fetch(mode)))
|
|
10
|
+
when :response_url, :delete_original
|
|
11
|
+
send_message(options)
|
|
12
|
+
else
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def interaction_payload
|
|
19
|
+
params
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def slack_field(name)
|
|
23
|
+
interaction_payload[name] || interaction_payload[name.to_sym]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def send_view(mode, view)
|
|
27
|
+
case mode
|
|
28
|
+
when :open_modal, :push_modal
|
|
29
|
+
trigger_id = require_slack_field(slack_field("trigger_id"), "trigger_id")
|
|
30
|
+
SlackBot::Interaction.public_send(mode, callback: new_view_callback, trigger_id: trigger_id, view: view)
|
|
31
|
+
when :update_modal
|
|
32
|
+
update_rendered_modal(view)
|
|
33
|
+
when :publish_view
|
|
34
|
+
publish_rendered_view(view)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def update_rendered_modal(view)
|
|
39
|
+
source = interaction_payload["view"] || {}
|
|
40
|
+
SlackBot::Interaction.update_modal(callback: active_callback,
|
|
41
|
+
view_id: require_slack_field(source["id"], "view.id"), view: view, hash: source["hash"])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def publish_rendered_view(view)
|
|
45
|
+
user_id = slack_field("user_id") || interaction_payload.dig("user", "id") ||
|
|
46
|
+
params.dig("event", "user") || params.dig(:event, :user)
|
|
47
|
+
SlackBot::Interaction.publish_view(user_id: require_slack_field(user_id, "user"), view: view,
|
|
48
|
+
callback: active_callback || new_view_callback)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def send_message(options)
|
|
52
|
+
url = require_slack_field(response_destination, "response_url")
|
|
53
|
+
response = ApiClient.response_url_post(url, **response_message(options))
|
|
54
|
+
raise Errors::SlackApiError.new("Slack did not accept the response message") unless response.ok?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def response_destination
|
|
58
|
+
url = slack_field("response_url")
|
|
59
|
+
return url if url.present?
|
|
60
|
+
|
|
61
|
+
urls = slack_field("response_urls") || interaction_payload.dig("view", "response_urls")
|
|
62
|
+
entry = urls.first if urls.is_a?(Array) && urls.one?
|
|
63
|
+
entry["response_url"] if entry.is_a?(Hash)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def response_message(options)
|
|
67
|
+
message = options.except(:response_url)
|
|
68
|
+
message[:response_type] = options[:response_url].to_s if options.key?(:response_url)
|
|
69
|
+
message[:blocks] = render_view(message[:blocks]) if message[:blocks].is_a?(Symbol)
|
|
70
|
+
message
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def require_slack_field(value, name)
|
|
74
|
+
return value if value.present?
|
|
75
|
+
|
|
76
|
+
raise Errors::InvalidPayloadError.new("Missing Slack field #{name}")
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
class SlashCommandGroup
|
|
4
|
+
class_attribute :default_handler, :commands, instance_accessor: false
|
|
5
|
+
self.commands = {}.freeze
|
|
6
|
+
|
|
7
|
+
def self.default_command(handler)
|
|
8
|
+
self.default_handler = handler
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.command(token, handler)
|
|
12
|
+
self.commands = commands.merge(token.to_sym => handler).freeze
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
module ViewOwner
|
|
4
|
+
EMPTY_HANDLERS = {}.freeze
|
|
5
|
+
|
|
6
|
+
module Declaration
|
|
7
|
+
def interaction_handlers
|
|
8
|
+
EMPTY_HANDLERS
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
%w[block_actions view_submission view_closed].each do |type|
|
|
12
|
+
define_method(type) do |handler|
|
|
13
|
+
handlers = interaction_handlers.merge(type => handler).freeze
|
|
14
|
+
define_singleton_method(:interaction_handlers) { handlers }
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.included(base)
|
|
20
|
+
base.extend Declaration
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def active_callback
|
|
26
|
+
callback if respond_to?(:callback)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def new_view_callback
|
|
30
|
+
previous = active_callback
|
|
31
|
+
return unless previous || self.class.interaction_handlers.any?
|
|
32
|
+
|
|
33
|
+
raise Errors::UserAuthenticationError.new("User is not authorized") unless current_user
|
|
34
|
+
|
|
35
|
+
persist_view_callback(build_view_callback(previous), previous)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def persist_view_callback(record, previous)
|
|
39
|
+
source_args = previous ? previous.args : (args if respond_to?(:args))
|
|
40
|
+
record.args.raw_args = source_args&.to_s
|
|
41
|
+
raise Errors::SlackApiError.new("Unable to store Slack callback") if record.save == false
|
|
42
|
+
|
|
43
|
+
record
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def build_view_callback(previous)
|
|
47
|
+
owner_name = previous&.class_name || self.class.name
|
|
48
|
+
raise Errors::ConfigurationError.new("Register a named view owner") if owner_name.nil?
|
|
49
|
+
|
|
50
|
+
channel_id = previous&.channel_id || slack_field("channel_id") || interaction_payload.dig("channel", "id")
|
|
51
|
+
Callback.new(class_name: owner_name, user: current_user, channel_id: channel_id, config: config, payload: previous&.payload)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module SlackBot
|
|
2
|
+
class Schema
|
|
3
|
+
class ViewSubmission < Interaction
|
|
4
|
+
VALUE_FIELDS = %w[value selected_option selected_options selected_user selected_users selected_channel selected_channels
|
|
5
|
+
selected_conversation selected_conversations selected_date selected_time selected_date_time rich_text_value].freeze
|
|
6
|
+
|
|
7
|
+
def view_state(block_id = nil, action_id = nil)
|
|
8
|
+
values = payload.dig("view", "state", "values") || {}
|
|
9
|
+
return values if block_id.nil?
|
|
10
|
+
return values[block_id.to_s] if action_id.nil?
|
|
11
|
+
|
|
12
|
+
field = values.dig(block_id.to_s, action_id.to_s)
|
|
13
|
+
submitted_value(field) if field
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def submitted_value(field)
|
|
19
|
+
value_field = VALUE_FIELDS.find { |name| field.key?(name) }
|
|
20
|
+
case value_field
|
|
21
|
+
when "selected_option" then field.dig("selected_option", "value")
|
|
22
|
+
when "selected_options" then field["selected_options"]&.map { |option| option["value"] }
|
|
23
|
+
when nil then field
|
|
24
|
+
else field[value_field]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def submission_override(result)
|
|
29
|
+
errors = result[:errors] || result["errors"]
|
|
30
|
+
errors ? {response_action: "errors", errors: errors} : super
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def apply_response(mode, options)
|
|
34
|
+
return super unless mode == :response_action
|
|
35
|
+
|
|
36
|
+
operation = options.fetch(:response_action)
|
|
37
|
+
case operation
|
|
38
|
+
when :clear then {response_action: "clear"}
|
|
39
|
+
when :errors then nil
|
|
40
|
+
when :update, :push
|
|
41
|
+
view = render_view(options.fetch(:view))
|
|
42
|
+
record = (operation == :push) ? new_view_callback : active_callback
|
|
43
|
+
{response_action: operation.to_s, view: SlackBot::Interaction.modal_payload(record, view)}
|
|
44
|
+
else
|
|
45
|
+
raise Errors::ConfigurationError.new("Unknown response_action #{operation}")
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|