Class: FlowChat::Whatsapp::Gateway::CloudApi

Inherits:
Object
  • Object
show all
Includes:
GatewayAsyncSupport, Instrumentation, Meta::SignatureValidation, Meta::WebhookVerification
Defined in:
lib/flow_chat/whatsapp/gateway/cloud_api.rb

Constant Summary

Constants included from Instrumentation

Instrumentation::DELIVERED_MESSAGE_ID_KEY, Instrumentation::DELIVERY_DURATION_KEY

Instance Attribute Summary collapse

Attributes included from GatewayAsyncSupport

#controller

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Meta::GatewayIdentity

#log_tag

Methods included from GatewayAsyncSupport

#async_supported?, #enqueue_async_job, #extract_body_for_background, #extract_headers_for_background, #extract_host, #extract_path, #extract_remote_ip, #in_background?, #should_enqueue_async?, #side_events_already_published?

Methods included from Instrumentation

#elapsed_ms_since, #inbound_message?, #instrument, instrument, report_api_error, #report_delivery_failure, #report_delivery_success, #report_to_app, #report_to_subscribers

Constructor Details

#initialize(app, config = nil) ⇒ CloudApi

Returns a new instance of CloudApi.



15
16
17
18
19
20
21
22
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 15

def initialize(app, config = nil)
  @app = app
  @config = config || FlowChat::Whatsapp::Configuration.from_credentials
  @client = FlowChat::Whatsapp::Client.new(@config)

  FlowChat.logger.info { "CloudApi: Initialized WhatsApp Cloud API gateway with phone_number_id: #{@config.phone_number_id}" }
  FlowChat.logger.debug { "CloudApi: Gateway configuration - API base URL: #{FlowChat::Config.whatsapp.api_base_url}" }
end

Instance Attribute Details

#clientObject (readonly)

Expose client for out-of-band messaging



51
52
53
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 51

def client
  @client
end

#contextObject (readonly)

Returns the value of attribute context.



13
14
15
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 13

def context
  @context
end

Class Method Details

.configure_middleware_stack(builder, custom_middleware) ⇒ Object

Configure WhatsApp-specific middleware stack



54
55
56
57
58
59
60
61
62
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 54

def self.configure_middleware_stack(builder, custom_middleware)
  FlowChat.logger.debug { "CloudApi: Configuring WhatsApp middleware stack" }

  builder.use custom_middleware
  FlowChat.logger.debug { "CloudApi: Added custom middleware" }

  builder.use FlowChat::Whatsapp::Middleware::ChoiceMapper
  FlowChat.logger.debug { "CloudApi: Added Whatsapp::Middleware::ChoiceMapper" }
end

Instance Method Details

#call(context) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 24

def call(context)
  @context = context
  @controller = context.controller
  request = @controller.request

  FlowChat.logger.debug { "CloudApi: Processing #{request.request_method} request to #{request.path}" }

  # Skip webhook-specific handling in background mode
  unless in_background?
    # Handle webhook verification
    if request.get? && request.params["hub.mode"] == "subscribe"
      FlowChat.logger.info { "CloudApi: Handling webhook verification request" }
      return handle_verification(context)
    end
  end

  # Handle webhook messages
  if request.post?
    FlowChat.logger.info { "CloudApi: Handling webhook message (background: #{in_background?})" }
    return handle_webhook(context)
  end

  FlowChat.logger.warn { "CloudApi: Invalid request method or parameters - returning bad request" }
  @controller.head :bad_request
end

#configuration_error_classObject



77
78
79
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 77

def configuration_error_class
  FlowChat::Whatsapp::ConfigurationError
end

#platformObject

What this gateway is, for the shared Meta

modules. Public because

FlowChat::Meta::GatewayIdentity declares them public: a gateway saying which platform it speaks for is not a secret, unlike how it validates a signature. Keeping them here rather than below private means every Meta gateway answers these the same way.



69
70
71
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 69

def platform
  :whatsapp
end

#platform_labelObject



73
74
75
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 73

def platform_label
  "WhatsApp"
end