Python용 Azure Communication SMS 패키지 클라이언트 라이브러리 - 버전 1.0.1

이 패키지에는 SMS용 Azure Communication Services 대한 Python SDK가 포함되어 있습니다. Azure Communication Services 대한 자세한 내용은 여기를 참조하세요.

소스 코드 | 패키지(Pypi) | API 참조 설명서 | 제품 설명서

시작

필수 조건

  • 이 패키지를 사용하려면 Python 2.7 또는 3.6 이상이 필요합니다.
  • 배포된 Communication Services 리소스. Azure Portal 또는 Azure PowerShell 사용하여 설정할 수 있습니다.
  • Azure 구독과 연결된 전화 번호가 구성되어 있어야 합니다.

패키지 설치

pip를 사용하여 Python용 Azure Communication SMS 클라이언트 라이브러리를 설치합니다.

pip install azure-communication-sms

주요 개념

Azure Communication SMS 패키지는 다음을 수행하는 데 사용됩니다.

  • 1:1 SMS 메시지 보내기
  • 1:N SMS 메시지 보내기

예제

다음 섹션에서는 다음을 포함하여 가장 일반적인 Azure Communication Services 작업을 다루는 몇 가지 코드 조각을 제공합니다.

클라이언트 초기화

SMS 클라이언트를 초기화하기 위해 연결 문자열을 사용하여 인스턴스화할 수 있습니다. 또는 DefaultAzureCredential을 사용하여 Active Directory 인증을 사용할 수도 있습니다.

from azure.communication.sms import SmsClient
from azure.identity import DefaultAzureCredential

connection_str = "endpoint=ENDPOINT;accessKey=KEY"
sms_client = SmsClient.from_connection_string(connection_string)

# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
endpoint = "https://<RESOURCE_NAME>.communication.azure.com"
sms_client = SmsClient(endpoint, DefaultAzureCredential())

1:1 SMS 메시지 보내기

클라이언트가 초기화되면 메서드를 send 호출할 수 있습니다.

from azure.communication.sms import SendSmsOptions

sms_responses = sms_client.send(
    from_="<from-phone-number>",
    to="<to-phone-number-1>",
    message="Hello World via SMS",
    enable_delivery_report=True, # optional property
    tag="custom-tag") # optional property
  • from_: 통신 서비스와 연결된 SMS 사용 전화 번호입니다.
  • to: 메시지를 보내려는 전화 번호 또는 전화 번호 목록입니다.
  • message: 보낼 메시지입니다.
  • enable_delivery_report: 배달 보고를 구성하는 데 사용할 수 있는 선택적 매개 변수입니다. 이 기능은 SMS 메시지가 전달될 때 이벤트를 내보내려는 시나리오에 유용합니다.
  • tag: 사용자 지정 태그 지정을 구성하는 데 사용할 수 있는 선택적 매개 변수입니다.

1:N SMS 메시지 보내기

클라이언트가 초기화되면 메서드를 send 호출할 수 있습니다.

from azure.communication.sms import SendSmsOptions

sms_responses = sms_client.send(
    from_="<from-phone-number>",
    to=["<to-phone-number-1>", "<to-phone-number-2>", "<to-phone-number-3>"],
    message="Hello World via SMS",
    enable_delivery_report=True, # optional property
    tag="custom-tag") # optional property
  • from_: 통신 서비스와 연결된 SMS 사용 전화 번호입니다.
  • to: 메시지를 보내려는 전화 번호 또는 전화 번호 목록입니다.
  • message: 보낼 메시지입니다.
  • enable_delivery_report: 배달 보고를 구성하는 데 사용할 수 있는 선택적 매개 변수입니다. 이 기능은 SMS 메시지가 전달될 때 이벤트를 내보내려는 시나리오에 유용합니다.
  • tag: 사용자 지정 태그 지정을 구성하는 데 사용할 수 있는 선택적 매개 변수입니다.

문제 해결

서버에 대한 요청이 실패하면 SMS 작업이 예외를 throw합니다. SMS 클라이언트는 Azure Core에 정의된 예외를 발생합니다. 오류가 개별 메시지로 인해 발생하는 경우 전체 요청과 함께 오류가 발생하는 경우에만 예외가 throw되지 않습니다. 성공적인 플래그를 사용하여 각 개별 결과의 유효성을 검사하여 메시지가 전송되었는지 확인하세요.

try:
    sms_responses = sms_client.send(
        from_="<leased-phone-number>",
        to=["<to-phone-number-1>", "<to-phone-number-2>", "<to-phone-number-3>"],
        message="Hello World via SMS")
        
    for sms_response in sms_responses:
        if (sms_response.successful):
            print("Message with message id {} was successful sent to {}"
            .format(sms_response.message_id, sms_response.to))
        else:
            print("Message failed to send to {} with the status code {} and error: {}"
            .format(sms_response.to, sms_response.http_status_code, sms_response.error_message))
except Exception as ex:
    print('Exception:')
    print(ex)

다음 단계

추가 샘플 코드

이 라이브러리를 사용하여 SMS를 보내는 방법에 대한 자세한 예제는 샘플 디렉터리를 참조하세요.

피드백 제공

버그가 발생하거나 제안이 있는 경우 프로젝트의 문제 섹션에 문제를 제출하세요.

참여

이 프로젝트에 대한 기여와 제안을 환영합니다. 대부분의 경우 기여하려면 권한을 부여하며 실제로 기여를 사용할 권한을 당사에 부여한다고 선언하는 CLA(기여자 라이선스 계약)에 동의해야 합니다. 자세한 내용은 https://cla.microsoft.com 을 참조하세요.

끌어오기 요청을 제출하면 CLA-bot은 CLA를 제공하고 PR을 적절하게 데코레이팅해야 하는지 여부를 자동으로 결정합니다(예: 레이블, 설명). 봇에서 제공하는 지침을 따르기만 하면 됩니다. 이 작업은 CLA를 사용하여 모든 리포지토리에서 한 번만 수행하면 됩니다.

이 프로젝트에는 Microsoft Open Source Code of Conduct(Microsoft 오픈 소스 준수 사항)가 적용됩니다. 자세한 내용은 Code of Conduct FAQ(규정 FAQ)를 참조하세요. 또는 추가 질문이나 의견은 opencode@microsoft.com으로 문의하세요.