Class: BoltRb::Handlers::ActionHandler

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

Examples:

Basic button handler

class ApproveHandler < BoltRb::ActionHandler
  action 'approve_button'

  def handle
    ack
    say("Approved by <@#{user}>!")
  end
end

Handler with block_id filter

class RequestApprovalHandler < BoltRb::ActionHandler
  action 'approve', block_id: 'approval_block'

  def handle
    ack
    # Handle approval request
  end
end

Regex-based action matching

class DynamicButtonHandler < BoltRb::ActionHandler
  action /^approve_request_/

  def handle
    ack
    request_id = action_id.gsub('approve_request_', '')
    # Process the request
  end
end

Instance Attribute Summary

Attributes inherited from Base

#context

Class Method Summary collapse

Instance Method Summary collapse

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

Examples:

Match exact action_id

action 'approve_button'

Match with block_id

action 'approve_button', block_id: 'approval_block'

Match action_id pattern

action /^approve_/

Parameters:

  • action_id (String, Regexp)

    The action_id to match (exact string or regex)

  • block_id (String, Regexp, nil) (defaults to: nil)

    Optional block_id filter for more specific matching



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.

Parameters:

  • payload (Hash)

    The incoming Slack block_actions payload

Returns:

  • (Boolean)

    true if this handler should process the action



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

#actionHash?

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.

Returns:

  • (Hash, nil)

    The action hash containing action_id, value, etc.



120
121
122
# File 'lib/bolt_rb/handlers/action_handler.rb', line 120

def action
  payload['actions']&.first
end

#action_idString?

Returns the action_id from the triggered action

Returns:

  • (String, nil)

    The action_id



127
128
129
# File 'lib/bolt_rb/handlers/action_handler.rb', line 127

def action_id
  action&.dig('action_id')
end

#action_valueString?

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.

Returns:

  • (String, nil)

    The action value



137
138
139
# File 'lib/bolt_rb/handlers/action_handler.rb', line 137

def action_value
  action&.dig('value')
end

#block_idString?

Returns the block_id from the triggered action

Returns:

  • (String, nil)

    The block_id



144
145
146
# File 'lib/bolt_rb/handlers/action_handler.rb', line 144

def block_id
  action&.dig('block_id')
end

#trigger_idString?

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.

Returns:

  • (String, nil)

    The trigger_id for views.open



154
155
156
# File 'lib/bolt_rb/handlers/action_handler.rb', line 154

def trigger_id
  payload['trigger_id']
end