Skip to content

Commit

Permalink
[Communication][SMS] Add javascript samples and update readme (#32403)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR


### Issues associated with this PR


### Describe the problem that is addressed by this PR
Add missing JS samples, update README

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
  • Loading branch information
phermanov-msft authored Jan 2, 2025
1 parent 7642987 commit 594557a
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 1 deletion.
54 changes: 54 additions & 0 deletions sdk/communication/communication-sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,60 @@ for (const sendResult of sendResults) {
}
```

## Check if a list of recipients is in the Opt Out list
To check if the recipients are in the Opt Out list, call the `check` function from the `SmsClient.optOuts` with a list of recipient phone numbers.

```typescript
const optOutCheckResults = await client.optOuts.check(
from: "<from-phone-number>", // Your E.164 formatted phone number used to send SMS
to: ["<to-phone-number-1>", "<to-phone-number-2>"], E.164 formatted recipient phone numbers
);

for (const optOutCheckResult of optOutCheckResults) {
if (optOutCheckResult.httpStatusCode == 200) {
console.log("Success: ", optOutCheckResult);
} else {
console.error("Something went wrong when trying to send opt out check request: ", optOutCheckResult);
}
}
```

## Add a list of recipients to Opt Out list
To add the list of recipients to Opt Out list, call the `add` function from the `SmsClient.pptOuts` with a list of recipient phone numbers.

```typescript
const optOutAddResults = await client.optOuts.add(
from: "<from-phone-number>", // Your E.164 formatted phone number used to send SMS
to: ["<to-phone-number-1>", "<to-phone-number-2>"], // E.164 formatted recipient phone numbers
);

for (const optOutAddResult of optOutAddResults) {
if (optOutAddResult.httpStatusCode == 200) {
console.log("Success: ", optOutAddResult);
} else {
console.error("Something went wrong when trying to send opt out add request: ", optOutAddResult);
}
}
```

## Remove a list of recipients from Opt Out list
To remove the list of recipients to Opt Out list, call the `remove` function from the `SmsClient.optOuts.` with a list of recipient phone numbers.

```typescript
const optOutRemoveResults = await client.optOuts.remove(
from: "<from-phone-number>", // Your E.164 formatted phone number used to send SMS
to: ["<to-phone-number-1>", "<to-phone-number-2>"], // E.164 formatted recipient phone numbers
);

for (const optOutRemoveResult of optOutRemoveResults) {
if (optOutRemoveResult.httpStatusCode == 200) {
console.log("Success: ", optOutRemoveResult);
} else {
console.error("Something went wrong when trying to send opt out remove request: ", optOutRemoveResult);
}
}
```

## Troubleshooting

SMS operations will throw an exception if the request to the server fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ These sample programs show how to use the JavaScript client libraries for Azure
| [sendSms.js][sendsms] | Send an SMS message to 1 or more recipients |
| [sendSmsWithOptions.js][sendsmswithoptions] | Configure SMS options when sending a message |
| [usingAadAuth.js][usingaadauth] | Use AAD token credentials when sending a SMS message. |
| [optOutCheck.js][optoutcheck] | Check if recipients opted out of receiving messages |
| [optOutAdd.js][optoutadd] | Opt out recipients from receiving messages |
| [optOutRemove.js][optoutremove] | Remove recipients from Opt Out list |


## Prerequisites

Expand Down Expand Up @@ -65,3 +69,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
[freesub]: https://azure.microsoft.com/free/
[createinstance_azurecommunicationservicesaccount]: https://learn.microsoft.com/azure/communication-services/quickstarts/create-communication-resource
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/communication/communication-sms/README.md
[optoutcheck]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-sms/samples/v1/javascript/optOutCheck.js
[optoutadd]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-sms/samples/v1/javascript/optOutAdd.js
[optoutremove]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/communication/communication-sms/samples/v1/javascript/optOutRemove.js

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary Opt out 1 or more recipients from receiving SMS messages
*/

const { SmsClient } = require("@azure/communication-sms");

// Load the .env file if it exists
require("dotenv").config();

async function main() {
console.log("== Opt Out Add ==");

// You will need to set this environment variable or edit the following values
const connectionString =
process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING ||
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";

// create new client
const client = new SmsClient(connectionString);

// construct send parameters
const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>";
let phoneNumbers;
if (process.env.TO_PHONE_NUMBERS !== undefined) {
phoneNumbers = process.env.TO_PHONE_NUMBERS.split(",");
} else if (process.env.AZURE_PHONE_NUMBER !== undefined) {
phoneNumbers = [process.env.AZURE_PHONE_NUMBER];
} else {
phoneNumbers = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// send add opt out request
const optOutAddResults = await client.optOuts.add(
from,
phoneNumbers);

// individual messages can encounter errors during sending
// use the "httpStatusCode" property to verify
for (const optOutAddResult of optOutAddResults) {
if (optOutAddResult.httpStatusCode == 200) {
console.log("Success: ", optOutAddResult);
} else {
console.error("Something went wrong when trying to send opt out add request: ", optOutAddResults);
}
}

console.log("== Done: Opt Out Add ==");
}

main().catch((error) => {
console.error("Encountered an error while sending opt out add request: ", error);
process.exit(1);
});

module.exports = { main };
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary Check if 1 or more recipients are opted out of receiving SMS messages
*/

const { SmsClient } = require("@azure/communication-sms");

// Load the .env file if it exists
require("dotenv").config();

async function main() {
console.log("== Opt Out Check ==");

// You will need to set this environment variable or edit the following values
const connectionString =
process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING ||
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";

// create new client
const client = new SmsClient(connectionString);

// construct send parameters
const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>";
let phoneNumbers;
if (process.env.TO_PHONE_NUMBERS !== undefined) {
phoneNumbers = process.env.TO_PHONE_NUMBERS.split(",");
} else if (process.env.AZURE_PHONE_NUMBER !== undefined) {
phoneNumbers = [process.env.AZURE_PHONE_NUMBER];
} else {
phoneNumbers = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// send check opt out request
const optOutCheckResults = await client.optOuts.check(
from,
phoneNumbers);

// individual messages can encounter errors during sending
// use the "httpStatusCode" property to verify
for (const optOutCheckResult of optOutCheckResults) {
if (optOutCheckResult.httpStatusCode == 200) {
console.log("Success: ", optOutCheckResult);
} else {
console.error("Something went wrong when trying to send opt out check request: ", optOutCheckResults);
}
}

console.log("== Done: Opt Out Check ==");
}

main().catch((error) => {
console.error("Encountered an error while sending Opt Out Check request: ", error);
process.exit(1);
});

module.exports = { main };
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

/**
* @summary Remove 1 or more recipients from Opt Out list
*/

const { SmsClient } = require("@azure/communication-sms");

// Load the .env file if it exists
require("dotenv").config();

async function main() {
console.log("== Opt Out Remove ==");

// You will need to set this environment variable or edit the following values
const connectionString =
process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING ||
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";

// create new client
const client = new SmsClient(connectionString);

// construct send parameters
const from = process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>";
let phoneNumbers;
if (process.env.TO_PHONE_NUMBERS !== undefined) {
phoneNumbers = process.env.TO_PHONE_NUMBERS.split(",");
} else if (process.env.AZURE_PHONE_NUMBER !== undefined) {
phoneNumbers = [process.env.AZURE_PHONE_NUMBER];
} else {
phoneNumbers = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// send opt out remove request
const optOutRemoveResults = await client.optOuts.remove(
from,
phoneNumbers);

// individual messages can encounter errors during sending
// use the "httpStatusCode" property to verify
for (const optOutRemoveResult of optOutRemoveResults) {
if (optOutRemoveResult.httpStatusCode == 200) {
console.log("Success: ", optOutRemoveResult);
} else {
console.error("Something went wrong when trying to send opt out remove request: ", optOutRemoveResults);
}
}

console.log("== Done: Opt Out Remove ==");
}

main().catch((error) => {
console.error("Encountered an error while sending opt out remove request: ", error);
process.exit(1);
});

module.exports = { main };
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function main() {
phoneNumbers = ["<to-phone-number-1>", "<to-phone-number-2>"];
}

// send add opt out request
// send remove opt out request
const optOutRemoveResults = await client.optOuts.remove(
from,
phoneNumbers);
Expand Down

0 comments on commit 594557a

Please sign in to comment.