2019-10-16 06:52:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
class Integrations::Facebook::MessageCreator
|
|
|
|
attr_reader :response
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def initialize(response)
|
|
|
|
@response = response
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def perform
|
|
|
|
# begin
|
2020-07-21 06:45:24 +00:00
|
|
|
if agent_message_via_echo?
|
|
|
|
create_agent_message
|
2020-01-09 07:36:40 +00:00
|
|
|
else
|
2020-07-21 06:45:24 +00:00
|
|
|
create_contact_message
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
# rescue => e
|
|
|
|
# Raven.capture_exception(e)
|
|
|
|
# end
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
private
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def agent_message_via_echo?
|
2020-01-09 07:36:40 +00:00
|
|
|
response.echo? && !response.sent_from_chatwoot_app?
|
2020-07-21 06:45:24 +00:00
|
|
|
# this means that it is an agent message from page, but not sent from chatwoot.
|
|
|
|
# User can send from fb page directly on mobile / web messenger, so this case should be handled as agent message
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def create_agent_message
|
2020-01-09 07:36:40 +00:00
|
|
|
Channel::FacebookPage.where(page_id: response.sender_id).each do |page|
|
2020-09-08 05:54:08 +00:00
|
|
|
mb = Messages::Facebook::MessageBuilder.new(response, page.inbox, outgoing_echo: true)
|
2020-01-09 07:36:40 +00:00
|
|
|
mb.perform
|
|
|
|
end
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def create_contact_message
|
2020-01-09 07:36:40 +00:00
|
|
|
Channel::FacebookPage.where(page_id: response.recipient_id).each do |page|
|
2020-07-21 06:45:24 +00:00
|
|
|
mb = Messages::Facebook::MessageBuilder.new(response, page.inbox)
|
2020-01-09 07:36:40 +00:00
|
|
|
mb.perform
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|