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)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/Apologist/channels/client.rb', line 301

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

#get_line_channel_status(request_options: {}, **params) ⇒ Apologist::Channels::Types::GetLineChannelStatusResponse

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

Examples:

client.channels.get_line_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:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/Apologist/channels/client.rb', line 119

def get_line_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)}/line",
    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::GetLineChannelStatusResponse.load(response.body)
  else
    error_class = Apologist::Errors::ResponseError.subclass_for_code(code)
    raise error_class.new(response.body, code: code)
  end
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)


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/Apologist/channels/client.rb', line 262

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_line_webhook(request_options: {}, **params) ⇒ untyped

Receives LINE Messaging API webhook events for the channel. Requests are verified via the x-line-signature HMAC-SHA256 (Base64) header using the channel secret unless an api_key is present. Payload shape is defined by LINE. The route acknowledges quickly and processes text message and follow events asynchronously.

Examples:

client.channels.receive_line_webhook(
  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)
  • :line_signature (String, nil)

Returns:

  • (untyped)

Raises:

  • (error_class)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/Apologist/channels/client.rb', line 164

def receive_line_webhook(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-line-signature"] = params[:line_signature] if params[:line_signature]

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/line",
    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_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)


342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/Apologist/channels/client.rb', line 342

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)


381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/Apologist/channels/client.rb', line 381

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

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

Receives WhatsApp Cloud API message events for the channel. Payload shape is defined by Meta. Signature verification via x-hub-signature-256 is used when the channel has an App Secret configured; otherwise the webhook relies on URL secrecy and/or an api_key query parameter.

Examples:

client.channels.receive_whats_app_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)
  • :hub_signature256 (String, nil)

Returns:

  • (untyped)

Raises:

  • (error_class)


478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/Apologist/channels/client.rb', line 478

def receive_whats_app_message(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-hub-signature-256"] = params[:hub_signature256] if params[:hub_signature256]

  request = Apologist::Internal::JSON::Request.new(
    base_url: request_options[:base_url],
    method: "POST",
    path: "channels/#{URI.encode_uri_component(params[:id].to_s)}/whatsapp",
    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

#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)


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/Apologist/channels/client.rb', line 215

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

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

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

Examples:

client.channels.verify_whats_app_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)


429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/Apologist/channels/client.rb', line 429

def verify_whats_app_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)}/whatsapp",
    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