Class: BoltRb::Handlers::ShortcutHandler

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

Examples:

Global shortcut handler

class CreateTicketHandler < BoltRb::ShortcutHandler
  shortcut 'create_ticket'

  def handle
    ack
    # Open a modal with views.open using trigger_id
  end
end

Message shortcut handler

class QuoteMessageHandler < BoltRb::ShortcutHandler
  shortcut 'quote_message'

  def handle
    ack
    text = message_text
    # Do something with the quoted message
  end
end

Regex-based shortcut matching

class CreateHandler < BoltRb::ShortcutHandler
  shortcut /^create_/

  def handle
    ack
    # Handle any shortcut starting with 'create_'
  end
end

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

#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

.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.

Parameters:

  • payload (Hash)

    The incoming Slack shortcut payload

Returns:

  • (Boolean)

    true if this handler should process the shortcut



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

Examples:

Match exact callback_id

shortcut 'create_ticket'

Match callback_id pattern

shortcut /^create_/

Parameters:

  • callback_id (String, Regexp)

    The callback_id to match (exact string or regex)



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

Returns the callback_id from the shortcut payload

Returns:

  • (String, nil)

    The callback_id



87
88
89
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 87

def callback_id
  payload['callback_id']
end

#messageHash?

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.

Returns:

  • (Hash, nil)

    The message object or nil for global shortcuts



115
116
117
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 115

def message
  payload['message']
end

#message_textString?

Returns the text of the message for message shortcuts

Convenience method to get the message text directly.

Returns:

  • (String, nil)

    The message text or nil if not available



124
125
126
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 124

def message_text
  message&.dig('text')
end

#shortcut_typeSymbol

Returns the type of shortcut (:global or :message)

Returns:

  • (Symbol)

    :message for message shortcuts (message_action), :global for global shortcuts



105
106
107
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 105

def shortcut_type
  payload['type'] == 'message_action' ? :message : :global
end

#trigger_idString?

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.

Returns:

  • (String, nil)

    The trigger_id for views.open



97
98
99
# File 'lib/bolt_rb/handlers/shortcut_handler.rb', line 97

def trigger_id
  payload['trigger_id']
end