Class: BoltRb::Context

Inherits:
Object
  • Object
show all
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()

Examples:

Basic usage in an event handler

app.event('message') do |ctx|
  ctx.say("You said: #{ctx.text}")
end

Using respond for slash commands

app.command('/echo') do |ctx|
  ctx.ack
  ctx.respond("Echoing: #{ctx.text}")
end

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clientSlack::Web::Client (readonly)



30
31
32
# File 'lib/bolt_rb/context.rb', line 30

def client
  @client
end

#payloadHash (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

#channelString?

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

#eventHash?

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.

Examples:

Simple response

ctx.respond("Processing complete!")

Ephemeral response with blocks

ctx.respond(
  text: "Here's your data",
  response_type: "ephemeral",
  blocks: [...]
)


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bolt_rb/context.rb', line 147

def respond(message)
  response_url = payload['response_url']
  return unless response_url

  options = message.is_a?(Hash) ? message : { text: message }
  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 = options.to_json
  http.request(request)
end

#say(message) ⇒ Hash

Posts a message to the channel

Examples:

Simple text message

ctx.say("Hello!")

Message with options

ctx.say(text: "Hello!", thread_ts: "123.456")


124
125
126
127
# File 'lib/bolt_rb/context.rb', line 124

def say(message)
  options = message.is_a?(Hash) ? message : { text: message }
  client.chat_postMessage(options.merge(channel: channel))
end

#textString?

Extracts the text content from the event



90
91
92
# File 'lib/bolt_rb/context.rb', line 90

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

#userString?

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