diff --git a/config/initializers/telephony.rb b/config/initializers/telephony.rb index eb47eb66779..a6a81259c06 100644 --- a/config/initializers/telephony.rb +++ b/config/initializers/telephony.rb @@ -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'] diff --git a/lib/telephony/configuration.rb b/lib/telephony/configuration.rb index 020b38f7654..56feec80013 100644 --- a/lib/telephony/configuration.rb +++ b/lib/telephony/configuration.rb @@ -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 @@ -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, diff --git a/lib/telephony/pinpoint/sms_sender.rb b/lib/telephony/pinpoint/sms_sender.rb index ce63b395533..85a3b3420fa 100644 --- a/lib/telephony/pinpoint/sms_sender.rb +++ b/lib/telephony/pinpoint/sms_sender.rb @@ -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 diff --git a/spec/lib/telephony/pinpoint/sms_sender_spec.rb b/spec/lib/telephony/pinpoint/sms_sender_spec.rb index 75966a5f728..b55f7c23820 100644 --- a/spec/lib/telephony/pinpoint/sms_sender_spec.rb +++ b/spec/lib/telephony/pinpoint/sms_sender_spec.rb @@ -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| diff --git a/spec/lib/telephony/pinpoint_configuration_spec.rb b/spec/lib/telephony/pinpoint_configuration_spec.rb new file mode 100644 index 00000000000..58e9b221009 --- /dev/null +++ b/spec/lib/telephony/pinpoint_configuration_spec.rb @@ -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 diff --git a/spec/support/shared_contexts/telephony.rb b/spec/support/shared_contexts/telephony.rb index a5bb5afe751..67d04a26755 100644 --- a/spec/support/shared_contexts/telephony.rb +++ b/spec/support/shared_contexts/telephony.rb @@ -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|