2019-10-16 06:52:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
module Integrations
|
|
|
|
module Facebook
|
|
|
|
class MessageCreator
|
|
|
|
attr_reader :response
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
def initialize(response)
|
|
|
|
@response = response
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
def perform
|
|
|
|
# begin
|
|
|
|
if outgoing_message_via_echo?
|
|
|
|
create_outgoing_message
|
|
|
|
else
|
|
|
|
create_incoming_message
|
|
|
|
end
|
|
|
|
# rescue => e
|
|
|
|
# Raven.capture_exception(e)
|
|
|
|
# end
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
private
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
def outgoing_message_via_echo?
|
|
|
|
response.echo? && !response.sent_from_chatwoot_app?
|
|
|
|
# this means that it is an outgoing message from page, but not sent from chatwoot.
|
|
|
|
# User can send from fb page directly on mobile messenger, so this case should be handled as outgoing message
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
def create_outgoing_message
|
|
|
|
Channel::FacebookPage.where(page_id: response.sender_id).each do |page|
|
|
|
|
mb = Messages::Outgoing::EchoBuilder.new(response, page.inbox, true)
|
|
|
|
mb.perform
|
|
|
|
end
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 19:10:18 +00:00
|
|
|
def create_incoming_message
|
|
|
|
Channel::FacebookPage.where(page_id: response.recipient_id).each do |page|
|
|
|
|
mb = Messages::IncomingMessageBuilder.new(response, page.inbox)
|
|
|
|
mb.perform
|
|
|
|
end
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|