支援終止通知:2026 年 10 月 30 日, AWS 將結束對 Amazon Pinpoint 的支援。2026 年 10 月 30 日之後,您將無法再存取 Amazon Pinpoint 主控台或 Amazon Pinpoint 資源 (端點、客群、行銷活動、旅程和分析)。如需詳細資訊,請參閱 Amazon Pinpoint 終止支援。注意:與 SMS、語音、行動推播、OTP 和電話號碼驗證相關的 APIs 不受此變更影響,並受 AWS 最終使用者傳訊支援。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 Amazon Pinpoint 中使用 SDK for Python (Boto3) 的 OTP 程式碼範例
本節包含的密碼範例,說明如何使用 SDK for Python (Boto3) 傳送和驗證 OTP 密碼。
產生參考 ID
以下功能會根據收件人的電話號碼、收件人接收 OTP 的產品或品牌,以及請求來源 (可能是網站或應用程式中的頁面名稱),為每位收件人產生一組唯一的參考 ID。驗證 OTP 密碼時,您必須傳遞相同的參考 ID,才能驗證成功。傳送密碼和驗證密碼範例,都使用此公用程式函數。
此函數非必要,但如果要將 OTP 傳送和驗證程序範圍限定為特定交易,以在驗證期間輕鬆重新提交,此函數就很有用。您可以使用任何您要的參考 ID—這只是一個基本範例。但本節中的其他代碼範例仰賴此函數。
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import hashlib def generate_ref_id(destinationNumber,brandName,source): refId = brandName + source + destinationNumber return hashlib.md5(refId.encode()).hexdigest()
傳送 OTP 代碼
以下程式碼範例說明如何使用 SDK for Python (Boto3) 傳送 OTP 密碼。
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import boto3 from botocore.exceptions import ClientError from generate_ref_id import generate_ref_id ### Some variables that are unlikely to change from request to request. ### # The AWS Region that you want to use to send the message. region = "us-east-1" # The phone number or short code to send the message from. originationNumber = "+18555550142" # The project/application ID to use when you send the message. appId = "7353f53e6885409fa32d07cedexample" # The number of times the user can unsuccessfully enter the OTP code before it becomes invalid. allowedAttempts = 3 # Function that sends the OTP as an SMS message. def send_otp(destinationNumber,codeLength,validityPeriod,brandName,source,language): client = boto3.client('pinpoint',region_name=region) try: response = client.send_otp_message( ApplicationId=appId, SendOTPMessageRequestParameters={ 'Channel': 'SMS', 'BrandName': brandName, 'CodeLength': codeLength, 'ValidityPeriod': validityPeriod, 'AllowedAttempts': allowedAttempts, 'Language': language, 'OriginationIdentity': originationNumber, 'DestinationIdentity': destinationNumber, 'ReferenceId': generate_ref_id(destinationNumber,brandName,source) } ) except ClientError as e: print(e.response) else: print(response) # Send a message to +14255550142 that contains a 6-digit OTP that is valid for 15 minutes. The # message will include the brand name "ExampleCorp", and the request originated from a part of your # site or application called "CreateAccount". The US English message template should be used to # send the message. send_otp("+14255550142",6,15,"ExampleCorp","CreateAccount","en-US")
驗證 OTP 代碼
以下程式碼範例說明如何使用 SDK for Python (Boto3) 驗證已傳送的 OTP 密碼。為了使驗證步驟成功,您的請求包含的參考 ID,必須與傳送訊息的參考 ID 完全相符。
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 import boto3 from botocore.exceptions import ClientError from generate_ref_id import generate_ref_id # The AWS Region that you want to use to send the message. region = "us-east-1" # The project/application ID to use when you send the message. appId = "7353f53e6885409fa32d07cedexample" # Function that verifies the OTP code. def verify_otp(destinationNumber,otp,brandName,source): client = boto3.client('pinpoint',region_name=region) try: response = client.verify_otp_message( ApplicationId=appId, VerifyOTPMessageRequestParameters={ 'DestinationIdentity': destinationNumber, 'ReferenceId': generate_ref_id(destinationNumber,brandName,source), 'Otp': otp } ) except ClientError as e: print(e.response) else: print(response) # Verify the OTP 012345, which was sent to +14255550142. The brand name ("ExampleCorp") and the # source name ("CreateAccount") are used to generate the correct reference ID. verify_otp("+14255550142","012345","ExampleCorp","CreateAccount")