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
|
|
|
|
set_contact
|
|
|
|
set_conversation
|
2020-04-29 20:11:13 +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,
|
|
|
|
contact_id: @contact.id,
|
|
|
|
source_id: params[:SmsSid]
|
|
|
|
)
|
2020-04-29 20:11:13 +00:00
|
|
|
attach_files
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def twilio_inbox
|
|
|
|
@twilio_inbox ||= ::Channel::TwilioSms.find_by!(
|
|
|
|
account_sid: params[:AccountSid],
|
|
|
|
phone_number: params[:To]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def inbox
|
|
|
|
@inbox ||= twilio_inbox.inbox
|
|
|
|
end
|
|
|
|
|
|
|
|
def account
|
|
|
|
@account ||= inbox.account
|
|
|
|
end
|
|
|
|
|
2020-04-29 20:11:13 +00:00
|
|
|
def phone_number
|
|
|
|
twilio_inbox.sms? ? params[:From] : params[:From].gsub('whatsapp:', '')
|
|
|
|
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
|
2020-04-29 20:11:13 +00:00
|
|
|
if twilio_inbox.sms?
|
|
|
|
{
|
|
|
|
from_zip_code: params[:FromZip],
|
|
|
|
from_country: params[:FromCountry],
|
|
|
|
from_state: params[:FromState]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attach_files
|
|
|
|
return if params[:MediaUrl0].blank?
|
|
|
|
|
|
|
|
file_resource = LocalResource.new(params[:MediaUrl0], params[:MediaContentType0])
|
|
|
|
|
|
|
|
attachment = @message.attachments.new(
|
|
|
|
account_id: @message.account_id,
|
|
|
|
file_type: file_type(params[:MediaContentType0])
|
|
|
|
)
|
|
|
|
|
|
|
|
attachment.file.attach(
|
|
|
|
io: file_resource.file,
|
|
|
|
filename: file_resource.tmp_filename,
|
|
|
|
content_type: file_resource.encoding
|
|
|
|
)
|
|
|
|
|
|
|
|
@message.save!
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
end
|