Created
May 29, 2020 01:19
-
-
Save aws-john/fd79c39d97435ae10ce6d249bcd78d45 to your computer and use it in GitHub Desktop.
Send transactional SMS via Amazon SNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from https://docs.aws.amazon.com/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk | |
# Disclaimer: this is sample code for demonstration purposes only | |
import boto3 | |
from botocore.exceptions import ClientError | |
# The AWS Region that you want to use to send the message. For a list of | |
# AWS Regions where the Amazon Pinpoint API is available, see | |
# https://docs.aws.amazon.com/pinpoint/latest/apireference/ | |
region = "us-west-2" | |
# The recipient's phone number. For best results, you should specify the | |
# phone number in E.164 format. | |
destinationNumber = "+14255550142" | |
# The content of the SMS message. | |
message = ("This is a sample message sent from Amazon SNS by using the " | |
"AWS SDK for Python (Boto 3).") | |
# The type of SMS message that you want to send. If you plan to send | |
# time-sensitive content, specify Transactional. If you plan to send | |
# marketing-related content, specify Promotional. | |
messageType = "Transactional" | |
# Create a new client and specify a region. | |
client = boto3.client('sns',region_name=region) | |
try: | |
response = client.set_sms_attributes(attributes={"DefaultSMSType":messageType}) | |
response = client.publish( | |
PhoneNumber=destinationNumber, | |
Message=message | |
) | |
except ClientError as e: | |
print(e.response['Error']['Message']) | |
else: | |
print("Message sent! Message ID: " | |
+ response['MessageId']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment