Class: BoltRb::Context
- Inherits:
-
Object
- Object
- BoltRb::Context
- Defined in:
- lib/bolt_rb/context.rb
Overview
Context wraps the incoming Slack event payload and provides convenience methods for responding to events.
This is the object passed to event handlers and provides access to:
- The raw payload data
- The Slack Web API client
- Helper methods like say(), ack(), and respond()
Instance Attribute Summary collapse
-
#client ⇒ Slack::Web::Client
readonly
The Slack Web API client.
-
#payload ⇒ Hash
readonly
The raw payload from the Slack event.
Instance Method Summary collapse
-
#ack(response = nil) ⇒ void
Acknowledges the event.
-
#acked? ⇒ Boolean
Returns whether this context has been acknowledged.
-
#channel ⇒ String?
Extracts the channel ID from the payload.
-
#event ⇒ Hash?
Returns the event portion of the payload.
-
#initialize(payload:, client:, ack:) ⇒ Context
constructor
Creates a new Context instance.
-
#respond(message) ⇒ Net::HTTPResponse?
Responds using the response_url.
-
#say(message) ⇒ Hash
Posts a message to the channel.
-
#text ⇒ String?
Extracts the text content from the event.
-
#user ⇒ String?
Extracts the user ID from the payload.
Constructor Details
#initialize(payload:, client:, ack:) ⇒ Context
Creates a new Context instance
37 38 39 40 41 42 |
# File 'lib/bolt_rb/context.rb', line 37 def initialize(payload:, client:, ack:) @payload = payload @client = client @ack_fn = ack @acked = false end |
Instance Attribute Details
#client ⇒ Slack::Web::Client (readonly)
30 31 32 |
# File 'lib/bolt_rb/context.rb', line 30 def client @client end |
#payload ⇒ Hash (readonly)
27 28 29 |
# File 'lib/bolt_rb/context.rb', line 27 def payload @payload end |
Instance Method Details
#ack(response = nil) ⇒ void
This method returns an undefined value.
Acknowledges the event
For events that require acknowledgement (slash commands, interactive components), this method sends the acknowledgement to Slack.
101 102 103 104 |
# File 'lib/bolt_rb/context.rb', line 101 def ack(response = nil) @ack_fn.call(response) @acked = true end |
#acked? ⇒ Boolean
Returns whether this context has been acknowledged
109 110 111 |
# File 'lib/bolt_rb/context.rb', line 109 def acked? @acked end |
#channel ⇒ String?
Extracts the channel ID from the payload
Handles various payload formats:
- event.channel (string)
- channel_id (slash commands)
- channel (string)
- channel.id (nested object)
78 79 80 81 82 83 84 85 |
# File 'lib/bolt_rb/context.rb', line 78 def channel extract_id( payload.dig('event', 'channel') || payload['channel_id'] || payload['channel'] || payload.dig('channel', 'id') ) end |
#event ⇒ Hash?
Returns the event portion of the payload
47 48 49 |
# File 'lib/bolt_rb/context.rb', line 47 def event payload['event'] end |
#respond(message) ⇒ Net::HTTPResponse?
Responds using the response_url
This is used for slash commands and interactive components where Slack provides a response_url for sending follow-up messages.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/bolt_rb/context.rb', line 147 def respond() response_url = payload['response_url'] return unless response_url = .is_a?(Hash) ? : { text: } uri = URI(response_url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri.path) request['Content-Type'] = 'application/json' request.body = .to_json http.request(request) end |
#say(message) ⇒ Hash
Posts a message to the channel
124 125 126 127 |
# File 'lib/bolt_rb/context.rb', line 124 def say() = .is_a?(Hash) ? : { text: } client.chat_postMessage(.merge(channel: channel)) end |
#text ⇒ String?
Extracts the text content from the event
90 91 92 |
# File 'lib/bolt_rb/context.rb', line 90 def text event&.dig('text') end |
#user ⇒ String?
Extracts the user ID from the payload
Handles various payload formats:
- event.user (string)
- user_id (slash commands)
- user (string)
- user.id (nested object)
60 61 62 63 64 65 66 67 |
# File 'lib/bolt_rb/context.rb', line 60 def user extract_id( payload.dig('event', 'user') || payload['user_id'] || payload['user'] || payload.dig('user', 'id') ) end |