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:
Jordan Brough
2022-07-08 06:50:07 -06:00
committed by GitHub
parent fdf449dc87
commit 49d08a6773
22 changed files with 379 additions and 105 deletions

View File

@@ -38,6 +38,7 @@ class Api::V1::Accounts::Channels::TwilioChannelsController < Api::V1::Accounts:
@twilio_channel = Current.account.twilio_sms.create!(
account_sid: permitted_params[:account_sid],
auth_token: permitted_params[:auth_token],
messaging_service_sid: permitted_params[:messaging_service_sid],
phone_number: phone_number,
medium: medium
)
@@ -49,7 +50,7 @@ class Api::V1::Accounts::Channels::TwilioChannelsController < Api::V1::Accounts:
def permitted_params
params.require(:twilio_channel).permit(
:account_id, :phone_number, :account_sid, :auth_token, :name, :medium
:account_id, :messaging_service_sid, :phone_number, :account_sid, :auth_token, :name, :medium
)
end
end

View File

@@ -7,7 +7,7 @@ class Twilio::CallbackController < ApplicationController
private
def permitted_params
def permitted_params # rubocop:disable Metrics/MethodLength
params.permit(
:ApiVersion,
:SmsSid,
@@ -25,7 +25,8 @@ class Twilio::CallbackController < ApplicationController
:ToCountry,
:FromState,
:MediaUrl0,
:MediaContentType0
:MediaContentType0,
:MessagingServiceSid
)
end
end

View File

@@ -12,7 +12,7 @@ export const getInboxClassByType = (type, phoneNumber) => {
return 'brand-twitter';
case INBOX_TYPES.TWILIO:
return phoneNumber.startsWith('whatsapp')
return phoneNumber?.startsWith('whatsapp')
? 'brand-whatsapp'
: 'brand-sms';

View File

@@ -111,6 +111,12 @@
"PLACEHOLDER": "Please enter your Twilio Account SID",
"ERROR": "This field is required"
},
"MESSAGING_SERVICE_SID": {
"LABEL": "Messaging Service SID",
"PLACEHOLDER": "Please enter your Twilio Messaging Service SID",
"ERROR": "This field is required",
"USE_MESSAGING_SERVICE": "Use a Twilio Messaging Service"
},
"CHANNEL_TYPE": {
"LABEL": "Channel Type",
"ERROR": "Please select your Channel Type"

View File

@@ -438,7 +438,11 @@ export default {
return this.$store.getters['inboxes/getInbox'](this.currentInboxId);
},
inboxName() {
if (this.isATwilioSMSChannel || this.isAWhatsappChannel) {
if (this.isATwilioSMSChannel || this.isATwilioWhatsappChannel) {
return `${this.inbox.name} (${this.inbox.messaging_service_sid ||
this.inbox.phone_number})`;
}
if (this.isAWhatsappChannel) {
return `${this.inbox.name} (${this.inbox.phone_number})`;
}
if (this.isAnEmailChannel) {

View File

@@ -17,6 +17,26 @@
</div>
<div class="medium-8 columns">
<label
v-if="useMessagingService"
:class="{ error: $v.messagingServiceSID.$error }"
>
{{ $t('INBOX_MGMT.ADD.TWILIO.MESSAGING_SERVICE_SID.LABEL') }}
<input
v-model.trim="messagingServiceSID"
type="text"
:placeholder="
$t('INBOX_MGMT.ADD.TWILIO.MESSAGING_SERVICE_SID.PLACEHOLDER')
"
@blur="$v.messagingServiceSID.$touch"
/>
<span v-if="$v.messagingServiceSID.$error" class="message">{{
$t('INBOX_MGMT.ADD.TWILIO.MESSAGING_SERVICE_SID.ERROR')
}}</span>
</label>
</div>
<div v-if="!useMessagingService" class="medium-8 columns">
<label :class="{ error: $v.phoneNumber.$error }">
{{ $t('INBOX_MGMT.ADD.TWILIO.PHONE_NUMBER.LABEL') }}
<input
@@ -31,6 +51,22 @@
</label>
</div>
<div class="medium-8 columns messagingServiceHelptext">
<label for="useMessagingService">
<input
id="useMessagingService"
v-model="useMessagingService"
type="checkbox"
class="checkbox"
/>
{{
$t(
'INBOX_MGMT.ADD.TWILIO.MESSAGING_SERVICE_SID.USE_MESSAGING_SERVICE'
)
}}
</label>
</div>
<div class="medium-8 columns">
<label :class="{ error: $v.accountSID.$error }">
{{ $t('INBOX_MGMT.ADD.TWILIO.ACCOUNT_SID.LABEL') }}
@@ -91,6 +127,8 @@ export default {
authToken: '',
medium: this.type,
channelName: '',
messagingServiceSID: '',
useMessagingService: false,
phoneNumber: '',
};
},
@@ -99,12 +137,25 @@ export default {
uiFlags: 'inboxes/getUIFlags',
}),
},
validations: {
channelName: { required },
phoneNumber: { required, shouldStartWithPlusSign },
authToken: { required },
accountSID: { required },
medium: { required },
validations() {
if (this.phoneNumber) {
return {
channelName: { required },
messagingServiceSID: {},
phoneNumber: { shouldStartWithPlusSign },
authToken: { required },
accountSID: { required },
medium: { required },
};
}
return {
channelName: { required },
messagingServiceSID: { required },
phoneNumber: {},
authToken: { required },
accountSID: { required },
medium: { required },
};
},
methods: {
async createChannel() {
@@ -122,7 +173,10 @@ export default {
medium: this.medium,
account_sid: this.accountSID,
auth_token: this.authToken,
phone_number: `+${this.phoneNumber.replace(/\D/g, '')}`,
messaging_service_sid: this.messagingServiceSID,
phone_number: this.messagingServiceSID
? null
: `+${this.phoneNumber.replace(/\D/g, '')}`,
},
}
);
@@ -141,3 +195,13 @@ export default {
},
};
</script>
<style lang="scss" scoped>
.messagingServiceHelptext {
margin-top: -10px;
margin-bottom: 15px;
.checkbox {
margin: 0px 4px;
}
}
</style>

View File

@@ -2,19 +2,21 @@
#
# Table name: channel_twilio_sms
#
# id :bigint not null, primary key
# account_sid :string not null
# auth_token :string not null
# medium :integer default("sms")
# phone_number :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# id :bigint not null, primary key
# account_sid :string not null
# auth_token :string not null
# medium :integer default("sms")
# messaging_service_sid :string
# phone_number :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
# Indexes
#
# index_channel_twilio_sms_on_account_sid_and_phone_number (account_sid,phone_number) UNIQUE
# index_channel_twilio_sms_on_phone_number (phone_number) UNIQUE
# index_channel_twilio_sms_on_account_id_and_phone_number (account_id,phone_number) UNIQUE
# index_channel_twilio_sms_on_messaging_service_sid (messaging_service_sid) UNIQUE
# index_channel_twilio_sms_on_phone_number (phone_number) UNIQUE
#
class Channel::TwilioSms < ApplicationRecord
@@ -24,8 +26,10 @@ class Channel::TwilioSms < ApplicationRecord
validates :account_sid, presence: true
validates :auth_token, presence: true
# NOTE: allowing nil for future when we suppor twilio messaging services
# https://github.com/chatwoot/chatwoot/pull/4242
# Must have _one_ of messaging_service_sid _or_ phone_number, and messaging_service_sid is preferred
validates :messaging_service_sid, uniqueness: true, presence: true, unless: :phone_number?
validates :phone_number, absence: true, if: :messaging_service_sid?
validates :phone_number, uniqueness: true, allow_nil: true
enum medium: { sms: 0, whatsapp: 1 }
@@ -37,4 +41,24 @@ class Channel::TwilioSms < ApplicationRecord
def messaging_window_enabled?
medium == 'whatsapp'
end
def send_message(to:, body:, media_url: nil)
params = send_message_from.merge(to: to, body: body)
params[:media_url] = media_url if media_url.present?
client.messages.create(**params)
end
private
def client
::Twilio::REST::Client.new(account_sid, auth_token)
end
def send_message_from
if messaging_service_sid?
{ messaging_service_sid: messaging_service_sid }
else
{ from: phone_number }
end
end
end

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -45,6 +45,7 @@ if resource.facebook?
end
## Twilio Attributes
json.messaging_service_sid resource.channel.try(:messaging_service_sid)
json.phone_number resource.channel.try(:phone_number)
json.medium resource.channel.try(:medium) if resource.twilio?