feat: Support Twilio Messaging Services (#4242)
This allows sending and receiving from multiple phone numbers using Twilio messaging services Fixes: #4204
This commit is contained in:
@@ -20,10 +20,9 @@ class Twilio::IncomingMessageService
|
||||
private
|
||||
|
||||
def twilio_inbox
|
||||
@twilio_inbox ||= ::Channel::TwilioSms.find_by!(
|
||||
account_sid: params[:AccountSid],
|
||||
phone_number: params[:To]
|
||||
)
|
||||
@twilio_inbox ||=
|
||||
::Channel::TwilioSms.find_by(messaging_service_sid: params[:MessagingServiceSid]) ||
|
||||
::Channel::TwilioSms.find_by!(account_sid: params[:AccountSid], phone_number: params[:To])
|
||||
end
|
||||
|
||||
def inbox
|
||||
|
||||
@@ -22,15 +22,7 @@ class Twilio::OneoffSmsCampaignService
|
||||
campaign.account.contacts.tagged_with(audience_labels, any: true).each do |contact|
|
||||
next if contact.phone_number.blank?
|
||||
|
||||
send_message(to: contact.phone_number, from: channel.phone_number, content: campaign.message)
|
||||
channel.send_message(to: contact.phone_number, body: campaign.message)
|
||||
end
|
||||
end
|
||||
|
||||
def send_message(to:, from:, content:)
|
||||
client.messages.create(body: content, from: from, to: to)
|
||||
end
|
||||
|
||||
def client
|
||||
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ class Twilio::SendOnTwilioService < Base::SendOnChannelService
|
||||
|
||||
def perform_reply
|
||||
begin
|
||||
twilio_message = client.messages.create(**message_params)
|
||||
twilio_message = channel.send_message(**message_params)
|
||||
rescue Twilio::REST::TwilioError => e
|
||||
ChatwootExceptionTracker.new(e, user: message.sender, account: message.account).capture_exception
|
||||
end
|
||||
@@ -15,13 +15,11 @@ class Twilio::SendOnTwilioService < Base::SendOnChannelService
|
||||
end
|
||||
|
||||
def message_params
|
||||
params = {
|
||||
{
|
||||
body: message.content,
|
||||
from: channel.phone_number,
|
||||
to: contact_inbox.source_id
|
||||
to: contact_inbox.source_id,
|
||||
media_url: attachments
|
||||
}
|
||||
params[:media_url] = attachments if message.attachments.present?
|
||||
params
|
||||
end
|
||||
|
||||
def attachments
|
||||
@@ -39,8 +37,4 @@ class Twilio::SendOnTwilioService < Base::SendOnChannelService
|
||||
def outgoing_message?
|
||||
message.outgoing? || message.template?
|
||||
end
|
||||
|
||||
def client
|
||||
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,28 @@ class Twilio::WebhookSetupService
|
||||
pattr_initialize [:inbox!]
|
||||
|
||||
def perform
|
||||
if channel.messaging_service_sid?
|
||||
update_messaging_service
|
||||
else
|
||||
update_phone_number
|
||||
end
|
||||
rescue Twilio::REST::TwilioError => e
|
||||
Rails.logger.error "TWILIO_FAILURE: #{e.message}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_messaging_service
|
||||
twilio_client
|
||||
.messaging.services(channel.messaging_service_sid)
|
||||
.update(
|
||||
inbound_method: 'POST',
|
||||
inbound_request_url: twilio_callback_index_url,
|
||||
use_inbound_webhook_on_number: false
|
||||
)
|
||||
end
|
||||
|
||||
def update_phone_number
|
||||
if phone_numbers.empty?
|
||||
Rails.logger.warn "TWILIO_PHONE_NUMBER_NOT_FOUND: #{channel.phone_number}"
|
||||
else
|
||||
@@ -11,12 +33,8 @@ class Twilio::WebhookSetupService
|
||||
.incoming_phone_numbers(phonenumber_sid)
|
||||
.update(sms_method: 'POST', sms_url: twilio_callback_index_url)
|
||||
end
|
||||
rescue Twilio::REST::TwilioError => e
|
||||
Rails.logger.error "TWILIO_FAILURE: #{e.message}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def phonenumber_sid
|
||||
phone_numbers.first.sid
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user