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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user