Feature: Twilio Whatsapp Integration (#779)
Twilio Whatsapp Integration Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
class Twilio::IncomingMessageService
|
||||
include ::FileTypeHelper
|
||||
|
||||
pattr_initialize [:params!]
|
||||
|
||||
def perform
|
||||
set_contact
|
||||
set_conversation
|
||||
@conversation.messages.create(
|
||||
@message = @conversation.messages.create(
|
||||
content: params[:Body],
|
||||
account_id: @inbox.account_id,
|
||||
inbox_id: @inbox.id,
|
||||
@@ -12,6 +14,7 @@ class Twilio::IncomingMessageService
|
||||
contact_id: @contact.id,
|
||||
source_id: params[:SmsSid]
|
||||
)
|
||||
attach_files
|
||||
end
|
||||
|
||||
private
|
||||
@@ -31,6 +34,14 @@ class Twilio::IncomingMessageService
|
||||
@account ||= inbox.account
|
||||
end
|
||||
|
||||
def phone_number
|
||||
twilio_inbox.sms? ? params[:From] : params[:From].gsub('whatsapp:', '')
|
||||
end
|
||||
|
||||
def formatted_phone_number
|
||||
TelephoneNumber.parse(phone_number).international_number
|
||||
end
|
||||
|
||||
def set_contact
|
||||
contact_inbox = ::ContactBuilder.new(
|
||||
source_id: params[:From],
|
||||
@@ -61,17 +72,40 @@ class Twilio::IncomingMessageService
|
||||
|
||||
def contact_attributes
|
||||
{
|
||||
name: params[:From],
|
||||
phone_number: params[:From],
|
||||
contact_attributes: additional_attributes
|
||||
name: formatted_phone_number,
|
||||
phone_number: phone_number,
|
||||
additional_attributes: additional_attributes
|
||||
}
|
||||
end
|
||||
|
||||
def additional_attributes
|
||||
{
|
||||
from_zip_code: params[:FromZip],
|
||||
from_country: params[:FromCountry],
|
||||
from_state: params[:FromState]
|
||||
}
|
||||
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!
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,11 +7,7 @@ class Twilio::OutgoingMessageService
|
||||
return if inbox.channel.class.to_s != 'Channel::TwilioSms'
|
||||
return unless message.outgoing?
|
||||
|
||||
twilio_message = client.messages.create(
|
||||
body: message.content,
|
||||
from: channel.phone_number,
|
||||
to: contact.phone_number
|
||||
)
|
||||
twilio_message = client.messages.create(message_params)
|
||||
message.update!(source_id: twilio_message.sid)
|
||||
end
|
||||
|
||||
@@ -19,6 +15,21 @@ class Twilio::OutgoingMessageService
|
||||
|
||||
delegate :conversation, to: :message
|
||||
delegate :contact, to: :conversation
|
||||
delegate :contact_inbox, to: :conversation
|
||||
|
||||
def message_params
|
||||
params = {
|
||||
body: message.content,
|
||||
from: channel.phone_number,
|
||||
to: contact_inbox.source_id
|
||||
}
|
||||
params[:media_url] = attachments if channel.whatsapp? && message.attachments.present?
|
||||
params
|
||||
end
|
||||
|
||||
def attachments
|
||||
message.attachments.map(&:file_url)
|
||||
end
|
||||
|
||||
def inbox
|
||||
@inbox ||= message.inbox
|
||||
|
||||
Reference in New Issue
Block a user