2019-08-14 09:48:44 +00:00
|
|
|
require 'open-uri'
|
|
|
|
class Messages::MessageBuilder
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
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, fb_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.
|
|
|
|
=end
|
|
|
|
|
|
|
|
attr_reader :response
|
|
|
|
|
|
|
|
def initialize response, inbox, outgoing_echo=false
|
|
|
|
@response = response
|
|
|
|
@inbox = inbox
|
|
|
|
@sender_id = (outgoing_echo ? @response.recipient_id : @response.sender_id)
|
|
|
|
@message_type = (outgoing_echo ? :outgoing : :incoming)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform #for incoming
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
build_contact
|
|
|
|
build_conversation
|
|
|
|
build_message
|
|
|
|
end
|
|
|
|
#build_attachments
|
|
|
|
rescue => e
|
|
|
|
Raven.capture_exception(e)
|
|
|
|
#change this asap
|
|
|
|
return true
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_attachments
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-08-31 06:22:09 +00:00
|
|
|
def contact
|
|
|
|
@contact ||= @inbox.contacts.find_by(source_id: @sender_id)
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def build_contact
|
2019-08-31 06:22:09 +00:00
|
|
|
if contact.nil?
|
|
|
|
@contact = @inbox.contacts.create!(contact_params)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_message
|
|
|
|
@message = @conversation.messages.new(message_params)
|
|
|
|
(response.attachments || []).each do |attachment|
|
|
|
|
@message.build_attachment(attachment_params(attachment))
|
|
|
|
end
|
|
|
|
@message.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_conversation
|
|
|
|
@conversation ||=
|
|
|
|
if (conversation = Conversation.find_by(conversation_params))
|
|
|
|
conversation
|
|
|
|
else
|
|
|
|
Conversation.create!(conversation_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachment_params(attachment)
|
|
|
|
file_type = attachment['type'].to_sym
|
2019-10-05 09:15:32 +00:00
|
|
|
params = { file_type: file_type, account_id: @message.account_id }
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
if [:image, :file, :audio, :video].include? file_type
|
2019-10-05 09:15:32 +00:00
|
|
|
params.merge!(file_type_params(attachment))
|
2019-08-14 09:48:44 +00:00
|
|
|
elsif file_type == :location
|
2019-10-05 09:15:32 +00:00
|
|
|
params.merge!(location_params(attachment))
|
2019-08-14 09:48:44 +00:00
|
|
|
elsif file_type == :fallback
|
2019-10-05 09:15:32 +00:00
|
|
|
params.merge!(fallback_params(attachment))
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2019-10-05 09:15:32 +00:00
|
|
|
def file_type_params(attachment)
|
|
|
|
{
|
|
|
|
external_url: attachment['payload']['url'],
|
|
|
|
remote_file_url: attachment['payload']['url']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def location_params(attachment)
|
|
|
|
lat, long = attachment['payload']['coordinates']['lat'], 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
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
inbox_id: @inbox.id,
|
2019-08-31 06:22:09 +00:00
|
|
|
sender_id: contact.id
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_params
|
|
|
|
{
|
|
|
|
account_id: @conversation.account_id,
|
|
|
|
inbox_id: @conversation.inbox_id,
|
|
|
|
message_type: @message_type,
|
|
|
|
content: response.content,
|
|
|
|
fb_id: response.identifier
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def contact_params
|
2019-10-06 07:29:54 +00:00
|
|
|
begin
|
|
|
|
k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook?
|
|
|
|
result = k.get_object(@sender_id) || {}
|
|
|
|
rescue Exception => e
|
|
|
|
result = {}
|
|
|
|
Raven.capture_exception(e)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2019-10-06 07:29:54 +00:00
|
|
|
params = {
|
|
|
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
source_id: @sender_id,
|
|
|
|
remote_avatar_url: result['profile_pic'] || nil
|
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|