Class: Stealth::Services::Facebook::MessageHandler

Inherits:
BaseMessageHandler
  • Object
show all
Defined in:
lib/stealth/services/facebook/message_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, headers:) ⇒ MessageHandler

Returns a new instance of MessageHandler.



17
18
19
20
# File 'lib/stealth/services/facebook/message_handler.rb', line 17

def initialize(params:, headers:)
  @params = params
  @headers = headers
end

Instance Attribute Details

#facebook_messageObject (readonly)

Returns the value of attribute facebook_message.



15
16
17
# File 'lib/stealth/services/facebook/message_handler.rb', line 15

def facebook_message
  @facebook_message
end

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/stealth/services/facebook/message_handler.rb', line 15

def headers
  @headers
end

#paramsObject (readonly)

Returns the value of attribute params.



15
16
17
# File 'lib/stealth/services/facebook/message_handler.rb', line 15

def params
  @params
end

#service_messageObject (readonly)

Returns the value of attribute service_message.



15
16
17
# File 'lib/stealth/services/facebook/message_handler.rb', line 15

def service_message
  @service_message
end

Instance Method Details

#coordinateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/stealth/services/facebook/message_handler.rb', line 22

def coordinate
  if facebook_is_validating_webhook?
    respond_with_validation
  else
    # Queue the request processing so we can respond quickly to FB
    # and also keep track of this message
    Stealth::Services::HandleMessageJob.perform_async(
      'facebook',
      params,
      headers
    )

    # Relay our acceptance
    [200, 'OK']
  end
end

#processObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/stealth/services/facebook/message_handler.rb', line 39

def process
  @service_message = ServiceMessage.new(service: 'facebook')
  @facebook_message = params['entry'].first['messaging'].first
  service_message.sender_id = get_sender_id
  service_message.target_id = get_target_id
  service_message.timestamp = get_timestamp
  process_facebook_event

  service_message
end