OTP Amazon Pinpoint에서 Python(Boto3)에 사용하기 SDK 위한 코드 예제 - Amazon Pinpoint

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

OTP Amazon Pinpoint에서 Python(Boto3)에 사용하기 SDK 위한 코드 예제

이 섹션에는 PythonSDK용 (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(Boto3)SDK용 를 사용하여 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 코드 검증

다음 코드 예제에서는 for Python(Boto3)SDK을 사용하여 이미 전송한 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")