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

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

Instance Attribute Summary collapse

Attributes included from GatewayAsyncSupport

#controller

Class Method Summary collapse

Instance Method Summary collapse

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?

Methods included from Instrumentation

#instrument, instrument, report_api_error

Constructor Details

#initialize(app, config = nil) ⇒ CloudApi

Returns a new instance of CloudApi.



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

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



53
54
55
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 53

def client
  @client
end

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/flow_chat/whatsapp/gateway/cloud_api.rb', line 15

def context
  @context
end

Class Method Details

.configure_middleware_stack(builder, custom_middleware) ⇒ Object

Configure WhatsApp-specific middleware stack



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

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



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

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