共用方式為


適用于 Python 的 Azure 通訊 SMS 套件用戶端程式庫 - 1.0.1 版

此套件包含適用于 SMS Azure 通訊服務 的 Python SDK。 在這裡深入瞭解Azure 通訊服務

| 原始程式碼套件 (Pypi) | API 參考檔 | 產品檔

開始使用

Prerequisites

  • 需要 Python 2.7 或 3.6 或更新版本,才能使用此套件。
  • 已部署通訊服務資源。 您可以使用Azure 入口網站或Azure PowerShell來設定。
  • 您必須設定與 Azure 訂用帳戶相關聯的電話號碼

安裝套件

使用 pip安裝適用于 Python 的 Azure 通訊 SMS 用戶端程式庫:

pip install azure-communication-sms

重要概念

Azure 通訊 SMS 套件可用來執行下列動作:

  • 傳送 1:1 SMS 訊息
  • 傳送 1:N SMS 訊息

範例

下一節提供數個程式碼片段,涵蓋一些最常見的Azure 通訊服務工作,包括:

用戶端初始化

若要初始化 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 作業將會擲回例外狀況。 SMS 用戶端會引發 Azure Core中定義的例外狀況。 如果錯誤是由個別訊息所造成,則不會擲回例外狀況,只有在整體要求失敗時才會擲回。 請使用成功旗標來驗證每個個別的結果,以確認是否已傳送訊息。

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)

下一步

更多的程式碼範例

如需如何使用此程式庫傳送簡訊的詳細範例,請參閱 範例 目錄。

提供意見反應

如果您遇到任何錯誤或有建議,請在專案的 [問題 ] 區段中提出問題

參與

此專案歡迎參與和提供建議。 大部分的參與都要求您同意「參與者授權合約 (CLA)」,宣告您有權且確實授與我們使用投稿的權利。 如需詳細資料,請前往 https://cla.microsoft.com

當您提交提取要求時,CLA Bot 會自動判斷您是否需要提供 CLA,並適當地裝飾 PR (例如標籤、註解)。 請遵循 bot 提供的指示。 您只需要使用我們的 CLA 在所有存放庫上執行此動作一次。

此專案採用 Microsoft Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題集,如有任何其他問題或意見請連絡 opencode@microsoft.com