Skip to content

Commit

Permalink
rough in using default pinpoint pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels committed May 8, 2024
1 parent 7fb51e1 commit 6e7de9f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ CONTACT_FORM_EMAIL_ADDRESS = ""

AWS_PINPOINT_SC_POOL_ID=
AWS_PINPOINT_SC_TEMPLATE_IDS=
AWS_PINPOINT_LC_POOL_ID=
9 changes: 7 additions & 2 deletions app/clients/sms/aws_pinpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from app.clients.sms import SmsClient

from app.config import Config

class AwsPinpointClient(SmsClient):
"""
Expand All @@ -22,11 +23,15 @@ def init_app(self, current_app, statsd_client, *args, **kwargs):
def get_name(self):
return self.name

def send_sms(self, to, content, reference, multi=True, sender=None):
pool_id = self.current_app.config["AWS_PINPOINT_SC_POOL_ID"]
def send_sms(self, to, content, reference, multi=True, sender=None, template_id=None):
messageType = "TRANSACTIONAL"
matched = False

if template_id is not None and str(template_id) in Config.AWS_PINPOINT_SC_TEMPLATE_IDS:
pool_id = Config.AWS_PINPOINT_SC_POOL_ID
else:
pool_id = Config.AWS_PINPOINT_DEFAULT_POOL_ID

for match in phonenumbers.PhoneNumberMatcher(to, "US"):
matched = True
to = phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
Expand Down
1 change: 1 addition & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class Config(object):
AWS_SES_SECRET_KEY = os.getenv("AWS_SES_SECRET_KEY")
AWS_PINPOINT_REGION = os.getenv("AWS_PINPOINT_REGION", "us-west-2")
AWS_PINPOINT_SC_POOL_ID = os.getenv("AWS_PINPOINT_SC_POOL_ID", None)
AWS_PINPOINT_DEFAULT_POOL_ID = os.getenv("AWS_PINPOINT_DEFAULT_POOL_ID", None)
AWS_PINPOINT_CONFIGURATION_SET_NAME = os.getenv("AWS_PINPOINT_CONFIGURATION_SET_NAME", "pinpoint-configuration")
AWS_PINPOINT_SC_TEMPLATE_IDS = env.list("AWS_PINPOINT_SC_TEMPLATE_IDS", [])
AWS_US_TOLL_FREE_NUMBER = os.getenv("AWS_US_TOLL_FREE_NUMBER")
Expand Down
24 changes: 15 additions & 9 deletions app/delivery/send_to_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def send_sms_to_provider(notification):
content=str(template),
reference=str(notification.id),
sender=notification.reply_to_text,
template_id=notification.template_id,
)
except Exception as e:
notification.billable_units = template.fragment_count
Expand Down Expand Up @@ -337,15 +338,20 @@ def update_notification_to_sending(notification, provider):


def provider_to_use(notification_type, notification_id, international=False, sender=None, template_id=None):
# Temporary redirect setup for template IDs that are meant for the short code usage.
if notification_type == SMS_TYPE and template_id is not None and str(template_id) in Config.AWS_PINPOINT_SC_TEMPLATE_IDS:
return clients.get_client_by_name_and_type("pinpoint", SMS_TYPE)

active_providers_in_order = [
p
for p in get_provider_details_by_notification_type(notification_type, international)
if p.active and p.identifier != PINPOINT_PROVIDER
]

# TODO: remove the first option once we have pinpoint fully integrated
if Config.AWS_PINPOINT_SC_POOL_ID is None or Config.AWS_PINPOINT_DEFAULT_POOL_ID is None:
active_providers_in_order = [
p
for p in get_provider_details_by_notification_type(notification_type, international)
if p.active and p.identifier != PINPOINT_PROVIDER
]
else:
active_providers_in_order = [
p
for p in get_provider_details_by_notification_type(notification_type, international)
if p.active
]

if not active_providers_in_order:
current_app.logger.error("{} {} failed as no active providers".format(notification_type, notification_id))
Expand Down

0 comments on commit 6e7de9f

Please sign in to comment.