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:
parent
fdf449dc87
commit
49d08a6773
22 changed files with 379 additions and 105 deletions
2
Gemfile
2
Gemfile
|
@ -78,7 +78,7 @@ gem 'wisper', '2.0.0'
|
|||
# TODO: bump up gem to 2.0
|
||||
gem 'facebook-messenger'
|
||||
gem 'line-bot-api'
|
||||
gem 'twilio-ruby', '~> 5.32.0'
|
||||
gem 'twilio-ruby', '~> 5.66'
|
||||
# twitty will handle subscription of twitter account events
|
||||
# gem 'twitty', git: 'https://github.com/chatwoot/twitty'
|
||||
gem 'twitty'
|
||||
|
|
|
@ -582,8 +582,8 @@ GEM
|
|||
activesupport
|
||||
i18n
|
||||
trailblazer-option (0.1.2)
|
||||
twilio-ruby (5.32.0)
|
||||
faraday (~> 1.0.0)
|
||||
twilio-ruby (5.66.0)
|
||||
faraday (>= 0.9, < 2.0)
|
||||
jwt (>= 1.5, <= 2.5)
|
||||
nokogiri (>= 1.6, < 2.0)
|
||||
twitty (0.1.4)
|
||||
|
@ -730,7 +730,7 @@ DEPENDENCIES
|
|||
squasher
|
||||
telephone_number
|
||||
time_diff
|
||||
twilio-ruby (~> 5.32.0)
|
||||
twilio-ruby (~> 5.66)
|
||||
twitty
|
||||
tzinfo-data
|
||||
uglifier
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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?
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
class AddMessagingServiceSidToChannelTwilioSms < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
change_column_null :channel_twilio_sms, :phone_number, true
|
||||
add_column :channel_twilio_sms, :messaging_service_sid, :string
|
||||
add_index :channel_twilio_sms, [:messaging_service_sid], unique: true
|
||||
end
|
||||
end
|
|
@ -290,14 +290,16 @@ ActiveRecord::Schema.define(version: 2022_07_06_085458) do
|
|||
end
|
||||
|
||||
create_table "channel_twilio_sms", force: :cascade do |t|
|
||||
t.string "phone_number", null: false
|
||||
t.string "phone_number"
|
||||
t.string "auth_token", null: false
|
||||
t.string "account_sid", null: false
|
||||
t.integer "account_id", null: false
|
||||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.integer "medium", default: 0
|
||||
t.index ["account_sid", "phone_number"], name: "index_channel_twilio_sms_on_account_sid_and_phone_number", unique: true
|
||||
t.string "messaging_service_sid"
|
||||
t.index ["account_id", "phone_number"], name: "index_channel_twilio_sms_on_account_id_and_phone_number", unique: true
|
||||
t.index ["messaging_service_sid"], name: "index_channel_twilio_sms_on_messaging_service_sid", unique: true
|
||||
t.index ["phone_number"], name: "index_channel_twilio_sms_on_phone_number", unique: true
|
||||
end
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ RSpec.describe '/api/v1/accounts/{account.id}/channels/twilio_channel', type: :r
|
|||
twilio_channel: {
|
||||
account_sid: 'sid',
|
||||
auth_token: 'token',
|
||||
phone_number: '+1234567890',
|
||||
messaging_service_sid: 'MGec8130512b5dd462cfe03095ec1342ed',
|
||||
name: 'SMS Channel',
|
||||
medium: 'sms'
|
||||
}
|
||||
|
@ -48,7 +48,36 @@ RSpec.describe '/api/v1/accounts/{account.id}/channels/twilio_channel', type: :r
|
|||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['name']).to eq('SMS Channel')
|
||||
expect(json_response['phone_number']).to eq('+1234567890')
|
||||
expect(json_response['messaging_service_sid']).to eq('MGec8130512b5dd462cfe03095ec1342ed')
|
||||
end
|
||||
|
||||
context 'with a phone number' do # rubocop:disable RSpec/NestedGroups
|
||||
let(:params) do
|
||||
{
|
||||
twilio_channel: {
|
||||
account_sid: 'sid',
|
||||
auth_token: 'token',
|
||||
phone_number: '+1234567890',
|
||||
name: 'SMS Channel',
|
||||
medium: 'sms'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
it 'creates inbox and returns inbox object' do
|
||||
allow(twilio_client).to receive(:messages).and_return(message_double)
|
||||
allow(message_double).to receive(:list).and_return([])
|
||||
|
||||
post api_v1_account_channels_twilio_channel_path(account),
|
||||
params: params,
|
||||
headers: admin.create_new_auth_token
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = JSON.parse(response.body)
|
||||
|
||||
expect(json_response['name']).to eq('SMS Channel')
|
||||
expect(json_response['phone_number']).to eq('+1234567890')
|
||||
end
|
||||
end
|
||||
|
||||
it 'return error if Twilio tokens are incorrect' do
|
||||
|
|
|
@ -2,11 +2,16 @@ FactoryBot.define do
|
|||
factory :channel_twilio_sms, class: 'Channel::TwilioSms' do
|
||||
auth_token { SecureRandom.uuid }
|
||||
account_sid { SecureRandom.uuid }
|
||||
sequence(:phone_number) { |n| "+123456789#{n}1" }
|
||||
messaging_service_sid { "MG#{Faker::Number.hexadecimal(digits: 32)}" }
|
||||
medium { :sms }
|
||||
account
|
||||
after(:build) do |channel|
|
||||
channel.inbox ||= create(:inbox, account: channel.account)
|
||||
end
|
||||
|
||||
trait :with_phone_number do
|
||||
sequence(:phone_number) { |n| "+123456789#{n}1" }
|
||||
messaging_service_sid { nil }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,23 +3,74 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Channel::TwilioSms do
|
||||
context 'with medium whatsapp' do
|
||||
let!(:whatsapp_channel) { create(:channel_twilio_sms, medium: :whatsapp) }
|
||||
describe '#has_24_hour_messaging_window?' do
|
||||
context 'with medium whatsapp' do
|
||||
let!(:whatsapp_channel) { create(:channel_twilio_sms, medium: :whatsapp) }
|
||||
|
||||
it 'returns true' do
|
||||
expect(whatsapp_channel.messaging_window_enabled?).to eq true
|
||||
expect(whatsapp_channel.name).to eq 'Whatsapp'
|
||||
expect(whatsapp_channel.medium).to eq 'whatsapp'
|
||||
it 'returns true' do
|
||||
expect(whatsapp_channel.messaging_window_enabled?).to eq true
|
||||
expect(whatsapp_channel.name).to eq 'Whatsapp'
|
||||
expect(whatsapp_channel.medium).to eq 'whatsapp'
|
||||
end
|
||||
end
|
||||
|
||||
context 'with medium sms' do
|
||||
let!(:sms_channel) { create(:channel_twilio_sms, medium: :sms) }
|
||||
|
||||
it 'returns false' do
|
||||
expect(sms_channel.messaging_window_enabled?).to eq false
|
||||
expect(sms_channel.name).to eq 'Twilio SMS'
|
||||
expect(sms_channel.medium).to eq 'sms'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with medium sms' do
|
||||
let!(:sms_channel) { create(:channel_twilio_sms, medium: :sms) }
|
||||
describe '#send_message' do
|
||||
let(:channel) { create(:channel_twilio_sms) }
|
||||
|
||||
it 'returns false' do
|
||||
expect(sms_channel.messaging_window_enabled?).to eq false
|
||||
expect(sms_channel.name).to eq 'Twilio SMS'
|
||||
expect(sms_channel.medium).to eq 'sms'
|
||||
let(:twilio_client) { instance_double(Twilio::REST::Client) }
|
||||
let(:twilio_messages) { double }
|
||||
|
||||
before do
|
||||
allow(::Twilio::REST::Client).to receive(:new).and_return(twilio_client)
|
||||
allow(twilio_client).to receive(:messages).and_return(twilio_messages)
|
||||
end
|
||||
|
||||
it 'sends via twilio client' do
|
||||
expect(twilio_messages).to receive(:create).with(
|
||||
messaging_service_sid: channel.messaging_service_sid,
|
||||
to: '+15555550111',
|
||||
body: 'hello world'
|
||||
).once
|
||||
|
||||
channel.send_message(to: '+15555550111', body: 'hello world')
|
||||
end
|
||||
|
||||
context 'with a "from" phone number' do
|
||||
let(:channel) { create(:channel_twilio_sms, :with_phone_number) }
|
||||
|
||||
it 'sends via twilio client' do
|
||||
expect(twilio_messages).to receive(:create).with(
|
||||
from: channel.phone_number,
|
||||
to: '+15555550111',
|
||||
body: 'hello world'
|
||||
).once
|
||||
|
||||
channel.send_message(to: '+15555550111', body: 'hello world')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with media urls' do
|
||||
it 'supplies a media url' do
|
||||
expect(twilio_messages).to receive(:create).with(
|
||||
messaging_service_sid: channel.messaging_service_sid,
|
||||
to: '+15555550111',
|
||||
body: 'hello world',
|
||||
media_url: ['https://example.com/1.jpg']
|
||||
).once
|
||||
|
||||
channel.send_message(to: '+15555550111', body: 'hello world', media_url: ['https://example.com/1.jpg'])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ require 'rails_helper'
|
|||
describe Twilio::IncomingMessageService do
|
||||
let!(:account) { create(:account) }
|
||||
let!(:twilio_sms) do
|
||||
create(:channel_twilio_sms, account: account, phone_number: '+1234567890', account_sid: 'ACxxx',
|
||||
create(:channel_twilio_sms, account: account, account_sid: 'ACxxx',
|
||||
inbox: create(:inbox, account: account, greeting_enabled: false))
|
||||
end
|
||||
let!(:contact) { create(:contact, account: account, phone_number: '+12345') }
|
||||
|
@ -16,7 +16,7 @@ describe Twilio::IncomingMessageService do
|
|||
SmsSid: 'SMxx',
|
||||
From: '+12345',
|
||||
AccountSid: 'ACxxx',
|
||||
To: '+1234567890',
|
||||
MessagingServiceSid: twilio_sms.messaging_service_sid,
|
||||
Body: 'testing3'
|
||||
}
|
||||
|
||||
|
@ -29,12 +29,45 @@ describe Twilio::IncomingMessageService do
|
|||
SmsSid: 'SMxx',
|
||||
From: '+123456',
|
||||
AccountSid: 'ACxxx',
|
||||
To: '+1234567890',
|
||||
MessagingServiceSid: twilio_sms.messaging_service_sid,
|
||||
Body: 'new conversation'
|
||||
}
|
||||
|
||||
described_class.new(params: params).perform
|
||||
expect(Conversation.count).to eq(2)
|
||||
end
|
||||
|
||||
context 'with a phone number' do
|
||||
let!(:twilio_sms) do
|
||||
create(:channel_twilio_sms, :with_phone_number, account: account, account_sid: 'ACxxx',
|
||||
inbox: create(:inbox, account: account, greeting_enabled: false))
|
||||
end
|
||||
|
||||
it 'creates a new message in existing conversation' do
|
||||
params = {
|
||||
SmsSid: 'SMxx',
|
||||
From: '+12345',
|
||||
AccountSid: 'ACxxx',
|
||||
To: twilio_sms.phone_number,
|
||||
Body: 'testing3'
|
||||
}
|
||||
|
||||
described_class.new(params: params).perform
|
||||
expect(conversation.reload.messages.last.content).to eq('testing3')
|
||||
end
|
||||
|
||||
it 'creates a new conversation' do
|
||||
params = {
|
||||
SmsSid: 'SMxx',
|
||||
From: '+123456',
|
||||
AccountSid: 'ACxxx',
|
||||
To: twilio_sms.phone_number,
|
||||
Body: 'new conversation'
|
||||
}
|
||||
|
||||
described_class.new(params: params).perform
|
||||
expect(Conversation.count).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,12 +38,21 @@ describe Twilio::OneoffSmsCampaignService do
|
|||
contact_with_label1.update_labels([label1.title])
|
||||
contact_with_label2.update_labels([label2.title])
|
||||
contact_with_both_labels.update_labels([label1.title, label2.title])
|
||||
expect(twilio_messages).to receive(:create).with(body: campaign.message,
|
||||
from: twilio_sms.phone_number, to: contact_with_label1.phone_number).once
|
||||
expect(twilio_messages).to receive(:create).with(body: campaign.message,
|
||||
from: twilio_sms.phone_number, to: contact_with_label2.phone_number).once
|
||||
expect(twilio_messages).to receive(:create).with(body: campaign.message,
|
||||
from: twilio_sms.phone_number, to: contact_with_both_labels.phone_number).once
|
||||
expect(twilio_messages).to receive(:create).with(
|
||||
body: campaign.message,
|
||||
messaging_service_sid: twilio_sms.messaging_service_sid,
|
||||
to: contact_with_label1.phone_number
|
||||
).once
|
||||
expect(twilio_messages).to receive(:create).with(
|
||||
body: campaign.message,
|
||||
messaging_service_sid: twilio_sms.messaging_service_sid,
|
||||
to: contact_with_label2.phone_number
|
||||
).once
|
||||
expect(twilio_messages).to receive(:create).with(
|
||||
body: campaign.message,
|
||||
messaging_service_sid: twilio_sms.messaging_service_sid,
|
||||
to: contact_with_both_labels.phone_number
|
||||
).once
|
||||
|
||||
sms_campaign_service.perform
|
||||
expect(campaign.reload.completed?).to eq true
|
||||
|
|
|
@ -3,46 +3,80 @@ require 'rails_helper'
|
|||
describe Twilio::WebhookSetupService do
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
let(:channel_twilio_sms) { create(:channel_twilio_sms) }
|
||||
let(:twilio_client) { instance_double(::Twilio::REST::Client) }
|
||||
let(:phone_double) { instance_double('phone_double') }
|
||||
let(:phone_record_double) { instance_double('phone_record_double') }
|
||||
|
||||
before do
|
||||
allow(::Twilio::REST::Client).to receive(:new).and_return(twilio_client)
|
||||
allow(phone_double).to receive(:update)
|
||||
allow(phone_record_double).to receive(:sid).and_return('1234')
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
it 'logs error if phone_number is not found' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_return([])
|
||||
context 'with a messaging service sid' do
|
||||
let(:channel_twilio_sms) { create(:channel_twilio_sms) }
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
let(:messaging) { instance_double(Twilio::REST::Messaging) }
|
||||
let(:services) { instance_double(Twilio::REST::Messaging::V1::ServiceContext) }
|
||||
|
||||
expect(phone_double).not_to have_received(:update)
|
||||
before do
|
||||
allow(twilio_client).to receive(:messaging).and_return(messaging)
|
||||
allow(messaging).to receive(:services).with(channel_twilio_sms.messaging_service_sid).and_return(services)
|
||||
allow(services).to receive(:update)
|
||||
end
|
||||
|
||||
it 'updates the messaging service' do
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(services).to have_received(:update)
|
||||
end
|
||||
|
||||
it 'does not raise if TwilioError is thrown' do
|
||||
expect(services).to receive(:update).and_raise(Twilio::REST::TwilioError)
|
||||
|
||||
expect do
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
it 'update webhook_url if phone_number is found' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_return([phone_record_double])
|
||||
context 'with a phone number' do
|
||||
let(:channel_twilio_sms) { create(:channel_twilio_sms, :with_phone_number) }
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
let(:phone_double) { instance_double('phone_double') }
|
||||
let(:phone_record_double) { instance_double('phone_record_double') }
|
||||
|
||||
expect(phone_double).to have_received(:update).with(
|
||||
sms_method: 'POST',
|
||||
sms_url: twilio_callback_index_url
|
||||
)
|
||||
end
|
||||
before do
|
||||
allow(phone_double).to receive(:update)
|
||||
allow(phone_record_double).to receive(:sid).and_return('1234')
|
||||
end
|
||||
|
||||
it 'doesnot call update if TwilioError is thrown' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_raise(Twilio::REST::TwilioError)
|
||||
it 'logs error if phone_number is not found' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_return([])
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(phone_double).not_to have_received(:update)
|
||||
expect(phone_double).not_to have_received(:update)
|
||||
end
|
||||
|
||||
it 'update webhook_url if phone_number is found' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_return([phone_record_double])
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(phone_double).to have_received(:update).with(
|
||||
sms_method: 'POST',
|
||||
sms_url: twilio_callback_index_url
|
||||
)
|
||||
end
|
||||
|
||||
it 'does not call update if TwilioError is thrown' do
|
||||
allow(twilio_client).to receive(:incoming_phone_numbers).and_return(phone_double)
|
||||
allow(phone_double).to receive(:list).and_raise(Twilio::REST::TwilioError)
|
||||
|
||||
described_class.new(inbox: channel_twilio_sms.inbox).perform
|
||||
|
||||
expect(phone_double).not_to have_received(:update)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue