diff --git a/sdk/communication/communication-sms/README.md b/sdk/communication/communication-sms/README.md index d6a06c1b6840..d1c62c33ab1c 100644 --- a/sdk/communication/communication-sms/README.md +++ b/sdk/communication/communication-sms/README.md @@ -39,7 +39,7 @@ You can get a key and/or connection string from your Communication Services reso ```typescript import { SmsClient } from "@azure/communication-sms"; -const connectionString = `endpoint=;accessKey=`; +const connectionString = `endpoint=https://.communication.azure.com/;accessKey=`; const client = new SmsClient(connectionString); ``` @@ -49,8 +49,9 @@ const client = new SmsClient(connectionString); import { AzureKeyCredential } from "@azure/core-auth"; import { SmsClient } from "@azure/communication-sms"; +const endpoint = "https://.communication.azure.com"; const credential = new AzureKeyCredential(""); -const client = new SmsClient("", credential); +const client = new SmsClient(endpoint, credential); ``` ### Using Azure Active Directory managed identity @@ -62,33 +63,70 @@ npm install @azure/identity ``` The [`@azure/identity`][azure_identity] package provides a variety of credential types that your application can use to do this. The README for @azure/identity provides more details and samples to get you started. +AZURE_CLIENT_SECRET, AZURE_CLIENT_ID and AZURE_TENANT_ID environment variables are needed to create a DefaultAzureCredential object. ```typescript import { DefaultAzureCredential } from "@azure/identity"; import { SmsClient } from "@azure/communication-sms"; +const endpoint = "https://.communication.azure.com"; let credential = new DefaultAzureCredential(); const client = new SmsClient(endpoint, credential); ``` -## Sending SMS - -```typescript -import { AzureKeyCredential } from "@azure/core-auth"; -import { SmsClient } from "@azure/communication-sms"; +## Send a 1:N SMS Message -const credential = new AzureKeyCredential(""); -const client = new SmsClient("", credential); +To send a SMS message, call the `send` function from the `SmsClient`. You need to pass in a `SmsSendRequest` object. +You may also add pass in an options object to specify whether the delivery report should be enabled and set custom tags for the report. +An array of `SmsSendResult` is returned. A `successful` flag is used to validate if each individual message was sent successfully. -const response = await client.send({ - from: "+12345678902", - to: ["+12345678901"], - message: "Hey there!" -}); +```typescript +const sendResults = await client.send( + { + from: "", // Your E.164 formatted phone number used to send SMS + to: ["", ""], // 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); + } +} ``` ## Troubleshooting +SMS operations will throw an exception if the request to the server fails. +Exceptions will not be thrown if the error is caused by an individual message, only if something fails with the overall request. +Please use the `successful` flag to validate each individual result to verify if the message was sent. + +```typescript +try { + const sendResults = await client.send({ + from: "", // Your E.164 formatted phone number used to send SMS + to: ["", ""], // 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); +} +``` + ## Next steps Please take a look at the diff --git a/sdk/communication/communication-sms/samples/javascript/package.json b/sdk/communication/communication-sms/samples/javascript/package.json index 659f5380930c..752ecf799cd5 100644 --- a/sdk/communication/communication-sms/samples/javascript/package.json +++ b/sdk/communication/communication-sms/samples/javascript/package.json @@ -26,6 +26,7 @@ "sideEffects": false, "dependencies": { "@azure/communication-sms": "latest", + "@azure/identity": "^1.1.0", "dotenv": "^8.2.0" } } diff --git a/sdk/communication/communication-sms/samples/javascript/sample.env b/sdk/communication/communication-sms/samples/javascript/sample.env index 87b7cca169c9..eae0684c3985 100644 --- a/sdk/communication/communication-sms/samples/javascript/sample.env +++ b/sdk/communication/communication-sms/samples/javascript/sample.env @@ -1,3 +1,9 @@ # Used in most samples. Retrieve these values from a Communication Service instance # in the Azure Portal. -COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.net/;accessKey=" +COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.com/;accessKey=" + +# For token authentication +COMMUNICATION_ENDPOINT="https://.communication.azure.com", +AZURE_CLIENT_ID: "YourClientId", +AZURE_TENANT_ID: "YourTenantId", +AZURE_CLIENT_SECRET: "YourClientSecret" \ No newline at end of file diff --git a/sdk/communication/communication-sms/samples/javascript/sendSms.js b/sdk/communication/communication-sms/samples/javascript/sendSms.js index a425eb128841..0c393952a44b 100644 --- a/sdk/communication/communication-sms/samples/javascript/sendSms.js +++ b/sdk/communication/communication-sms/samples/javascript/sendSms.js @@ -7,36 +7,80 @@ */ const { SmsClient } = require("@azure/communication-sms"); +const { DefaultAzureCredential } = require("@azure/identity"); // Load the .env file if it exists const dotenv = require("dotenv"); -dotenv.config(); +dotenv.config({ + path: "sample.env" +}); -// You will need to set this environment variables or edit the following values -const connectionString = - process.env["COMMUNICATION_CONNECTION_STRING"] || ""; +export const createSmsClient = () => { + // You will need to set this environment variable or edit the following value + const connectionString = + process.env["COMMUNICATION_CONNECTION_STRING"] || + "endpoint=https://.communication.azure.com/;"; + return new SmsClient(connectionString); +}; -async function main() { - console.log("== Send SMS Message Sample =="); +export const createSmsClientWithToken = () => { + // You will need to set this environment variable or edit the following value + const endpoint = + process.env["COMMUNICATION_ENDPOINT"] || "https://.communication.azure.com"; + //AZURE_CLIENT_SECRET, AZURE_CLIENT_ID and AZURE_TENANT_ID environment variables are needed to create a DefaultAzureCredential object. + return new SmsClient(endpoint, new DefaultAzureCredential()); +}; + +export const sendingSmsMessage = async () => { + console.log("== Send SMS Message, Default Options =="); + const client = createSmsClient(); + const sendResults = await client.send({ + from: "", // Your E.164 formatted phone number used to send SMS + to: ["", ""], // The list of E.164 formatted phone numbers to which message is being sent + message: "Hello World via SMS!" // The message being sent + }); - const client = new SmsClient(connectionString); + 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); + } + } + console.log("== Done: Send SMS Message, Default Options =="); +}; - // Send SMS message +export const sendingSmsMessageWithOptions = async () => { + console.log("== Send SMS Message With Options =="); + const client = createSmsClient(); const sendResults = await client.send( { - from: "", // Your E.164 formatted phone number used to send SMS - to: ["", "", ""], // The list of E.164 formatted phone numbers to which message is being send - message: "Hello World via SMS!" // The message being sent + from: "", // Your E.164 formatted phone number used to send SMS + to: ["", ""], // The list of E.164 formatted phone numbers to which message is being sent + message: "Weekly Promotion!" // The message being sent }, { enableDeliveryReport: true, - tag: "customTag" + tag: "marketing" } ); + for (const sendResult of sendResults) { - console.log("result: ", sendResult); + if (sendResult.successful) { + console.log("Success: ", sendResult); + } else { + console.error("Something went wrong when trying to send this message: ", sendResult); + } } -} + console.log("== Done: Send SMS Message With Options =="); +}; + +export const main = async () => { + console.log("== Send SMS Message Sample =="); + await sendingSmsMessage(); + await sendingSmsMessageWithOptions(); + console.log("== SMS Sample Complete! =="); +}; main().catch((error) => { console.error("Encountered an error while sending sms: "); diff --git a/sdk/communication/communication-sms/samples/typescript/package.json b/sdk/communication/communication-sms/samples/typescript/package.json index fc3b96c709f4..856e92759e67 100644 --- a/sdk/communication/communication-sms/samples/typescript/package.json +++ b/sdk/communication/communication-sms/samples/typescript/package.json @@ -31,6 +31,7 @@ "sideEffects": false, "dependencies": { "@azure/communication-sms": "latest", + "@azure/identity": "^1.1.0", "dotenv": "^8.2.0" }, "devDependencies": { diff --git a/sdk/communication/communication-sms/samples/typescript/sample.env b/sdk/communication/communication-sms/samples/typescript/sample.env index 87b7cca169c9..eae0684c3985 100644 --- a/sdk/communication/communication-sms/samples/typescript/sample.env +++ b/sdk/communication/communication-sms/samples/typescript/sample.env @@ -1,3 +1,9 @@ # Used in most samples. Retrieve these values from a Communication Service instance # in the Azure Portal. -COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.net/;accessKey=" +COMMUNICATION_CONNECTION_STRING="endpoint=https://.communication.azure.com/;accessKey=" + +# For token authentication +COMMUNICATION_ENDPOINT="https://.communication.azure.com", +AZURE_CLIENT_ID: "YourClientId", +AZURE_TENANT_ID: "YourTenantId", +AZURE_CLIENT_SECRET: "YourClientSecret" \ No newline at end of file diff --git a/sdk/communication/communication-sms/samples/typescript/src/sendSms.ts b/sdk/communication/communication-sms/samples/typescript/src/sendSms.ts index 9f26b1f6d256..1835a8107b9a 100644 --- a/sdk/communication/communication-sms/samples/typescript/src/sendSms.ts +++ b/sdk/communication/communication-sms/samples/typescript/src/sendSms.ts @@ -7,38 +7,83 @@ */ import { SmsClient } from "@azure/communication-sms"; +import { DefaultAzureCredential } from "@azure/identity"; // Load the .env file if it exists import * as dotenv from "dotenv"; -dotenv.config(); +dotenv.config({ + path: "sample.env" +}); -// You will need to set this environment variables or edit the following values -const connectionString = - process.env["COMMUNICATION_CONNECTION_STRING"] || ""; +export const createSmsClient = () => { + // You will need to set this environment variable or edit the following value + const connectionString = + process.env["COMMUNICATION_CONNECTION_STRING"] || + "endpoint=https://.communication.azure.com/;"; + return new SmsClient(connectionString); +}; -export const main = async () => { - console.log("== Send SMS Message Sample =="); +export const createSmsClientWithToken = () => { + // You will need to set this environment variable or edit the following value + const endpoint = + process.env["COMMUNICATION_ENDPOINT"] || "https://.communication.azure.com"; + //AZURE_CLIENT_SECRET, AZURE_CLIENT_ID and AZURE_TENANT_ID environment variables are needed to create a DefaultAzureCredential object. + return new SmsClient(endpoint, new DefaultAzureCredential()); +}; - const client = new SmsClient(connectionString); +export const sendingSmsMessage = async () => { + console.log("== Send SMS Message, Default Options =="); + const client = createSmsClient(); + const sendResults = await client.send({ + from: "", // Your E.164 formatted phone number used to send SMS + to: ["", ""], // The list of E.164 formatted phone numbers to which message is being sent + message: "Hello World via SMS!" // The message being sent + }); - // Send SMS message + 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); + } + } + console.log("== Done: Send SMS Message, Default Options =="); +}; + +export const sendingSmsMessageWithOptions = async () => { + console.log("== Send SMS Message With Options =="); + const client = createSmsClient(); const sendResults = await client.send( { - from: "", // Your E.164 formatted phone number used to send SMS - to: ["", "", ""], // The list of E.164 formatted phone numbers to which message is being send - message: "Hello World via SMS!" // The message being sent + from: "", // Your E.164 formatted phone number used to send SMS + to: ["", ""], // The list of E.164 formatted phone numbers to which message is being sent + message: "Weekly Promotion!" // The message being sent }, { enableDeliveryReport: true, - tag: "customTag" + tag: "marketing" } ); for (const sendResult of sendResults) { - console.log("result: ", sendResult); + if (sendResult.successful) { + console.log("Success: ", sendResult); + } else { + console.error("Something went wrong when trying to send this message: ", sendResult); + } } + console.log("== Done: Send SMS Message With Options =="); +}; + +export const main = async () => { + console.log("== Send SMS Message Sample =="); + await sendingSmsMessage(); + await sendingSmsMessageWithOptions(); + console.log("== SMS Sample Complete! =="); }; main().catch((error) => { - console.error("Encountered an error while sending sms: ", error); + console.error("Encountered an error while sending sms: "); + console.error("Request: \n", error.request); + console.error("\nResponse: \n", error.response); });