class Stealth::Services::Facebook::MessageEvent

Attributes

params[R]
service_message[R]

Public Class Methods

new(service_message:, params:) click to toggle source
# File lib/stealth/services/facebook/events/message_event.rb, line 12
def initialize(service_message:, params:)
  @service_message = service_message
  @params = params
end

Public Instance Methods

process() click to toggle source
# File lib/stealth/services/facebook/events/message_event.rb, line 17
def process
  fetch_message
  fetch_location
  fetch_attachments
end

Private Instance Methods

fetch_attachments() click to toggle source
# File lib/stealth/services/facebook/events/message_event.rb, line 50
def fetch_attachments
  if params.dig('message', 'attachments').present? && params.dig('message', 'attachments').is_a?(Array)
    params.dig('message', 'attachments').each do |attachment|
      # Seems to be a bug in Messenger, but in attachments of type `fallback`
      # we are seeing the URL come in at the attachment-level rather than
      # nested within the payload as the API specifies:
      # https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages
      payload_url = if attachment.dig('payload', 'url').present?
        attachment['payload']['url']
      else
        attachment['url']
      end

      service_message.attachments << {
        type: attachment['type'],
        url: payload_url
      }
    end
  end
end
fetch_location() click to toggle source
# File lib/stealth/services/facebook/events/message_event.rb, line 34
def fetch_location
  if params.dig('message', 'attachments').present? && params.dig('message', 'attachments').is_a?(Array)
    params.dig('message', 'attachments').each do |attachment|
      next unless attachment['type'] == 'location'

      lat = attachment.dig('payload', 'coordinates', 'lat')
      lng = attachment.dig('payload', 'coordinates', 'long')

      service_message.location = {
        lat: lat,
        lng: lng
      }
    end
  end
end
fetch_message() click to toggle source
# File lib/stealth/services/facebook/events/message_event.rb, line 25
def fetch_message
  if params.dig('message', 'quick_reply').present?
    service_message.message = params.dig('message', 'text')
    service_message.payload = params.dig('message', 'quick_reply', 'payload')
  elsif params.dig('message', 'text').present?
    service_message.message = params.dig('message', 'text')
  end
end