Amazon Pinpoint examples using SDK for Python (Boto3) - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Amazon Pinpoint examples using SDK for Python (Boto3)

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Python (Boto3) with Amazon Pinpoint.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use SendMessages.

SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Send an email message.

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_email_message( pinpoint_client, app_id, sender, to_addresses, char_set, subject, html_message, text_message, ): """ Sends an email message with HTML and plain text versions. :param pinpoint_client: A Boto3 Pinpoint client. :param app_id: The Amazon Pinpoint project ID to use when you send this message. :param sender: The "From" address. This address must be verified in Amazon Pinpoint in the AWS Region you're using to send email. :param to_addresses: The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses must be verified. :param char_set: The character encoding to use for the subject line and message body of the email. :param subject: The subject line of the email. :param html_message: The body of the email for recipients whose email clients can display HTML content. :param text_message: The body of the email for recipients whose email clients don't support HTML content. :return: A dict of to_addresses and their message IDs. """ try: response = pinpoint_client.send_messages( ApplicationId=app_id, MessageRequest={ "Addresses": { to_address: {"ChannelType": "EMAIL"} for to_address in to_addresses }, "MessageConfiguration": { "EmailMessage": { "FromAddress": sender, "SimpleEmail": { "Subject": {"Charset": char_set, "Data": subject}, "HtmlPart": {"Charset": char_set, "Data": html_message}, "TextPart": {"Charset": char_set, "Data": text_message}, }, } }, }, ) except ClientError: logger.exception("Couldn't send email.") raise else: return { to_address: message["MessageId"] for to_address, message in response["MessageResponse"]["Result"].items() } def main(): app_id = "ce796be37f32f178af652b26eexample" sender = "sender@example.com" to_address = "recipient@example.com" char_set = "UTF-8" subject = "Amazon Pinpoint Test (SDK for Python (Boto3))" text_message = """Amazon Pinpoint Test (SDK for Python) ------------------------------------- This email was sent with Amazon Pinpoint using the AWS SDK for Python (Boto3). For more information, see https://aws.amazon.com/sdk-for-python/ """ html_message = """<html> <head></head> <body> <h1>Amazon Pinpoint Test (SDK for Python (Boto3)</h1> <p>This email was sent with <a href='https://aws.amazon.com/pinpoint/'>Amazon Pinpoint</a> using the <a href='https://aws.amazon.com/sdk-for-python/'> AWS SDK for Python (Boto3)</a>.</p> </body> </html> """ print("Sending email.") message_ids = send_email_message( boto3.client("pinpoint"), app_id, sender, [to_address], char_set, subject, html_message, text_message, ) print(f"Message sent! Message IDs: {message_ids}") if __name__ == "__main__": main()

Send an SMS message.

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_sms_message( pinpoint_client, app_id, origination_number, destination_number, message, message_type, ): """ Sends an SMS message with Amazon Pinpoint. :param pinpoint_client: A Boto3 Pinpoint client. :param app_id: The Amazon Pinpoint project/application ID to use when you send this message. The SMS channel must be enabled for the project or application. :param destination_number: The recipient's phone number in E.164 format. :param origination_number: The phone number to send the message from. This phone number must be associated with your Amazon Pinpoint account and be in E.164 format. :param message: The content of the SMS message. :param message_type: The type of SMS message that you want to send. If you send time-sensitive content, specify TRANSACTIONAL. If you send marketing-related content, specify PROMOTIONAL. :return: The ID of the message. """ try: response = pinpoint_client.send_messages( ApplicationId=app_id, MessageRequest={ "Addresses": {destination_number: {"ChannelType": "SMS"}}, "MessageConfiguration": { "SMSMessage": { "Body": message, "MessageType": message_type, "OriginationNumber": origination_number, } }, }, ) except ClientError: logger.exception("Couldn't send message.") raise else: return response["MessageResponse"]["Result"][destination_number]["MessageId"] def main(): app_id = "ce796be37f32f178af652b26eexample" origination_number = "+12065550199" destination_number = "+14255550142" message = ( "This is a sample message sent from Amazon Pinpoint by using the AWS SDK for " "Python (Boto 3)." ) message_type = "TRANSACTIONAL" print("Sending SMS message.") message_id = send_sms_message( boto3.client("pinpoint"), app_id, origination_number, destination_number, message, message_type, ) print(f"Message sent! Message ID: {message_id}.") if __name__ == "__main__": main()

Send an email message with an existing email template.

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_templated_email_message( pinpoint_client, project_id, sender, to_addresses, template_name, template_version ): """ Sends an email message with HTML and plain text versions. :param pinpoint_client: A Boto3 Pinpoint client. :param project_id: The Amazon Pinpoint project ID to use when you send this message. :param sender: The "From" address. This address must be verified in Amazon Pinpoint in the AWS Region you're using to send email. :param to_addresses: The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses must be verified. :param template_name: The name of the email template to use when sending the message. :param template_version: The version number of the message template. :return: A dict of to_addresses and their message IDs. """ try: response = pinpoint_client.send_messages( ApplicationId=project_id, MessageRequest={ "Addresses": { to_address: {"ChannelType": "EMAIL"} for to_address in to_addresses }, "MessageConfiguration": {"EmailMessage": {"FromAddress": sender}}, "TemplateConfiguration": { "EmailTemplate": { "Name": template_name, "Version": template_version, } }, }, ) except ClientError: logger.exception("Couldn't send email.") raise else: return { to_address: message["MessageId"] for to_address, message in response["MessageResponse"]["Result"].items() } def main(): project_id = "296b04b342374fceb661bf494example" sender = "sender@example.com" to_addresses = ["recipient@example.com"] template_name = "My_Email_Template" template_version = "1" print("Sending email.") message_ids = send_templated_email_message( boto3.client("pinpoint"), project_id, sender, to_addresses, template_name, template_version, ) print(f"Message sent! Message IDs: {message_ids}") if __name__ == "__main__": main()

Send a text message with an existing SMS template.

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_templated_sms_message( pinpoint_client, project_id, destination_number, message_type, origination_number, template_name, template_version, ): """ Sends an SMS message to a specific phone number using a pre-defined template. :param pinpoint_client: A Boto3 Pinpoint client. :param project_id: An Amazon Pinpoint project (application) ID. :param destination_number: The phone number to send the message to. :param message_type: The type of SMS message (promotional or transactional). :param origination_number: The phone number that the message is sent from. :param template_name: The name of the SMS template to use when sending the message. :param template_version: The version number of the message template. :return The ID of the message. """ try: response = pinpoint_client.send_messages( ApplicationId=project_id, MessageRequest={ "Addresses": {destination_number: {"ChannelType": "SMS"}}, "MessageConfiguration": { "SMSMessage": { "MessageType": message_type, "OriginationNumber": origination_number, } }, "TemplateConfiguration": { "SMSTemplate": {"Name": template_name, "Version": template_version} }, }, ) except ClientError: logger.exception("Couldn't send message.") raise else: return response["MessageResponse"]["Result"][destination_number]["MessageId"] def main(): region = "us-east-1" origination_number = "+18555550001" destination_number = "+14255550142" project_id = "7353f53e6885409fa32d07cedexample" message_type = "TRANSACTIONAL" template_name = "My_SMS_Template" template_version = "1" message_id = send_templated_sms_message( boto3.client("pinpoint", region_name=region), project_id, destination_number, message_type, origination_number, template_name, template_version, ) print(f"Message sent! Message ID: {message_id}.") if __name__ == "__main__": main()
  • For API details, see SendMessages in AWS SDK for Python (Boto3) API Reference.