grape-slack-bot 2.1.2 → 3.0.0

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.
@@ -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
@@ -0,0 +1,39 @@
1
+ require "active_support/core_ext/class/attribute"
2
+
3
+ module SlackBot
4
+ class Schema
5
+ class_attribute :registrations, instance_accessor: false, default: {}.freeze
6
+
7
+ %i[callback_storage callback_user_finder user_session_resolver event_dispatcher].each do |setting|
8
+ define_singleton_method(setting) do |value|
9
+ self.registrations = registrations.merge([setting] => [value].freeze).freeze
10
+ end
11
+ end
12
+
13
+ %i[slash_command event block_actions shortcut message_action view_closed block_suggestion].each do |route|
14
+ define_singleton_method(route) do |key, handler|
15
+ identifier = key.to_s.dup.freeze
16
+ self.registrations = registrations.merge([route, identifier].freeze => [identifier, handler].freeze).freeze
17
+ end
18
+ end
19
+
20
+ def self.options(action_id, handler)
21
+ block_suggestion(action_id, handler)
22
+ end
23
+
24
+ def self.mount(schema)
25
+ self.registrations = registrations.merge(schema.registrations).freeze
26
+ self
27
+ end
28
+ end
29
+ end
30
+
31
+ require "slack_bot/schema/registrar"
32
+ require "slack_bot/schema/slash_command_group"
33
+ require "slack_bot/schema/input_object"
34
+ require "slack_bot/schema/responses"
35
+ require "slack_bot/schema/view_owner"
36
+ require "slack_bot/schema/slack_responses"
37
+ require "slack_bot/schema/handlers"
38
+ require "slack_bot/schema/block_actions"
39
+ require "slack_bot/schema/view_submission"
data/lib/slack_bot.rb CHANGED
@@ -21,9 +21,10 @@ require "slack_bot/menu_options"
21
21
  require "slack_bot/view"
22
22
 
23
23
  require "slack_bot/pager"
24
+ require "slack_bot/schema"
24
25
 
25
26
  require "slack_bot/grape_extension"
26
27
 
27
28
  module SlackBot
28
- VERSION = "2.1.2".freeze
29
+ VERSION = "3.0.0".freeze
29
30
  end
data/sig/slack_bot.rbs CHANGED
@@ -190,7 +190,7 @@ module SlackBot
190
190
  def initialize: (?authorization_token: String) -> void
191
191
 
192
192
  def views_open: (trigger_id: String, view: Hash[String, untyped]) -> ApiResponse
193
- def views_update: (view_id: String, view: Hash[String, untyped]) -> ApiResponse
193
+ def views_update: (view_id: String, view: Hash[String, untyped], ?hash: String?) -> ApiResponse
194
194
  def views_publish: (user_id: String, view: Hash[String, untyped]) -> ApiResponse
195
195
  def chat_post_message: (channel: String, text: String, blocks: Array[Hash[String, untyped]]) -> ApiResponse
196
196
  def chat_update: (channel: String, ts: String, text: String, blocks: Array[Hash[String, untyped]]) -> ApiResponse
@@ -264,7 +264,7 @@ module SlackBot
264
264
  end
265
265
 
266
266
  def self.open_modal: (callback: Callback?, trigger_id: String, view: Hash[String, untyped]) -> SlackViewsReply
267
- def self.update_modal: (callback: Callback?, view_id: String, view: Hash[String, untyped]) -> SlackViewsReply
267
+ def self.update_modal: (callback: Callback?, view_id: String, view: Hash[String, untyped], ?hash: String?) -> SlackViewsReply
268
268
  def self.publish_view: (user_id: String, view: Hash[String, untyped], ?callback: Callback?, ?metadata: Hash[String, untyped]?) -> SlackViewsReply
269
269
 
270
270
  attr_reader current_user: untyped?
@@ -332,7 +332,7 @@ module SlackBot
332
332
  def resolve_event_user: (Hash[String, untyped] params) -> untyped?
333
333
  def events_callback: (Hash[String, untyped] params) -> untyped
334
334
  def url_verification: (Hash[String, untyped] params) -> Hash[String, String]
335
- def handle_block_actions_view: (view: Hash[String, untyped]?, user: untyped, params: Hash[String, untyped]) -> untyped
335
+ def handle_block_actions_view: (view: Hash[String, untyped]?, user: untyped, params: Hash[String, untyped], ?type: String, ?payload: Hash[String, untyped]?) -> untyped
336
336
  def handle_block_actions_message: (payload: Hash[String, untyped], user: untyped, params: Hash[String, untyped]) -> untyped
337
337
  def dispatch_event: (handler: Class, params: Hash[String, untyped], current_user: untyped) -> bool
338
338
  def route_interaction: (payload: Hash[String, untyped], user: untyped, params: Hash[String, untyped]) -> untyped
@@ -0,0 +1,126 @@
1
+ module SlackBot
2
+ module Errors
3
+ class PushModalError < SlackResponseError
4
+ end
5
+ end
6
+
7
+ module ResponseUrl
8
+ class Reply
9
+ attr_reader status: Integer
10
+ def ok?: () -> bool
11
+ end
12
+ end
13
+
14
+ class Config
15
+ def use: (singleton(Schema) schema) -> Config
16
+ alias mount use
17
+ alias block_suggestion menu_options
18
+ def shortcut: (String | Symbol callback_id, Class handler) -> void
19
+ def message_action: (String | Symbol callback_id, Class handler) -> void
20
+ def view_closed: (String | Symbol callback_id, Class handler) -> void
21
+ def find_interaction_callback: (String type, String? callback_id) -> Class?
22
+ end
23
+
24
+ class ApiClient
25
+ def views_push: (trigger_id: String, view: Hash[String | Symbol, untyped]) -> ApiResponse
26
+ def self.response_url_post: (String url, **untyped message) -> ResponseUrl::Reply
27
+ def response_url_post: (String url, **untyped message) -> ResponseUrl::Reply
28
+ end
29
+
30
+ class Interaction
31
+ def self.push_modal: (callback: Callback?, trigger_id: String, view: Hash[String | Symbol, untyped]) -> SlackViewsReply
32
+ attr_writer payload: Hash[String, untyped]
33
+ end
34
+
35
+ class Schema
36
+ type route_id = String | Symbol
37
+
38
+ def self.callback_storage: (untyped storage) -> void
39
+ def self.callback_user_finder: (Proc finder) -> void
40
+ def self.user_session_resolver: (Proc resolver) -> void
41
+ def self.event_dispatcher: (Proc dispatcher) -> void
42
+
43
+ def self.slash_command: (route_id url_token, Class handler) -> void
44
+ def self.event: (route_id event_type, Class handler) -> void
45
+ def self.block_actions: (route_id action_id, Class handler) -> void
46
+ def self.shortcut: (route_id callback_id, Class handler) -> void
47
+ def self.message_action: (route_id callback_id, Class handler) -> void
48
+ def self.view_closed: (route_id callback_id, Class handler) -> void
49
+ def self.block_suggestion: (route_id action_id, Class handler) -> void
50
+ def self.options: (route_id action_id, Class handler) -> void
51
+ def self.mount: (singleton(Schema) schema) -> singleton(Schema)
52
+
53
+ class SlashCommandGroup
54
+ def self.default_handler: () -> Class?
55
+ def self.commands: () -> Hash[Symbol, Class]
56
+ def self.default_command: (Class handler) -> void
57
+ def self.command: (route_id token, Class handler) -> void
58
+ end
59
+
60
+ class InputObject
61
+ def self.argument: (route_id name, ?(Class | ^(untyped) -> untyped) type, ?required: bool, ?default: untyped) -> void
62
+ def self.coerce: (Args args) -> Hash[Symbol, untyped]
63
+ end
64
+
65
+ module Responses
66
+ module Declaration
67
+ def respond_with: (?(:ack | :none) mode, **untyped options) -> void
68
+ end
69
+
70
+ def call: () -> untyped
71
+ def resolve: (**untyped args) -> untyped
72
+ end
73
+
74
+ module ViewOwner
75
+ module Declaration
76
+ def block_actions: (Class handler) -> void
77
+ def view_submission: (Class handler) -> void
78
+ def view_closed: (Class handler) -> void
79
+ end
80
+ end
81
+
82
+ class SlashCommand < SlackBot::Command
83
+ include Responses
84
+ extend Responses::Declaration
85
+ extend ViewOwner::Declaration
86
+
87
+ def self.input: (singleton(InputObject) klass) -> void
88
+ end
89
+
90
+ class Event < SlackBot::Event
91
+ include Responses
92
+ extend Responses::Declaration
93
+ extend ViewOwner::Declaration
94
+ end
95
+
96
+ class Interaction < SlackBot::Interaction
97
+ include Responses
98
+ extend Responses::Declaration
99
+ extend ViewOwner::Declaration
100
+ end
101
+
102
+ class BlockActions < Interaction
103
+ def self.action: (route_id action_id, ?args: Hash[Symbol, String | Symbol | ^(Hash[String, untyped]) -> untyped], ?with: Symbol | Class) -> void
104
+ end
105
+
106
+ class ViewSubmission < Interaction
107
+ def view_state: (?route_id? block_id, ?route_id? action_id) -> untyped
108
+ end
109
+
110
+ class ViewClosed < Interaction
111
+ end
112
+
113
+ class Shortcut < Interaction
114
+ end
115
+
116
+ class MessageAction < Interaction
117
+ end
118
+
119
+ class Options < SlackBot::MenuOptions
120
+ include Responses
121
+ extend Responses::Declaration
122
+
123
+ def value: () -> String?
124
+ end
125
+ end
126
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-slack-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov
@@ -117,14 +117,14 @@ dependencies:
117
117
  requirements:
118
118
  - - ">="
119
119
  - !ruby/object:Gem::Version
120
- version: 2.2.0
120
+ version: 2.2.9
121
121
  type: :development
122
122
  prerelease: false
123
123
  version_requirements: !ruby/object:Gem::Requirement
124
124
  requirements:
125
125
  - - ">="
126
126
  - !ruby/object:Gem::Version
127
- version: 2.2.0
127
+ version: 2.2.9
128
128
  - !ruby/object:Gem::Dependency
129
129
  name: webmock
130
130
  requirement: !ruby/object:Gem::Requirement
@@ -339,8 +339,20 @@ files:
339
339
  - lib/slack_bot/logger.rb
340
340
  - lib/slack_bot/menu_options.rb
341
341
  - lib/slack_bot/pager.rb
342
+ - lib/slack_bot/response_url.rb
343
+ - lib/slack_bot/schema.rb
344
+ - lib/slack_bot/schema/block_actions.rb
345
+ - lib/slack_bot/schema/handlers.rb
346
+ - lib/slack_bot/schema/input_object.rb
347
+ - lib/slack_bot/schema/registrar.rb
348
+ - lib/slack_bot/schema/responses.rb
349
+ - lib/slack_bot/schema/slack_responses.rb
350
+ - lib/slack_bot/schema/slash_command_group.rb
351
+ - lib/slack_bot/schema/view_owner.rb
352
+ - lib/slack_bot/schema/view_submission.rb
342
353
  - lib/slack_bot/view.rb
343
354
  - sig/slack_bot.rbs
355
+ - sig/slack_bot_schema.rbs
344
356
  homepage: https://github.com/amkisko/grape-slack-bot.rb
345
357
  licenses:
346
358
  - MIT