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

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

View File

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

View File

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

View File

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

View File

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

View File

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