Pemberitahuan akhir dukungan: Pada 30 Oktober 2026, AWS akan mengakhiri dukungan untuk Amazon Pinpoint. Setelah 30 Oktober 2026, Anda tidak akan lagi dapat mengakses konsol Amazon Pinpoint atau sumber daya Amazon Pinpoint (titik akhir, segmen, kampanye, perjalanan, dan analitik). Untuk informasi selengkapnya, lihat Amazon Pinpoint akhir dukungan. Catatan: APIs terkait dengan SMS, suara, push seluler, OTP, dan validasi nomor telepon tidak terpengaruh oleh perubahan ini dan didukung oleh AWS End User Messaging.
Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Contoh kode OTP untuk menggunakan SDK for Python (Boto3) di Amazon Pinpoint
Bagian ini berisi contoh kode yang menunjukkan cara menggunakan SDK for Python (Boto3) untuk mengirim dan memverifikasi kode OTP.
Menghasilkan ID referensi
Fungsi berikut menghasilkan ID referensi unik untuk setiap penerima, berdasarkan nomor telepon penerima, produk atau merek tempat penerima menerima OTP, dan sumber permintaan (yang bisa berupa nama halaman di situs atau aplikasi, misalnya). Ketika Anda memverifikasi kode OTP, Anda harus melewati ID referensi yang identik agar validasi berhasil. Contoh kode pengiriman dan validasi menggunakan fungsi utilitas ini.
Fungsi ini tidak diperlukan, tetapi ini adalah cara yang berguna untuk mencakup proses pengiriman dan verifikasi OTP ke transaksi tertentu dengan cara yang dapat dengan mudah dikirimkan kembali selama langkah verifikasi. Anda dapat menggunakan ID referensi apa pun yang Anda inginkan—ini hanyalah contoh dasar. Namun, contoh kode lain di bagian ini bergantung pada fungsi ini.
# 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()
Kirim kode OTP
Contoh kode berikut menunjukkan cara menggunakan SDK for Python (Boto3) untuk mengirim kode 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")
Validasi kode OTP
Contoh kode berikut menunjukkan cara menggunakan SDK for Python (Boto3) untuk memverifikasi kode OTP yang sudah Anda kirim. Agar langkah validasi berhasil, permintaan Anda harus menyertakan ID referensi yang sama persis dengan ID referensi yang digunakan untuk mengirim pesan.
# 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")