Class: BoltRb::Handlers::EventHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/bolt_rb/handlers/event_handler.rb

Overview

Handler for Slack events (message, app_mention, reaction_added, etc.)

This handler provides the listen_to DSL for matching event types and optionally filtering by text patterns.

Examples:

Basic message handler

class GreetingHandler < BoltRb::EventHandler
  listen_to :message

  def handle
    say("Hello, #{user}!")
  end
end

Handler with pattern matching

class HelloHandler < BoltRb::EventHandler
  listen_to :message, pattern: /hello/i

  def handle
    say("Hello to you too!")
  end
end

App mention handler

class MentionHandler < BoltRb::EventHandler
  listen_to :app_mention

  def handle
    say("You mentioned me in #{channel}!")
  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

.listen_to(event_type, pattern: nil) ⇒ void

This method returns an undefined value.

Configures which event type this handler responds to

Examples:

Listen to all messages

listen_to :message

Listen to messages matching a pattern

listen_to :message, pattern: /help/i

Parameters:

  • event_type (Symbol, String)

    The Slack event type to listen for (e.g., :message, :app_mention, :reaction_added)

  • pattern (Regexp, nil) (defaults to: nil)

    Optional regex pattern to match against the event's text field



51
52
53
54
55
56
57
# File 'lib/bolt_rb/handlers/event_handler.rb', line 51

def listen_to(event_type, pattern: nil)
  @matcher_config = {
    type: :event,
    event_type: event_type,
    pattern: pattern
  }
end

.matches?(payload) ⇒ Boolean

Determines if this handler matches the given payload

Checks the event type and optionally the text pattern.

Parameters:

  • payload (Hash)

    The incoming Slack event payload

Returns:

  • (Boolean)

    true if this handler should process the event



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bolt_rb/handlers/event_handler.rb', line 65

def matches?(payload)
  return false unless matcher_config

  event = payload['event']
  return false unless event
  return false unless event['type'].to_s == matcher_config[:event_type].to_s

  if matcher_config[:pattern]
    return false if event['text'].nil?
    return matcher_config[:pattern].match?(event['text'])
  end

  true
end

Instance Method Details

#eventHash?

Returns the event portion of the payload

Returns:

  • (Hash, nil)

    The event data



84
85
86
# File 'lib/bolt_rb/handlers/event_handler.rb', line 84

def event
  payload['event']
end

#textString?

Returns the text content of the event

Returns:

  • (String, nil)

    The message text



91
92
93
# File 'lib/bolt_rb/handlers/event_handler.rb', line 91

def text
  event&.dig('text')
end

#thread_tsString?

Returns the thread timestamp if this message is in a thread

Returns:

  • (String, nil)

    The thread_ts value



98
99
100
# File 'lib/bolt_rb/handlers/event_handler.rb', line 98

def thread_ts
  event&.dig('thread_ts')
end

#tsString?

Returns the message timestamp

Returns:

  • (String, nil)

    The ts value



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

def ts
  event&.dig('ts')
end