Class: BoltRb::Handlers::ShortcutHandler
- Defined in:
- lib/bolt_rb/handlers/shortcut_handler.rb
Overview
Handler for Slack shortcuts (global shortcuts and message shortcuts)
This handler provides the shortcut DSL for matching shortcut payloads.
Global shortcuts are triggered from the lightning bolt menu in Slack,
while message shortcuts appear in the context menu of messages.
Constant Summary collapse
- SHORTCUT_TYPES =
Valid shortcut payload types 'shortcut' is a global shortcut (from lightning bolt menu) 'message_action' is a message shortcut (from message context menu)
%w[shortcut message_action].freeze
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload.
-
.shortcut(callback_id) ⇒ void
Configures which callback_id this handler responds to.
Instance Method Summary collapse
-
#callback_id ⇒ String?
Returns the callback_id from the shortcut payload.
-
#message ⇒ Hash?
Returns the message object for message shortcuts.
-
#message_text ⇒ String?
Returns the text of the message for message shortcuts.
-
#shortcut_type ⇒ Symbol
Returns the type of shortcut (:global or :message).
-
#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
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload
Checks if the payload is a shortcut or message_action type and if the callback_id matches the configured pattern.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 72 def matches?(payload) return false unless matcher_config return false unless SHORTCUT_TYPES.include?(payload['type']) if matcher_config[:callback_id].is_a?(Regexp) matcher_config[:callback_id].match?(payload['callback_id']) else payload['callback_id'] == matcher_config[:callback_id] end end |
.shortcut(callback_id) ⇒ void
This method returns an undefined value.
Configures which callback_id this handler responds to
58 59 60 61 62 63 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 58 def shortcut(callback_id) @matcher_config = { type: :shortcut, callback_id: callback_id } end |
Instance Method Details
#callback_id ⇒ String?
Returns the callback_id from the shortcut payload
87 88 89 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 87 def callback_id payload['callback_id'] end |
#message ⇒ Hash?
Returns the message object for message shortcuts
Only available for message shortcuts (message_action type). Contains the original message that the shortcut was triggered on.
115 116 117 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 115 def payload['message'] end |
#message_text ⇒ String?
Returns the text of the message for message shortcuts
Convenience method to get the message text directly.
124 125 126 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 124 def &.dig('text') end |
#shortcut_type ⇒ Symbol
Returns the type of shortcut (:global or :message)
105 106 107 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 105 def shortcut_type payload['type'] == 'message_action' ? :message : :global end |
#trigger_id ⇒ String?
Returns the trigger_id for opening modals
Slack provides a trigger_id with shortcuts that can be used to open modals within 3 seconds of receiving the shortcut.
97 98 99 |
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 97 def trigger_id payload['trigger_id'] end |