共用方式為


適用于 JavaScript 的 Azure 通訊 SMS 用戶端程式庫 - 1.1.0 版

Azure 通訊 SMS 服務可讓開發人員從可透過通訊服務購買的電話號碼傳送 SMS 訊息。

開始使用

Prerequisites

安裝

npm install @azure/communication-sms

如何取得電話號碼

電話號碼可以從 Azure入口網站取得並指派給通訊服務資源。 如需如何使用 Azure 入口網站 取得電話號碼的指示,請參閱 這裡

您也可以使用 @azure/communication-phone-numbers 套件來取得電話號碼。 如需如何使用套件的指示,請參閱 套件的讀我檔案

瀏覽器支援

JavaScript 套件組合

若要在瀏覽器中使用此用戶端程式庫,您必須先使用套件組合器。 如需如何執行這項操作的詳細資訊,請參閱我們的 統合檔

重要概念

SmsClient

SmsClient 是使用此用戶端程式庫的開發人員的主要介面。 它提供非同步方法來傳送 SMS 訊息。

範例

驗證

您可以從Azure入口網站中的通訊服務資源取得金鑰和/或連接字串。 擁有金鑰之後,您可以使用下列任何方法進行驗證:

使用連接字串

import { SmsClient } from "@azure/communication-sms";

const connectionString = `endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>`;
const client = new SmsClient(connectionString);

使用 建立認證 AzureKeyCredential

import { AzureKeyCredential } from "@azure/core-auth";
import { SmsClient } from "@azure/communication-sms";

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new AzureKeyCredential("<Base64-Encoded-Key>");
const client = new SmsClient(endpoint, credential);

使用 Azure Active Directory 受控識別

用戶端 API 金鑰驗證用於大部分的範例中,但您也可以使用 Azure 身分識別程式庫向 Azure Active Directory 進行驗證。 若要使用如下所示的 DefaultAzureCredential 提供者,或其他隨附于 Azure SDK 的認證提供者,請安裝 @azure/identity 套件:

npm install @azure/identity

套件 @azure/identity 提供各種認證類型,可供您的應用程式用來執行此動作。 的讀我檔案 @azure/identity 提供更多詳細資料和範例供您開始使用。 AZURE_CLIENT_SECRET,需要AZURE_CLIENT_ID和AZURE_TENANT_ID環境變數,才能建立 DefaultAzureCredential 物件。

import { DefaultAzureCredential } from "@azure/identity";
import { SmsClient } from "@azure/communication-sms";

const endpoint = "https://<resource-name>.communication.azure.com";
let credential = new DefaultAzureCredential();
const client = new SmsClient(endpoint, credential);

傳送 1:N SMS 訊息

若要傳送 SMS 訊息,請從 SmsClient 呼叫 函 send 式。 您必須傳入 SmsSendRequest 物件。 您也可以在 options 物件中新增傳遞,以指定是否應該啟用傳遞報表並設定報表的自訂標記。 的 SmsSendResult 陣列會傳回 。 successful旗標可用來驗證每個個別訊息是否成功傳送。

const sendResults = await client.send(
  {
    from: "<from-phone-number>", // Your E.164 formatted phone number used to send SMS
    to: ["<to-phone-number-1>", "<to-phone-number-2>"], // The list of E.164 formatted phone numbers to which message is being sent
    message: "Weekly Promotion!" // The message being sent
  },
  {
    enableDeliveryReport: true,
    tag: "marketing"
  }
);

for (const sendResult of sendResults) {
  if (sendResult.successful) {
    console.log("Success: ", sendResult);
  } else {
    console.error("Something went wrong when trying to send this message: ", sendResult);
  }
}

疑難排解

如果伺服器的要求失敗,SMS 作業將會擲回例外狀況。 如果錯誤是由個別訊息所造成,則不會擲回例外狀況,只有在發生因整體要求而失敗時。 請使用 successful 旗標來驗證每個個別的結果,以確認訊息是否已傳送。

try {
  const sendResults = await client.send({
    from: "<from-phone-number>", // Your E.164 formatted phone number used to send SMS
    to: ["<to-phone-number-1>", "<to-phone-number-2>"], // The list of E.164 formatted phone numbers to which message is being sent
    message: "Hello World via SMS!" // The message being sent
  });
  for (const sendResult of sendResults) {
    if (sendResult.successful) {
      console.log("Success: ", sendResult);
    } else {
      console.error("Something went wrong when trying to send this message: ", sendResult);
    }
  }
} catch (e) {
  console.error(e.message);
}

下一步

參與

如果您希望向此程式庫投稿,請參閱投稿指南,深入瞭解如何組建與測試程式碼。

曝光數