Class: FlowChat::Meta::MessagingGateway

Inherits:
Object
  • Object
show all
Includes:
GatewayAsyncSupport, Instrumentation, SignatureValidation, WebhookVerification
Defined in:
lib/flow_chat/meta/messaging_gateway.rb

Overview

The Messenger Platform envelope, shared by Facebook Messenger and Instagram DMs. Both deliver entry.messaging and both send through the same Send API, so the envelope is implemented once and each platform supplies only what actually differs.

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 GatewayIdentity

#configuration_error_class, #log_tag, #platform, #platform_label

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) ⇒ MessagingGateway

Returns a new instance of MessagingGateway.



17
18
19
20
21
22
23
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 17

def initialize(app, config = nil)
  @app = app
  @config = config || configuration_class.from_credentials
  @client = client_class.new(@config)

  FlowChat.logger.info { "#{log_tag}: Initialized #{platform} gateway for account #{@config.}" }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 15

def client
  @client
end

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 15

def context
  @context
end

Class Method Details

.configure_middleware_stack(builder, custom_middleware) ⇒ Object



42
43
44
45
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 42

def self.configure_middleware_stack(builder, custom_middleware)
  builder.use custom_middleware
  builder.use choice_mapper_class
end

Instance Method Details

#call(context) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 25

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

  unless in_background?
    if request.get? && request.params["hub.mode"] == "subscribe"
      return handle_verification(context)
    end
  end

  return handle_webhook(context) if request.post?

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

#client_classObject

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 57

def client_class
  raise NotImplementedError
end

#configuration_classObject

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 53

def configuration_class
  raise NotImplementedError
end

#expected_webhook_objectObject

Meta names the subscription this delivery came from. Messenger uses "page". Instagram's value depends on how the app is set up, so each platform states its own rather than sharing a guess.



68
69
70
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 68

def expected_webhook_object
  "page"
end

#gateway_nameObject

--- Hooks each platform overrides ---

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 49

def gateway_name
  raise NotImplementedError
end

#renderer_classObject

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/flow_chat/meta/messaging_gateway.rb', line 61

def renderer_class
  raise NotImplementedError
end