Class: Apologist::Channels::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/Apologist/channels/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ void



9
10
11
# File 'lib/Apologist/channels/client.rb', line 9

def initialize(client:)
  @client = client
end

Instance Method Details

#get_discord_channel_status(request_options: {}, **params) ⇒ Apologist::Channels::Types::GetDiscordChannelStatusResponse

Returns the status of the Discord channel. Used as a lightweight health/verification endpoint.

Examples:

client.channels.get_discord_channel_status(id: "id")

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/Apologist/channels/client.rb', line 28

def get_discord_channel_status(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/discord",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  if code.between?(200, 299)
    Apologist::Channels::Types::GetDiscordChannelStatusResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
end

#get_instagram_privacy_policy(request_options: {}, **params) ⇒ String

Returns a static HTML privacy policy page for the Instagram integration.

Examples:

client.channels.get_instagram_privacy_policy(id: "id")

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:

  • (String)

Raises:

  • (error_class)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/Apologist/channels/client.rb', line 213

def get_instagram_privacy_policy(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/instagram/privacy",
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end

#receive_discord_interaction(request_options: {}, **params) ⇒ untyped

Receives Discord interaction callbacks for the channel. Requests are verified via Ed25519 signature headers; unsigned or invalid requests are rejected. Payload shape is defined by Discord.

Examples:

client.channels.receive_discord_interaction(
  id: "id",
  signature_ed25519: "x-signature-ed25519",
  signature_timestamp: "x-signature-timestamp",
  body: {
    key: "value"
  }
)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)
  • :signature_ed25519 (String)
  • :signature_timestamp (String)

Returns:

  • (untyped)

Raises:

  • (error_class)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/Apologist/channels/client.rb', line 75

def receive_discord_interaction(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  path_param_names = %i[id]
  body_params = params.except(*path_param_names)

  headers = {}
  headers["x-signature-ed25519"] = params[:signature_ed25519] if params[:signature_ed25519]
  headers["x-signature-timestamp"] = params[:signature_timestamp] if params[:signature_timestamp]

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/discord",
    headers: headers,
    body: body_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end

#receive_facebook_message(request_options: {}, **params) ⇒ untyped

Receives Facebook/Messenger (and Instagram-style) message events for the channel. Payload shape is defined by Meta.

Examples:

client.channels.receive_facebook_message(
  id: "id",
  body: {
    key: "value"
  }
)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:

  • (untyped)

Raises:

  • (error_class)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/Apologist/channels/client.rb', line 174

def receive_facebook_message(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  path_param_names = %i[id]
  body_params = params.except(*path_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/facebook",
    body: body_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end

#receive_telegram_update(request_options: {}, **params) ⇒ untyped

Receives Telegram bot update events for the channel. Non-message updates are acknowledged and ignored. Payload shape is defined by Telegram.

Examples:

client.channels.receive_telegram_update(
  id: "id",
  body: {
    key: "value"
  }
)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:

  • (untyped)

Raises:

  • (error_class)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/Apologist/channels/client.rb', line 254

def receive_telegram_update(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  path_param_names = %i[id]
  body_params = params.except(*path_param_names)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/telegram",
    body: body_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end

#receive_twilio_message(request_options: {}, **params) ⇒ untyped

Receives inbound Twilio messages for the channel as form-encoded data. Payload fields are defined by Twilio.

Examples:

client.channels.receive_twilio_message(id: "id")

Parameters:

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

  • :id (String)

Returns:

  • (untyped)

Raises:

  • (error_class)


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/Apologist/channels/client.rb', line 293

def receive_twilio_message(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  request_data = Apologist::Channels::Types::ReceiveTwilioMessageRequest.new(params).to_h
  non_body_param_names = %w[id]
  body = request_data.except(*non_body_param_names)

  request = Apologist::Internal::UrlEncoded::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/twilio",
    body: body,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end

#verify_facebook_webhook(request_options: {}, **params) ⇒ String

Handles the Meta webhook verification handshake, echoing hub.challenge when hub.verify_token matches the channel's configured token.

Examples:

client.channels.verify_facebook_webhook(
  id: "id",
  hub_mode: "subscribe",
  hub_verify_token: "hub.verify_token"
)

Parameters:

  • request_options (Hash) (defaults to: {})
  • params (Hash)

Options Hash (request_options:):

  • :base_url (String)
  • :additional_headers (Hash{String => Object})
  • :additional_query_parameters (Hash{String => Object})
  • :additional_body_parameters (Hash{String => Object})
  • :timeout_in_seconds (Integer)

Options Hash (**params):

Returns:

  • (String)

Raises:

  • (error_class)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/Apologist/channels/client.rb', line 127

def verify_facebook_webhook(request_options: {}, **params)
  params = Apologist::Internal::Types::Utils.normalize_keys(params)
  query_params = {}
  query_params["hub.mode"] = params[:hub_mode] if params.key?(:hub_mode)
  query_params["hub.verify_token"] = params[:hub_verify_token] if params.key?(:hub_verify_token)
  query_params["hub.challenge"] = params[:hub_challenge] if params.key?(:hub_challenge)

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "GET",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/facebook",
    query: query_params,
    request_options: request_options
  )
  begin
    response = @client.send(request)
  rescue Net::HTTPRequestTimeout
    raise Apologist::Errors::TimeoutError
  end
  code = response.code.to_i
  return if code.between?(200, 299)

  error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
  raise error_class.new(response.body, code: code)
end