SDK for Python (Boto3) を使用する Amazon Pinpoint の例 - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

SDK for Python (Boto3) を使用する Amazon Pinpoint の例

次のコード例は、Amazon Pinpoint で AWS SDK for Python (Boto3) を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、関連するシナリオやサービス間の例ではアクションのコンテキストが確認できます。

「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

各例には、 へのリンクが含まれています。このリンクには GitHub、コンテキスト内でコードをセットアップして実行する方法の手順が記載されています。

トピック

アクション

次のコード例は、Amazon Pinpoint を使用して E メールおよびテキストメッセージを送信する方法を示しています。

SDK for Python (Boto3)
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

E メールメッセージを送信します。

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()

SMS メッセージを送信します。

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()
  • API の詳細については、 SendMessages AWS SDK for Python (Boto3) API リファレンスの「」を参照してください。

次のコード例は、Amazon Pinpoint を使用して、テンプレートで作成された E メールとテキストメッセージを送信する方法を示しています。

SDK for Python (Boto3)
注記

には他にもがあります GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

既存の E メールテンプレートを使用して E メールメッセージを送信します。

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()

既存の SMS テンプレートを使用してテキストメッセージを送信します。

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()
  • API の詳細については、SendMessagesAWS「 SDK for Python (Boto3) API リファレンス」の「」を参照してください。