2020-04-05 16:41:27 +00:00
|
|
|
class Twilio::IncomingMessageService
|
2020-04-29 20:11:13 +00:00
|
|
|
include ::FileTypeHelper
|
|
|
|
|
2020-04-05 16:41:27 +00:00
|
|
|
pattr_initialize [:params!]
|
|
|
|
|
|
|
|
def perform
|
2022-07-14 13:16:07 +00:00
|
|
|
return if twilio_channel.blank?
|
|
|
|
|
2020-04-05 16:41:27 +00:00
|
|
|
set_contact
|
|
|
|
set_conversation
|
2022-09-13 12:10:06 +00:00
|
|
|
@message = @conversation.messages.create!(
|
2020-04-05 16:41:27 +00:00
|
|
|
content: params[:Body],
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
inbox_id: @inbox.id,
|
|
|
|
message_type: :incoming,
|
2020-06-27 16:04:53 +00:00
|
|
|
sender: @contact,
|
2020-04-05 16:41:27 +00:00
|
|
|
source_id: params[:SmsSid]
|
|
|
|
)
|
2020-04-29 20:11:13 +00:00
|
|
|
attach_files
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-07-14 13:16:07 +00:00
|
|
|
def twilio_channel
|
|
|
|
@twilio_channel ||= ::Channel::TwilioSms.find_by(messaging_service_sid: params[:MessagingServiceSid]) if params[:MessagingServiceSid].present?
|
|
|
|
if params[:AccountSid].present? && params[:To].present?
|
|
|
|
@twilio_channel ||= ::Channel::TwilioSms.find_by!(account_sid: params[:AccountSid],
|
|
|
|
phone_number: params[:To])
|
|
|
|
end
|
|
|
|
@twilio_channel
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def inbox
|
2022-07-14 13:16:07 +00:00
|
|
|
@inbox ||= twilio_channel.inbox
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def account
|
|
|
|
@account ||= inbox.account
|
|
|
|
end
|
|
|
|
|
2020-04-29 20:11:13 +00:00
|
|
|
def phone_number
|
2022-07-14 13:16:07 +00:00
|
|
|
twilio_channel.sms? ? params[:From] : params[:From].gsub('whatsapp:', '')
|
2020-04-29 20:11:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_phone_number
|
|
|
|
TelephoneNumber.parse(phone_number).international_number
|
|
|
|
end
|
|
|
|
|
2020-04-05 16:41:27 +00:00
|
|
|
def set_contact
|
|
|
|
contact_inbox = ::ContactBuilder.new(
|
|
|
|
source_id: params[:From],
|
|
|
|
inbox: inbox,
|
|
|
|
contact_attributes: contact_attributes
|
|
|
|
).perform
|
|
|
|
|
|
|
|
@contact_inbox = contact_inbox
|
|
|
|
@contact = contact_inbox.contact
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
inbox_id: @inbox.id,
|
|
|
|
contact_id: @contact.id,
|
|
|
|
contact_inbox_id: @contact_inbox.id,
|
|
|
|
additional_attributes: additional_attributes
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_conversation
|
|
|
|
@conversation = @contact_inbox.conversations.first
|
|
|
|
return if @conversation
|
|
|
|
|
|
|
|
@conversation = ::Conversation.create!(conversation_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def contact_attributes
|
|
|
|
{
|
2020-04-29 20:11:13 +00:00
|
|
|
name: formatted_phone_number,
|
|
|
|
phone_number: phone_number,
|
|
|
|
additional_attributes: additional_attributes
|
2020-04-05 16:41:27 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def additional_attributes
|
2022-07-14 13:16:07 +00:00
|
|
|
if twilio_channel.sms?
|
2020-04-29 20:11:13 +00:00
|
|
|
{
|
|
|
|
from_zip_code: params[:FromZip],
|
|
|
|
from_country: params[:FromCountry],
|
|
|
|
from_state: params[:FromState]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach_files
|
|
|
|
return if params[:MediaUrl0].blank?
|
|
|
|
|
2021-08-11 11:10:28 +00:00
|
|
|
attachment_file = Down.download(
|
|
|
|
params[:MediaUrl0]
|
|
|
|
)
|
2020-04-29 20:11:13 +00:00
|
|
|
|
|
|
|
attachment = @message.attachments.new(
|
|
|
|
account_id: @message.account_id,
|
|
|
|
file_type: file_type(params[:MediaContentType0])
|
|
|
|
)
|
|
|
|
|
|
|
|
attachment.file.attach(
|
2021-08-11 11:10:28 +00:00
|
|
|
io: attachment_file,
|
|
|
|
filename: attachment_file.original_filename,
|
|
|
|
content_type: attachment_file.content_type
|
2020-04-29 20:11:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@message.save!
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
end
|