Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/initializers/telephony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
c.pinpoint.add_sms_config do |sms|
sms.application_id = sms_json_config['application_id']
sms.region = sms_json_config['region']
sms.country_code_shortcodes = sms_json_config['country_code_shortcodes'] || {}
sms.shortcode = sms_json_config['shortcode']
sms.country_code_longcode_pool = sms_json_config['country_code_longcode_pool'] || {}
sms.credential_role_arn = sms_json_config['credential_role_arn']
Expand Down
8 changes: 8 additions & 0 deletions lib/telephony/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def add_sms_config
raise 'missing sms configuration block' unless block_given?
sms = PinpointSmsConfiguration.new(region: 'us-west-2')
yield sms

shortcode_country_codes = sms.country_code_shortcodes&.keys || []
longcode_country_codes = sms.country_code_longcode_pool&.keys || []
if shortcode_country_codes.intersect?(longcode_country_codes)
raise 'cannot configure a country code for both longcodes and a shortcode'
end

sms_configs << sms
sms
end
Expand Down Expand Up @@ -42,6 +49,7 @@ def add_voice_config
PinpointSmsConfiguration = Struct.new(
:application_id,
:shortcode,
:country_code_shortcodes,
:country_code_longcode_pool,
*PINPOINT_CONFIGURATION_NAMES,
keyword_init: true,
Expand Down
2 changes: 2 additions & 0 deletions lib/telephony/pinpoint/sms_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def phone_info(phone_number)
def origination_number(country_code, sms_config)
if sms_config.country_code_longcode_pool&.dig(country_code).present?
sms_config.country_code_longcode_pool[country_code].sample
elsif sms_config.country_code_shortcodes&.dig(country_code).present?
sms_config.country_code_shortcodes[country_code]
else
sms_config.shortcode
end
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/telephony/pinpoint/sms_sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,40 @@ def ==(other)
end
end

context 'in a non-sender_id country that has a configured shortcode' do
let(:country_code) { 'MX' }

it 'sends a message with a longcode and no sender_id' do
mock_build_client
response = subject.send(
message: 'This is a test!',
to: '+525555555555',
country_code: country_code,
)

expected_result = {
application_id: Telephony.config.pinpoint.sms_configs.first.application_id,
message_request: {
addresses: {
'+525555555555' => { channel_type: 'SMS' },
},
message_configuration: {
sms_message: {
body: 'This is a test!',
message_type: 'TRANSACTIONAL',
origination_number: '987654',
},
},
},
}

expect(Pinpoint::MockClient.last_request).to eq(expected_result)
expect(response.success?).to eq(true)
expect(response.error).to eq(nil)
expect(response.extra[:request_id]).to eq('fake-message-request-id')
end
end

context 'with multiple sms configs' do
before do
Telephony.config.pinpoint.add_sms_config do |sms|
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/telephony/pinpoint_configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rails_helper'

RSpec.describe Telephony::PinpointConfiguration do
context '#add_sms_config' do
it 'raises if the same country code is used in both longcode and shortcode configuration' do
config = Telephony::PinpointConfiguration.new
expect do
config.add_sms_config do |sms|
sms.country_code_shortcodes = { 'PR' => '123456' }
sms.country_code_longcode_pool = { 'PR' => ['+1 (939) 456-7890'] }
end.to raise_error(
'cannot configure a country code for both longcodes and a shortcode',
)
end
end
end
end
1 change: 1 addition & 0 deletions spec/support/shared_contexts/telephony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def telephony_use_default_config!
sms.application_id = 'fake-pinpoint-application-id-sms'
sms.shortcode = '123456'
sms.country_code_longcode_pool = { 'PR' => ['+19393334444'] }
sms.country_code_shortcodes = { 'MX' => '987654' }
end

c.pinpoint.add_voice_config do |voice|
Expand Down