Class: BoltRb::Handlers::EventHandler
- 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.
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
-
.listen_to(event_type, pattern: nil) ⇒ void
Configures which event type this handler responds to.
-
.matches?(payload) ⇒ Boolean
Determines if this handler matches the given payload.
Instance Method Summary collapse
-
#event ⇒ Hash?
Returns the event portion of the payload.
-
#text ⇒ String?
Returns the text content of the event.
-
#thread_ts ⇒ String?
Returns the thread timestamp if this message is in a thread.
-
#ts ⇒ String?
Returns the message timestamp.
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
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.
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
#event ⇒ Hash?
Returns the event portion of the payload
84 85 86 |
# File 'lib/bolt_rb/handlers/event_handler.rb', line 84 def event payload['event'] end |
#text ⇒ String?
Returns the text content of the event
91 92 93 |
# File 'lib/bolt_rb/handlers/event_handler.rb', line 91 def text event&.dig('text') end |
#thread_ts ⇒ String?
Returns the thread timestamp if this message is in a thread
98 99 100 |
# File 'lib/bolt_rb/handlers/event_handler.rb', line 98 def thread_ts event&.dig('thread_ts') end |
#ts ⇒ String?
Returns the message timestamp
105 106 107 |
# File 'lib/bolt_rb/handlers/event_handler.rb', line 105 def ts event&.dig('ts') end |