Class: BoltRb::Handlers::CommandHandler

Inherits:
Base
  • Object
show all
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.

Examples:

Basic command handler

class DeployHandler < BoltRb::CommandHandler
  command '/deploy'

  def handle
    ack
    say("Deploying: #{command_text}")
  end
end

Command with modal

class SettingsHandler < BoltRb::CommandHandler
  command '/settings'

  def handle
    ack
    client.views_open(
      trigger_id: trigger_id,
      view: { type: 'modal', ... }
    )
  end
end

Instance Attribute Summary

Attributes inherited from Base

#context

Class Method Summary collapse

Instance Method Summary collapse

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

Examples:

command '/deploy'


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

#channelString?

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_nameString

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_textString?

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

#paramsHash

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_idString?

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

#userString?

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