-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsms_notifications.py
More file actions
90 lines (75 loc) · 2.77 KB
/
sms_notifications.py
File metadata and controls
90 lines (75 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import logging
import os
import boto3
import json
from botocore.exceptions import ClientError
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def send_sms(phone_number, message, message_type="TRANSACTIONAL"):
"""
Send SMS via AWS End User Messaging (PinpointSMSVoiceV2)
"""
try:
logger.info("Initializing AWS Pinpoint SMS Voice V2 client")
client = boto3.client("pinpoint-sms-voice-v2")
phone_pool_id = os.environ.get("AWS_SMS_PHONE_POOL_ID")
configuration_set = os.environ.get("AWS_SMS_CONFIGURATION_SET_NAME")
logger.info("Sending SMS Using Configuration Set: %s", configuration_set)
logger.info("Sending SMS Using Phone Pool ID: %s", phone_pool_id)
params = {
"DestinationPhoneNumber": phone_number,
"OriginationIdentity": phone_pool_id,
"MessageBody": message,
"MessageType": message_type
}
# Add configuration set for tracking
if configuration_set:
params["ConfigurationSetName"] = configuration_set
# Add context for tracking (optional)
params["Context"] = {
"ApplicationName": "template-app",
"Environment": "dev"
}
if check_opt_out_status(phone_number).get("opted_out"):
logger.warning("Phone number %s has opted out of SMS messages. Aborting send.", phone_number)
return {
"success": False,
"error": f"Phone number {phone_number} has opted out of SMS messages."
}
response = client.send_text_message(**params)
return {
"success": True,
"message_id": response.get("MessageId"),
"response": response
}
except ClientError as e:
error_code = e.response["Error"]["Code"]
error_message = e.response["Error"]["Message"]
logger.error(f"ClientError: {error_code} - {error_message}")
return {
"success": False,
"error": error_message,
"error_code": error_code,
"details": str(e)
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def check_opt_out_status(phone_number):
"""
Check if a phone number is opted out
"""
try:
client = boto3.client("pinpoint-sms-voice-v2")
response = client.describe_opted_out_numbers(
OptOutListName="default",
OptedOutNumbers=[phone_number]
)
opted_out_numbers = response.get("OptedOutNumbers", [])
is_opted_out = any(num["OptedOutNumber"] == phone_number for num in opted_out_numbers)
return {"opted_out": is_opted_out}
except Exception as e:
return {"error": str(e)}