Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Telephony.config do |c|
c.voice_pause_time = '0.5s'
c.voice_rate = 'slow'

c.sender_id = 'some_sender_id'
c.country_sender_ids = {
US: 'sender1',
CA: 'sender2',
}

c.pinpoint.add_sms_config do |sms|
sms.region = 'us-west-2' # This is optional, us-west-2 is the default
Expand Down
6 changes: 4 additions & 2 deletions lib/identity-telephony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,25 @@ def self.config
@config
end

def self.send_authentication_otp(to:, otp:, expiration:, channel:, domain:)
def self.send_authentication_otp(to:, otp:, expiration:, channel:, domain:, country_code:)
OtpSender.new(
to: to,
otp: otp,
expiration: expiration,
channel: channel,
domain: domain,
country_code: country_code,
).send_authentication_otp
end

def self.send_confirmation_otp(to:, otp:, expiration:, channel:, domain:)
def self.send_confirmation_otp(to:, otp:, expiration:, channel:, domain:, country_code:)
OtpSender.new(
to: to,
otp: otp,
expiration: expiration,
channel: channel,
domain: domain,
country_code: country_code,
).send_confirmation_otp
end

Expand Down
32 changes: 16 additions & 16 deletions lib/telephony/alert_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ module Telephony
class AlertSender
SMS_MAX_LENGTH = 160

def send_account_reset_notice(to:)
def send_account_reset_notice(to:, country_code:)
message = I18n.t('telephony.account_reset_notice')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end

def send_account_reset_cancellation_notice(to:)
def send_account_reset_cancellation_notice(to:, country_code:)
message = I18n.t('telephony.account_reset_cancellation_notice')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end

def send_doc_auth_link(to:, link:)
def send_doc_auth_link(to:, link:, country_code:)
message = I18n.t('telephony.doc_auth_link', link: link)
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
context = __method__.to_s.gsub(/^send_/, '')
if link.length > SMS_MAX_LENGTH
log_warning("link longer than #{SMS_MAX_LENGTH} characters", context: context)
Expand All @@ -27,37 +27,37 @@ def send_doc_auth_link(to:, link:)
response
end

def send_personal_key_regeneration_notice(to:)
def send_personal_key_regeneration_notice(to:, country_code:)
message = I18n.t('telephony.personal_key_regeneration_notice')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end

def send_personal_key_sign_in_notice(to:)
def send_personal_key_sign_in_notice(to:, country_code:)
message = I18n.t('telephony.personal_key_sign_in_notice')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end

def send_join_keyword_response(to:)
def send_join_keyword_response(to:, country_code:)
message = I18n.t('telephony.join_keyword_response')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end

def send_stop_keyword_response(to:)
def send_stop_keyword_response(to:, country_code:)
message = I18n.t('telephony.stop_keyword_response')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end

def send_help_keyword_response(to:)
def send_help_keyword_response(to:, country_code:)
message = I18n.t('telephony.help_keyword_response')
response = adapter.send(message: message, to: to)
response = adapter.send(message: message, to: to, country_code: country_code)
log_response(response, context: __method__.to_s.gsub(/^send_/, ''))
response
end
Expand Down
10 changes: 9 additions & 1 deletion lib/telephony/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class Configuration
attr_accessor :logger
attr_accessor :voice_pause_time
attr_accessor :voice_rate
attr_accessor :sender_id

# rubocop:disable Metrics/MethodLength
def initialize
Expand All @@ -65,5 +64,14 @@ def initialize
def adapter
@adapter.to_sym
end

# @param [Hash,nil] map
def country_sender_ids=(hash)
@country_sender_ids = hash&.transform_keys(&:to_s)
end

def country_sender_ids
@country_sender_ids || {}
end
end
end
9 changes: 5 additions & 4 deletions lib/telephony/otp_sender.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
module Telephony
class OtpSender
attr_reader :recipient_phone, :otp, :expiration, :channel, :domain
attr_reader :recipient_phone, :otp, :expiration, :channel, :domain, :country_code

def initialize(to:, otp:, expiration:, channel:, domain:)
def initialize(to:, otp:, expiration:, channel:, domain:, country_code:)
@recipient_phone = to
@otp = otp
@expiration = expiration
@channel = channel.to_sym
@domain = domain
@country_code = country_code
end

def send_authentication_otp
response = adapter.send(message: authentication_message, to: recipient_phone, otp: otp)
response = adapter.send(message: authentication_message, to: recipient_phone, otp: otp, country_code: country_code)
log_response(response, context: :authentication)
response
end

def send_confirmation_otp
response = adapter.send(message: confirmation_message, to: recipient_phone, otp: otp)
response = adapter.send(message: confirmation_message, to: recipient_phone, otp: otp, country_code: country_code)
log_response(response, context: :confirmation)
response
end
Expand Down
4 changes: 2 additions & 2 deletions lib/telephony/pinpoint/sms_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SmsSender

# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/BlockLength
# @return [Response]
def send(message:, to:, otp: nil)
def send(message:, to:, country_code:, otp: nil)
return handle_config_failure if Telephony.config.pinpoint.sms_configs.empty?

response = nil
Expand All @@ -37,7 +37,7 @@ def send(message:, to:, otp: nil)
body: message,
message_type: 'TRANSACTIONAL',
origination_number: sms_config.shortcode,
sender_id: Telephony.config.sender_id,
sender_id: Telephony.config.country_sender_ids[country_code.to_s],
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion lib/telephony/pinpoint/voice_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Telephony
module Pinpoint
class VoiceSender
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/BlockLength
def send(message:, to:, otp: nil)
def send(message:, to:, country_code:, otp: nil)
return handle_config_failure if Telephony.config.pinpoint.voice_configs.empty?

language_code, voice_id = language_code_and_voice_id
Expand Down
2 changes: 1 addition & 1 deletion lib/telephony/test/sms_sender.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Telephony
module Test
class SmsSender
def send(message:, to:, otp: nil)
def send(message:, to:, country_code:, otp: nil)
error = ErrorSimulator.new.error_for_number(to)
if error.nil?
Message.messages.push(Message.new(body: message, to: to, otp: otp))
Expand Down
2 changes: 1 addition & 1 deletion lib/telephony/test/voice_sender.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Telephony
module Test
class VoiceSender
def send(message:, to:, otp: nil)
def send(message:, to:, country_code:, otp: nil)
error = ErrorSimulator.new.error_for_number(to)
if error.nil?
Call.calls.push(Call.new(body: message, to: to, otp: otp))
Expand Down
2 changes: 1 addition & 1 deletion lib/telephony/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Telephony
VERSION = '0.3.1'.freeze
VERSION = '0.4.0'.freeze
end
23 changes: 12 additions & 11 deletions spec/lib/alert_sender_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

describe 'send_account_reset_notice' do
it 'sends the correct message' do
subject.send_account_reset_notice(to: recipient)
subject.send_account_reset_notice(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -21,7 +21,7 @@

describe 'send_account_reset_cancellation_notice' do
it 'sends the correct message' do
subject.send_account_reset_cancellation_notice(to: recipient)
subject.send_account_reset_cancellation_notice(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -35,7 +35,7 @@
end

it 'sends the correct message' do
subject.send_doc_auth_link(to: recipient, link: link)
subject.send_doc_auth_link(to: recipient, link: link, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -53,7 +53,7 @@
end

it 'puts the URL in the first 160 characters, so it stays within a single SMS message' do
subject.send_doc_auth_link(to: recipient, link: link)
subject.send_doc_auth_link(to: recipient, link: link, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
first160 = last_message.body[0...160]
Expand All @@ -67,13 +67,13 @@

expect(Telephony.config.logger).to receive(:warn)

subject.send_doc_auth_link(to: recipient, link: long_link)
subject.send_doc_auth_link(to: recipient, link: long_link, country_code: 'US')
end
end

describe 'send_personal_key_regeneration_notice' do
it 'sends the correct message' do
subject.send_personal_key_regeneration_notice(to: recipient)
subject.send_personal_key_regeneration_notice(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -83,7 +83,7 @@

describe 'send_personal_key_sign_in_notice' do
it 'sends the correct message' do
subject.send_personal_key_sign_in_notice(to: recipient)
subject.send_personal_key_sign_in_notice(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -93,7 +93,7 @@

describe 'send_join_keyword_response' do
it 'sends the correct message' do
subject.send_join_keyword_response(to: recipient)
subject.send_join_keyword_response(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -103,7 +103,7 @@

describe 'send_stop_keyword_response' do
it 'sends the correct message' do
subject.send_stop_keyword_response(to: recipient)
subject.send_stop_keyword_response(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -113,7 +113,7 @@

describe 'send_help_keyword_response' do
it 'sends the correct message' do
subject.send_help_keyword_response(to: recipient)
subject.send_help_keyword_response(to: recipient, country_code: 'US')

last_message = Telephony::Test::Message.messages.last
expect(last_message.to).to eq(recipient)
Expand All @@ -129,10 +129,11 @@
expect(adapter).to receive(:send).with(
message: I18n.t('telephony.join_keyword_response'),
to: recipient,
country_code: 'US',
)
expect(Telephony::Pinpoint::SmsSender).to receive(:new).and_return(adapter)

subject.send_join_keyword_response(to: recipient)
subject.send_join_keyword_response(to: recipient, country_code: 'US')
end
end
end
Loading