Skip to content

Commit

Permalink
[communication][sms] run sms samples in pipeline (#15345)
Browse files Browse the repository at this point in the history
  • Loading branch information
0rland0Wats0n authored May 21, 2021
1 parent 3437c8e commit bb5fd9a
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 186 deletions.
7 changes: 1 addition & 6 deletions sdk/communication/communication-sms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@
],
"requiredResources": {
"Azure Communication Services account": "https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource"
},
"skip": [
"sendSms.js",
"sendSmsWithOptions.js",
"usingAadAuth.js"
]
}
}
}
20 changes: 18 additions & 2 deletions sdk/communication/communication-sms/sample.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Used in most samples. Retrieve these values from a Communication Services resource
# Used in most samples. Retrieve these values from a Communication Services instance
# in the Azure Portal.
COMMUNICATION_CONNECTION_STRING="endpoint=<endpoint>;accessKey=<accessKey>"
COMMUNICATION_SAMPLES_CONNECTION_STRING="endpoint=https://<resource name>.communication.azure.net/;accessKey=<key>"

# The endpoint to the Communication Services resource
COMMUNICATION_ENDPOINT="https://<resource name>.communication.azure.net/"

# The client ID of an Azure Active Directory application.
AZURE_CLIENT_ID="<azure-client-id>"
# The client secret of an Azure Active Directory application.
AZURE_CLIENT_SECRET="<azure-client-secret>"
#The Tenant ID of your organization in Azure Active Directory.
AZURE_TENANT_ID="<azure-tenant-id>"

# The phone number used to send SMS messages
FROM_PHONE_NUMBER="+16135550147"

# Comma separated list of phone numbers to receive SMS messages
TO_PHONE_NUMBERS="+16135550147,+16135550147"
27 changes: 20 additions & 7 deletions sdk/communication/communication-sms/samples-dev/sendSms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
* @summary Send an SMS message to 1 or more recipients
*/

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

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

export async function main() {
console.log("== Send SMS Message ==");

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

// create new client
const client = new SmsClient(connectionString);
const sendResults = await client.send({
// Phone numbers must be in E.164 format
from: "<from-phone-number>",
to: ["<to-phone-number-1>", "<to-phone-number-2>"],

// construct send request
const sendRequest: SmsSendRequest = {
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER!] || [
"<to-phone-number-1>",
"<to-phone-number-2>"
],
message: "Hello World via SMS!"
});
};

// send sms with request
const sendResults = await client.send(sendRequest);

// individual messages can encounter errors during sending
// use the "successful" property to verify
Expand All @@ -32,6 +44,7 @@ export async function main() {
console.error("Something went wrong when trying to send this message: ", sendResult);
}
}

console.log("== Done: Send SMS Message ==");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,44 @@
* @summary Configure SMS options when sending a message
*/

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

// Load the .env file if it exists
import * as dotenv from "dotenv";
import { SmsSendOptions } from "../src/generated/src/models";
dotenv.config();

export const main = async () => {
console.log("== Send SMS Message With Options ==");

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

// create new client
const client = new SmsClient(connectionString);
const sendResults = await client.send(
{
// Phone numbers must be in E.164 format
from: "<from-phone-number>",
to: ["<to-phone-number-1>", "<to-phone-number-2>"],
message: "Weekly Promotion!"
},
{
//delivery reports are delivered via EventGrid
enableDeliveryReport: true,
//tags are applied to the delivery report
tag: "marketing"
}
);

// construct send request
const sendRequest: SmsSendRequest = {
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER!] || [
"<to-phone-number-1>",
"<to-phone-number-2>"
],
message: "Hello World via SMS!"
};

// construct send options
const sendOptions: SmsSendOptions = {
//delivery reports are delivered via EventGrid
enableDeliveryReport: true,
//tags are applied to the delivery report
tag: "marketing"
};

// send sms with request and options
const sendResults = await client.send(sendRequest, sendOptions);

// individual messages can encounter errors during sending
// use the "successful" property to verify
Expand All @@ -41,6 +53,7 @@ export const main = async () => {
console.error("Something went wrong when trying to send this message: ", sendResult);
}
}

console.log("== Done: Send SMS Message With Options ==");
};

Expand Down
66 changes: 35 additions & 31 deletions sdk/communication/communication-sms/samples-dev/usingAadAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,27 @@
// Licensed under the MIT Licence.

/**
* @summary (ONLY AVAILABLE IN NODE.JS RUNTIME) Use AAD token credentials when sending a SMS message.
* @summary Use AAD token credentials when sending a SMS message.
*/

/*
ONLY AVAILABLE IN NODE.JS RUNTIME
If you are using the browser, you can use the InteractiveBrowserCredential provided via @azure/identity or any other feasible implementation of TokenCredential.
Setup :
Please ensure that your Communication Services resource is in US East, US East 2, or West Europe
region. AAD Role Based Access Control is not supported in other regions yet.
Register a new application in AAD
- See https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app
to register a new application in the Azure Active Directory.
- Note down the CLIENT_ID and TENANT_ID from the above step.
- In the "Certificates & Secrets" tab, create a secret and note that down.
- In the Azure portal, go to your Communication Services resource and click on the Access control (IAM)
tab. Here, assign the "Owner" role to the registered application.
- Environment setup for the sample
- From the overview page of your AAD Application, note down the `CLIENT ID` and `TENANT ID`. In the "Certificates & Secrets" tab, create a secret and note that down.
- Make sure you have AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET as environment variables to successfully execute the sample (Can leverage process.env).
*/

import { SmsClient } from "@azure/communication-sms";
import { DefaultAzureCredential } from "@azure/identity";
import { parseConnectionString } from "@azure/communication-common";
import { SmsClient, SmsSendRequest } from "@azure/communication-sms";
import { isNode } from "@azure/core-http";
import { ClientSecretCredential, DefaultAzureCredential, TokenCredential } from "@azure/identity";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

export async function main() {
console.log("== Send SMS Message With AAD Authentication ==");

// You will need to set this environment variable or edit the following values
const endpoint =
process.env["COMMUNICATION_ENDPOINT"] || "https://<resource-name>.communication.azure.com";
process.env.COMMUNICATION_ENDPOINT ||
parseConnectionString(process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING!).endpoint ||
"https://<resource-name>.communication.azure.com";

// Azure AD Credential information is required to run this sample:
if (
!process.env.AZURE_TENANT_ID ||
Expand All @@ -48,13 +35,30 @@ export async function main() {
return;
}

const client = new SmsClient(endpoint, new DefaultAzureCredential());
const sendResults = await client.send({
// Phone numbers must be in E.164 format
from: "<from-phone-number>",
to: ["<to-phone-number-1>"],
// get credentials
const credential: TokenCredential = isNode
? new DefaultAzureCredential()
: new ClientSecretCredential(
process.env.AZURE_TENANT_ID,
process.env.AZURE_CLIENT_ID,
process.env.AZURE_CLIENT_SECRET
);

// create new client with endpoint and credentials
const client = new SmsClient(endpoint, credential);

// construct send request
const sendRequest: SmsSendRequest = {
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER!] || [
"<to-phone-number-1>",
"<to-phone-number-2>"
],
message: "Hello World via SMS!"
});
};

// send sms with request
const sendResults = await client.send(sendRequest);

for (const sendResult of sendResults) {
if (sendResult.successful) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ urlFragment: communication-sms-javascript

These sample programs show how to use the JavaScript client libraries for Azure Communication Services - SMS in some common scenarios.

| **File Name** | **Description** |
| ------------------------------------------- | ----------------------------------------------------------------------------------------- |
| [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] | (ONLY AVAILABLE IN NODE.JS RUNTIME) Use AAD token credentials when sending a SMS message. |
| **File Name** | **Description** |
| ------------------------------------------- | ----------------------------------------------------- |
| [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. |

## Prerequisites

Expand Down Expand Up @@ -51,7 +51,7 @@ node sendSms.js
Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):

```bash
npx cross-env COMMUNICATION_CONNECTION_STRING="<communication connection string>" node sendSms.js
npx cross-env COMMUNICATION_SAMPLES_CONNECTION_STRING="<communication samples connection string>" FROM_PHONE_NUMBER="<from phone number>" AZURE_PHONE_NUMBER="<azure phone number>" TO_PHONE_NUMBERS="<to phone numbers>" AZURE_PHONE_NUMBER="<azure phone number>" node sendSms.js
```

## Next Steps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"dependencies": {
"@azure/communication-sms": "latest",
"dotenv": "latest",
"@azure/identity": "^1.1.0"
"@azure/communication-common": "^1.0.0",
"@azure/core-http": "^1.2.0",
"@azure/identity": "~1.3.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Used in most samples. Retrieve these values from a Communication Services resource
# Used in most samples. Retrieve these values from a Communication Services instance
# in the Azure Portal.
COMMUNICATION_CONNECTION_STRING="endpoint=<endpoint>;accessKey=<accessKey>"
COMMUNICATION_SAMPLES_CONNECTION_STRING="endpoint=https://<resource name>.communication.azure.net/;accessKey=<key>"

# The endpoint to the Communication Services resource
COMMUNICATION_ENDPOINT="https://<resource name>.communication.azure.net/"

# The client ID of an Azure Active Directory application.
AZURE_CLIENT_ID="<azure-client-id>"
# The client secret of an Azure Active Directory application.
AZURE_CLIENT_SECRET="<azure-client-secret>"
#The Tenant ID of your organization in Azure Active Directory.
AZURE_TENANT_ID="<azure-tenant-id>"

# The phone number used to send SMS messages
FROM_PHONE_NUMBER="+16135550147"

# Comma separated list of phone numbers to receive SMS messages
TO_PHONE_NUMBERS="+16135550147,+16135550147"
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@ const dotenv = require("dotenv");
dotenv.config();

async function main() {
console.log("== Send SMS Message ==");

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

// create new client
const client = new SmsClient(connectionString);
const sendResults = await client.send({
// Phone numbers must be in E.164 format
from: "<from-phone-number>",
to: ["<to-phone-number-1>", "<to-phone-number-2>"],

// construct send request
const sendRequest = {
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER] || [
"<to-phone-number-1>",
"<to-phone-number-2>"
],
message: "Hello World via SMS!"
});
};

// send sms with request
const sendResults = await client.send(sendRequest);

// individual messages can encounter errors during sending
// use the "successful" property to verify
Expand All @@ -32,6 +44,7 @@ async function main() {
console.error("Something went wrong when trying to send this message: ", sendResult);
}
}

console.log("== Done: Send SMS Message ==");
}

Expand Down
Loading

0 comments on commit bb5fd9a

Please sign in to comment.