From 6e7de9f06990dd898ab88d162cf5c32f827d86b7 Mon Sep 17 00:00:00 2001 From: Stephen Astels Date: Wed, 8 May 2024 08:36:01 -0400 Subject: [PATCH] rough in using default pinpoint pool --- .env.example | 1 + app/clients/sms/aws_pinpoint.py | 9 +++++++-- app/config.py | 1 + app/delivery/send_to_providers.py | 24 +++++++++++++++--------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index cb36eefda5..8e7042fbda 100644 --- a/.env.example +++ b/.env.example @@ -22,3 +22,4 @@ CONTACT_FORM_EMAIL_ADDRESS = "" AWS_PINPOINT_SC_POOL_ID= AWS_PINPOINT_SC_TEMPLATE_IDS= +AWS_PINPOINT_LC_POOL_ID= \ No newline at end of file diff --git a/app/clients/sms/aws_pinpoint.py b/app/clients/sms/aws_pinpoint.py index 37140323c0..b1cf596774 100644 --- a/app/clients/sms/aws_pinpoint.py +++ b/app/clients/sms/aws_pinpoint.py @@ -5,6 +5,7 @@ from app.clients.sms import SmsClient +from app.config import Config class AwsPinpointClient(SmsClient): """ @@ -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) diff --git a/app/config.py b/app/config.py index b3c34feb7f..2113a9c733 100644 --- a/app/config.py +++ b/app/config.py @@ -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") diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index c291bbd16a..b77a5d6480 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -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 @@ -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))