Created
May 29, 2020 01:19
-
-
Save aws-john/1a14bf454bfe7e86b38ef1f202756ced to your computer and use it in GitHub Desktop.
Send transactional SMS via Amazon PinPoint
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/pinpoint/latest/developerguide/send-messages-sms.html | |
# 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 Pinpoint by using the " | |
"AWS SDK for Python (Boto 3).") | |
# The Amazon Pinpoint project/application ID to use when you send this message. | |
# Make sure that the SMS channel is enabled for the project or application | |
# that you choose. | |
applicationId = "ce796be37f32f178af652b26eexample" | |
# 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('pinpoint',region_name=region) | |
try: | |
response = client.send_messages( | |
ApplicationId=applicationId, | |
MessageRequest={ | |
'Addresses': { | |
destinationNumber: { | |
'ChannelType': 'SMS' | |
} | |
}, | |
'MessageConfiguration': { | |
'SMSMessage': { | |
'Body': message, | |
'MessageType': messageType | |
} | |
} | |
} | |
) | |
except ClientError as e: | |
print(e.response['Error']['Message']) | |
else: | |
print("Message sent! Message ID: " | |
+ response['MessageResponse']['Result'][destinationNumber]['MessageId']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment