class Xip::Services::Facebook::MessageEvent
Attributes
params[R]
service_message[R]
Public Class Methods
new(service_message:, params:)
click to toggle source
# File lib/xip/services/facebook/events/message_event.rb, line 11 def initialize(service_message:, params:) @service_message = service_message @params = params end
Public Instance Methods
process()
click to toggle source
# File lib/xip/services/facebook/events/message_event.rb, line 16 def process fetch_message fetch_location fetch_attachments end
Private Instance Methods
fetch_attachments()
click to toggle source
# File lib/xip/services/facebook/events/message_event.rb, line 49 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/xip/services/facebook/events/message_event.rb, line 33 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/xip/services/facebook/events/message_event.rb, line 24 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