Class: BoltRb::Handlers::CommandHandler
- Defined in:
- lib/bolt_rb/handlers/command_handler.rb
Overview
Handler for Slack slash commands (/deploy, /help, etc.)
This handler provides the command DSL for matching slash command invocations.
Slash commands have a different payload structure than events, with fields like
user_id, channel_id, and trigger_id at the top level.
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.command(command_name) ⇒ void
Configures which slash command this handler responds to.
-
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload.
Instance Method Summary collapse
-
#channel ⇒ String?
Returns the channel ID where the command was invoked.
-
#command_name ⇒ String
Returns the slash command that was invoked.
-
#command_text ⇒ String?
Returns the text provided after the command.
-
#params ⇒ Hash
Returns the command parameters as a hash.
-
#trigger_id ⇒ String?
Returns the trigger_id for opening modals.
-
#user ⇒ String?
Returns the user ID who invoked the command.
Methods inherited from Base
#ack, #call, #client, #handle, inherited, #initialize, middleware_stack, #payload, #respond, #say, use
Constructor Details
This class inherits a constructor from BoltRb::Handlers::Base
Class Method Details
.command(command_name) ⇒ void
This method returns an undefined value.
Configures which slash command this handler responds to
42 43 44 45 46 47 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 42 def command(command_name) @matcher_config = { type: :command, command: command_name } end |
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload
Checks if the payload's command field matches the configured command.
55 56 57 58 59 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 55 def matches?(payload) return false unless matcher_config payload['command'] == matcher_config[:command] end |
Instance Method Details
#channel ⇒ String?
Returns the channel ID where the command was invoked
Overrides Base#channel because slash command payloads use 'channel_id' instead of nested event.channel structure.
111 112 113 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 111 def channel payload['channel_id'] || super end |
#command_name ⇒ String
Returns the slash command that was invoked
65 66 67 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 65 def command_name payload['command'] end |
#command_text ⇒ String?
Returns the text provided after the command
For /deploy production --force, this returns "production --force"
74 75 76 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 74 def command_text payload['text'] end |
#params ⇒ Hash
Returns the command parameters as a hash
81 82 83 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 81 def params { text: command_text } end |
#trigger_id ⇒ String?
Returns the trigger_id for opening modals
Slack provides a trigger_id with slash commands that can be used to open modals within 3 seconds of receiving the command.
91 92 93 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 91 def trigger_id payload['trigger_id'] end |
#user ⇒ String?
Returns the user ID who invoked the command
Overrides Base#user because slash command payloads use 'user_id' instead of nested event.user structure.
101 102 103 |
# File 'lib/bolt_rb/handlers/command_handler.rb', line 101 def user payload['user_id'] || super end |