Chatwoot/lib/integrations/facebook/message_creator.rb

44 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Integrations::Facebook::MessageCreator
attr_reader :response
def initialize(response)
@response = response
end
def perform
# begin
2020-07-21 06:45:24 +00:00
if agent_message_via_echo?
create_agent_message
else
2020-07-21 06:45:24 +00:00
create_contact_message
end
# rescue => e
# Raven.capture_exception(e)
# end
end
private
2020-07-21 06:45:24 +00:00
def agent_message_via_echo?
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
end
2020-07-21 06:45:24 +00:00
def create_agent_message
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)
mb.perform
end
end
2020-07-21 06:45:24 +00:00
def create_contact_message
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)
mb.perform
end
end
end