Skip to content

Commit c1501e0

Browse files
authored
Use Pinpoint and default sender id for international sending (#2221)
1 parent c237e66 commit c1501e0

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

app/clients/sms/aws_pinpoint.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,25 @@ def send_sms(self, to, content, reference, multi=True, sender=None, template_id=
4141
opted_out = False
4242
to = phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
4343
destinationNumber = to
44-
4544
try:
4645
start_time = monotonic()
47-
response = self._client.send_text_message(
48-
DestinationPhoneNumber=destinationNumber,
49-
OriginationIdentity=pool_id,
50-
MessageBody=content,
51-
MessageType=messageType,
52-
ConfigurationSetName=self.current_app.config["AWS_PINPOINT_CONFIGURATION_SET_NAME"],
53-
)
46+
# For international numbers we send with an AWS number for the corresponding country, using our default sender id.
47+
# Note that Canada does not currently support sender ids.
48+
if phonenumbers.region_code_for_number(match.number) != "CA":
49+
response = self._client.send_text_message(
50+
DestinationPhoneNumber=destinationNumber,
51+
MessageBody=content,
52+
MessageType=messageType,
53+
ConfigurationSetName=self.current_app.config["AWS_PINPOINT_CONFIGURATION_SET_NAME"],
54+
)
55+
else:
56+
response = self._client.send_text_message(
57+
DestinationPhoneNumber=destinationNumber,
58+
OriginationIdentity=pool_id,
59+
MessageBody=content,
60+
MessageType=messageType,
61+
ConfigurationSetName=self.current_app.config["AWS_PINPOINT_CONFIGURATION_SET_NAME"],
62+
)
5463
except self._client.exceptions.ConflictException as e:
5564
if e.response.get("Reason") == "DESTINATION_PHONE_NUMBER_OPTED_OUT":
5665
opted_out = True

app/delivery/send_to_providers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,12 @@ def provider_to_use(
395395
elif phonenumbers.region_code_for_number(match.number) != "CA":
396396
recipient_outside_canada = True
397397
using_sc_pool_template = template_id is not None and str(template_id) in current_app.config["AWS_PINPOINT_SC_TEMPLATE_IDS"]
398-
398+
zone_1_outside_canada = recipient_outside_canada and not international
399399
do_not_use_pinpoint = (
400400
has_dedicated_number
401401
or sending_to_us_number
402402
or cannot_determine_recipient_country
403-
or international
404-
or recipient_outside_canada
403+
or zone_1_outside_canada
405404
or not current_app.config["AWS_PINPOINT_SC_POOL_ID"]
406405
or ((not current_app.config["AWS_PINPOINT_DEFAULT_POOL_ID"]) and not using_sc_pool_template)
407406
)

tests/app/clients/test_aws_pinpoint.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,30 @@ def test_handles_opted_out_numbers(notify_api, mocker, sample_template):
112112
content = "foo"
113113
reference = "ref"
114114
assert aws_pinpoint_client.send_sms(to, content, reference=reference, template_id=sample_template.id) == "opted_out"
115+
116+
117+
@pytest.mark.serial
118+
def test_send_sms_sends_international_without_pool_id(notify_api, mocker, sample_template):
119+
boto_mock = mocker.patch.object(aws_pinpoint_client, "_client", create=True)
120+
mocker.patch.object(aws_pinpoint_client, "statsd_client", create=True)
121+
to = "+447512501324"
122+
content = "foo"
123+
reference = "ref"
124+
125+
with set_config_values(
126+
notify_api,
127+
{
128+
"AWS_PINPOINT_SC_POOL_ID": "sc_pool_id",
129+
"AWS_PINPOINT_DEFAULT_POOL_ID": "default_pool_id",
130+
"AWS_PINPOINT_CONFIGURATION_SET_NAME": "config_set_name",
131+
"AWS_PINPOINT_SC_TEMPLATE_IDS": [],
132+
},
133+
):
134+
aws_pinpoint_client.send_sms(to, content, reference=reference, template_id=sample_template.id)
135+
136+
boto_mock.send_text_message.assert_called_once_with(
137+
DestinationPhoneNumber="+447512501324",
138+
MessageBody=content,
139+
MessageType="TRANSACTIONAL",
140+
ConfigurationSetName="config_set_name",
141+
)

tests/app/delivery/test_send_to_providers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ def test_should_use_sns_for_sms_if_sending_to_the_US(self, restore_provider_deta
110110
provider = send_to_providers.provider_to_use("sms", "1234", "+17065551234")
111111
assert provider.name == "sns"
112112

113-
def test_should_use_sns_for_sms_if_sending_outside_zone_1(self, restore_provider_details, notify_api):
113+
@pytest.mark.serial
114+
def test_should_use_pinpoint_for_sms_if_sending_outside_zone_1(self, restore_provider_details, notify_api):
114115
with set_config_values(
115116
notify_api,
116117
{
117118
"AWS_PINPOINT_SC_POOL_ID": "sc_pool_id",
118119
"AWS_PINPOINT_DEFAULT_POOL_ID": "default_pool_id",
119120
},
120121
):
121-
provider = send_to_providers.provider_to_use("sms", "1234", "+17065551234", international=True)
122-
assert provider.name == "sns"
122+
provider = send_to_providers.provider_to_use("sms", "1234", "+447512501324", international=True)
123+
assert provider.name == "pinpoint"
123124

124125
def test_should_use_sns_for_sms_if_sending_to_non_CA_zone_1(self, restore_provider_details, notify_api):
125126
with set_config_values(

0 commit comments

Comments
 (0)