Skip to content

Commit

Permalink
Add script to set phone number keywords (#1358)
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels authored Jun 13, 2024
1 parent 9ae7c1a commit fa93845
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ override.tf.json

# python environment variables
.env*

# Allow example environment variables file
!.env.example
2 changes: 2 additions & 0 deletions scripts/set_sms_keywords/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
2 changes: 2 additions & 0 deletions scripts/set_sms_keywords/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
boto3==1.34.25
python-dotenv==1.0.1
82 changes: 82 additions & 0 deletions scripts/set_sms_keywords/set_sms_keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import boto3
import argparse
from typing import Any, Dict
from dotenv import load_dotenv

"""
Set the keywords for phone numbers and pools.
"""


keywords_to_set = [
{
"Keyword": "AIDE",
"KeywordMessage": "GC Notify: Visitez https://notification.canada.ca/contact Frais de msg/donnée std applicables. La fréquence des messages peut varier. Textez ARRET pour annuler.",
"KeywordAction": "AUTOMATIC_RESPONSE",
},
{
"Keyword": "ARRET",
"KeywordMessage": "GC Notify: Vous êtes désinscrit des notifications du gouvernement du Canada. Frais de msg/donnée std applicables.",
"KeywordAction": "OPT_OUT",
},
{
"Keyword": "HELP",
"KeywordMessage": "GC Notify: More support found at https://notification.canada.ca/contact Std msg & data rates apply, msg frequency varies. Reply STOP to cancel.",
"KeywordAction": "AUTOMATIC_RESPONSE",
},
{
"Keyword": "INFO",
"KeywordMessage": "GC Notify: More info at https://notification.canada.ca Data rates apply. Plus d’informations à https://notification.canada.ca Frais de msg/donnée std applicables.",
"KeywordAction": "AUTOMATIC_RESPONSE",
},
{
"Keyword": "STOP",
"KeywordMessage": "GC Notify: You have been unsubscribed from the Government of Canada Notify. Std msg & data rates apply.",
"KeywordAction": "OPT_OUT",
},
]


def set_keywords(client: Any, origination_identity: str, keywords_to_set: Dict[str, str]) -> None:
"""
Set the keywords for the origination identity.
client: Any
The AWS Pinpoint client.
origination_identity: str
The origination identity (phone number or pool) to set the keywords for.
keywords_to_set: Dict[str, str]
The keywords to set.
"""

current_keywords = client.describe_keywords(OriginationIdentity=origination_identity)['Keywords']
for keyword in current_keywords:
if keyword['Keyword'] not in [k['Keyword'] for k in keywords_to_set]:
client.delete_keyword(OriginationIdentity=origination_identity, Keyword=keyword['Keyword'])

for keyword in keywords_to_set:
client.put_keyword(OriginationIdentity=origination_identity, Keyword=keyword["Keyword"], KeywordMessage=keyword["KeywordMessage"], KeywordAction=keyword["KeywordAction"])


def main():
load_dotenv()
client = boto3.client('pinpoint-sms-voice-v2', region_name='ca-central-1')
parser=argparse.ArgumentParser()
parser.add_argument("--pools", help="Change keywords of pools", action="store_true", default=False)
parser.add_argument("--phone_numbers", help="Change keywords of phone numbers not in a pool", action="store_true", default=False)
args = parser.parse_args()

if args.pools:
for pool in client.describe_pools()['Pools']:
print("Setting keywords for pool: " + pool['PoolId'])
set_keywords(client, pool['PoolId'], keywords_to_set)

if args.phone_numbers:
for phone_number in client.describe_phone_numbers()['PhoneNumbers']:
if phone_number.get('PoolId') is None:
print("Setting keywords for phone number: " + phone_number['PhoneNumber'])
set_keywords(client, phone_number['PhoneNumberId'] , keywords_to_set)


if __name__ == "__main__":
main()

0 comments on commit fa93845

Please sign in to comment.