Class: BoltRb::Handlers::ActionHandler
- Defined in:
- lib/bolt_rb/handlers/action_handler.rb
Overview
Handler for Slack interactive component actions (buttons, select menus, etc.)
This handler provides the action DSL for matching block_actions payloads.
Block actions are triggered when users interact with interactive components
like buttons, overflow menus, date pickers, and select menus in messages
or modals.
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.action(action_id, block_id: nil) ⇒ void
Configures which action_id this handler responds to.
-
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload.
Instance Method Summary collapse
-
#action ⇒ Hash?
Returns the first action from the payload.
-
#action_id ⇒ String?
Returns the action_id from the triggered action.
-
#action_value ⇒ String?
Returns the value from the triggered action.
-
#block_id ⇒ String?
Returns the block_id from the triggered action.
-
#trigger_id ⇒ String?
Returns the trigger_id for opening modals.
Methods inherited from Base
#ack, #call, #channel, #client, #handle, inherited, #initialize, middleware_stack, #payload, #respond, #say, use, #user
Constructor Details
This class inherits a constructor from BoltRb::Handlers::Base
Class Method Details
.action(action_id, block_id: nil) ⇒ void
This method returns an undefined value.
Configures which action_id this handler responds to
58 59 60 61 62 63 64 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 58 def action(action_id, block_id: nil) @matcher_config = { type: :action, action_id: action_id, block_id: block_id } end |
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload
Checks if the payload is a block_actions type and if any of the actions in the payload match the configured action_id and optional block_id.
73 74 75 76 77 78 79 80 81 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 73 def matches?(payload) return false unless matcher_config return false unless payload['type'] == 'block_actions' actions = payload['actions'] || [] actions.any? do |action| action_matches?(action) && block_matches?(action) end end |
Instance Method Details
#action ⇒ Hash?
Returns the first action from the payload
Block actions payloads can contain multiple actions, but typically only one action is triggered at a time. This returns the first action.
120 121 122 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 120 def action payload['actions']&.first end |
#action_id ⇒ String?
Returns the action_id from the triggered action
127 128 129 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 127 def action_id action&.dig('action_id') end |
#action_value ⇒ String?
Returns the value from the triggered action
For buttons this is the button's value. For select menus this is the selected option's value.
137 138 139 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 137 def action_value action&.dig('value') end |
#block_id ⇒ String?
Returns the block_id from the triggered action
144 145 146 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 144 def block_id action&.dig('block_id') end |
#trigger_id ⇒ String?
Returns the trigger_id for opening modals
Slack provides a trigger_id with interactive actions that can be used to open modals within 3 seconds of receiving the action.
154 155 156 |
# File 'lib/bolt_rb/handlers/action_handler.rb', line 154 def trigger_id payload['trigger_id'] end |