Amazon Pinpoint에서 SDK for Python (Boto3)을 사용한 OTP 코드 예제 - Amazon Pinpoint

지원 종료 공지: 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 코드 예제

이 단원에는 Python용 SDK(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 코드 전송

다음 코드 예제는 Python용 SDK(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 코드 확인

다음 코드 예제는 Python용 SDK(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")