2020-07-21 06:45:24 +00:00
|
|
|
# This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
|
|
|
|
# Assumptions
|
|
|
|
# 1. Incase of an outgoing message which is echo, source_id will NOT be nil,
|
|
|
|
# based on this we are showing "not sent from chatwoot" message in frontend
|
|
|
|
# Hence there is no need to set user_id in message for outgoing echo messages.
|
|
|
|
|
2021-10-05 09:05:32 +00:00
|
|
|
class Messages::Facebook::MessageBuilder < Messages::Messenger::MessageBuilder
|
2020-07-21 06:45:24 +00:00
|
|
|
attr_reader :response
|
|
|
|
|
2020-09-08 05:54:08 +00:00
|
|
|
def initialize(response, inbox, outgoing_echo: false)
|
2021-10-05 09:05:32 +00:00
|
|
|
super()
|
2020-07-21 06:45:24 +00:00
|
|
|
@response = response
|
|
|
|
@inbox = inbox
|
2020-09-04 13:43:47 +00:00
|
|
|
@outgoing_echo = outgoing_echo
|
|
|
|
@sender_id = (@outgoing_echo ? @response.recipient_id : @response.sender_id)
|
|
|
|
@message_type = (@outgoing_echo ? :outgoing : :incoming)
|
2021-06-07 08:28:01 +00:00
|
|
|
@attachments = (@response.attachments || [])
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2021-08-01 12:45:39 +00:00
|
|
|
# This channel might require reauthorization, may be owner might have changed the fb password
|
|
|
|
return if @inbox.channel.reauthorization_required?
|
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
build_contact
|
|
|
|
build_message
|
|
|
|
end
|
2021-08-11 11:10:28 +00:00
|
|
|
ensure_contact_avatar
|
2021-08-01 12:45:39 +00:00
|
|
|
rescue Koala::Facebook::AuthenticationError
|
2022-03-28 12:44:30 +00:00
|
|
|
Rails.logger.error "Facebook Authorization expired for Inbox #{@inbox.id}"
|
2020-07-21 06:45:24 +00:00
|
|
|
rescue StandardError => e
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception
|
2020-07-21 06:45:24 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def contact
|
|
|
|
@contact ||= @inbox.contact_inboxes.find_by(source_id: @sender_id)&.contact
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_contact
|
|
|
|
return if contact.present?
|
|
|
|
|
|
|
|
@contact = Contact.create!(contact_params.except(:remote_avatar_url))
|
2022-05-06 09:20:55 +00:00
|
|
|
@contact_inbox = ContactInbox.find_or_create_by!(contact: contact, inbox: @inbox, source_id: @sender_id)
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_message
|
|
|
|
@message = conversation.messages.create!(message_params)
|
2021-10-05 09:05:32 +00:00
|
|
|
|
2021-06-07 08:28:01 +00:00
|
|
|
@attachments.each do |attachment|
|
|
|
|
process_attachment(attachment)
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-11 11:10:28 +00:00
|
|
|
def ensure_contact_avatar
|
|
|
|
return if contact_params[:remote_avatar_url].blank?
|
|
|
|
return if @contact.avatar.attached?
|
|
|
|
|
|
|
|
ContactAvatarJob.perform_later(@contact, contact_params[:remote_avatar_url])
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def conversation
|
|
|
|
@conversation ||= Conversation.find_by(conversation_params) || build_conversation
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_conversation
|
|
|
|
@contact_inbox ||= contact.contact_inboxes.find_by!(source_id: @sender_id)
|
|
|
|
Conversation.create!(conversation_params.merge(
|
|
|
|
contact_inbox_id: @contact_inbox.id
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
def location_params(attachment)
|
|
|
|
lat = attachment['payload']['coordinates']['lat']
|
|
|
|
long = attachment['payload']['coordinates']['long']
|
|
|
|
{
|
|
|
|
external_url: attachment['url'],
|
|
|
|
coordinates_lat: lat,
|
|
|
|
coordinates_long: long,
|
|
|
|
fallback_title: attachment['title']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallback_params(attachment)
|
|
|
|
{
|
|
|
|
fallback_title: attachment['title'],
|
|
|
|
external_url: attachment['url']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
inbox_id: @inbox.id,
|
|
|
|
contact_id: contact.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_params
|
|
|
|
{
|
|
|
|
account_id: conversation.account_id,
|
|
|
|
inbox_id: conversation.inbox_id,
|
|
|
|
message_type: @message_type,
|
|
|
|
content: response.content,
|
|
|
|
source_id: response.identifier,
|
2020-09-04 13:43:47 +00:00
|
|
|
sender: @outgoing_echo ? nil : contact
|
2020-07-21 06:45:24 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-15 12:42:56 +00:00
|
|
|
def process_contact_params_result(result)
|
|
|
|
{
|
|
|
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
remote_avatar_url: result['profile_pic'] || ''
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-07-21 06:45:24 +00:00
|
|
|
def contact_params
|
|
|
|
begin
|
|
|
|
k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook?
|
|
|
|
result = k.get_object(@sender_id) || {}
|
2021-08-01 12:45:39 +00:00
|
|
|
rescue Koala::Facebook::AuthenticationError
|
|
|
|
@inbox.channel.authorization_error!
|
|
|
|
raise
|
2021-09-15 12:42:56 +00:00
|
|
|
rescue Koala::Facebook::ClientError => e
|
|
|
|
result = {}
|
|
|
|
# OAuthException, code: 100, error_subcode: 2018218, message: (#100) No profile available for this user
|
|
|
|
# We don't need to capture this error as we don't care about contact params in case of echo messages
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception unless @outgoing_echo
|
2020-07-21 06:45:24 +00:00
|
|
|
rescue StandardError => e
|
|
|
|
result = {}
|
2022-05-09 08:53:19 +00:00
|
|
|
ChatwootExceptionTracker.new(e, account: @inbox.account).capture_exception
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
2021-09-15 12:42:56 +00:00
|
|
|
process_contact_params_result(result)
|
2020-07-21 06:45:24 +00:00
|
|
|
end
|
|
|
|
end
|