Skip to content

Commit bb5fd9a

Browse files
[communication][sms] run sms samples in pipeline (Azure#15345)
1 parent 3437c8e commit bb5fd9a

File tree

17 files changed

+322
-186
lines changed

17 files changed

+322
-186
lines changed

sdk/communication/communication-sms/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@
130130
],
131131
"requiredResources": {
132132
"Azure Communication Services account": "https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource"
133-
},
134-
"skip": [
135-
"sendSms.js",
136-
"sendSmsWithOptions.js",
137-
"usingAadAuth.js"
138-
]
133+
}
139134
}
140135
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
# Used in most samples. Retrieve these values from a Communication Services resource
1+
# Used in most samples. Retrieve these values from a Communication Services instance
22
# in the Azure Portal.
3-
COMMUNICATION_CONNECTION_STRING="endpoint=<endpoint>;accessKey=<accessKey>"
3+
COMMUNICATION_SAMPLES_CONNECTION_STRING="endpoint=https://<resource name>.communication.azure.net/;accessKey=<key>"
4+
5+
# The endpoint to the Communication Services resource
6+
COMMUNICATION_ENDPOINT="https://<resource name>.communication.azure.net/"
7+
8+
# The client ID of an Azure Active Directory application.
9+
AZURE_CLIENT_ID="<azure-client-id>"
10+
# The client secret of an Azure Active Directory application.
11+
AZURE_CLIENT_SECRET="<azure-client-secret>"
12+
#The Tenant ID of your organization in Azure Active Directory.
13+
AZURE_TENANT_ID="<azure-tenant-id>"
14+
15+
# The phone number used to send SMS messages
16+
FROM_PHONE_NUMBER="+16135550147"
17+
18+
# Comma separated list of phone numbers to receive SMS messages
19+
TO_PHONE_NUMBERS="+16135550147,+16135550147"

sdk/communication/communication-sms/samples-dev/sendSms.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,35 @@
55
* @summary Send an SMS message to 1 or more recipients
66
*/
77

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

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

1414
export async function main() {
15+
console.log("== Send SMS Message ==");
16+
17+
// You will need to set this environment variable or edit the following values
1518
const connectionString =
16-
process.env["COMMUNICATION_CONNECTION_STRING"] ||
19+
process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING ||
1720
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
21+
22+
// create new client
1823
const client = new SmsClient(connectionString);
19-
const sendResults = await client.send({
20-
// Phone numbers must be in E.164 format
21-
from: "<from-phone-number>",
22-
to: ["<to-phone-number-1>", "<to-phone-number-2>"],
24+
25+
// construct send request
26+
const sendRequest: SmsSendRequest = {
27+
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
28+
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER!] || [
29+
"<to-phone-number-1>",
30+
"<to-phone-number-2>"
31+
],
2332
message: "Hello World via SMS!"
24-
});
33+
};
34+
35+
// send sms with request
36+
const sendResults = await client.send(sendRequest);
2537

2638
// individual messages can encounter errors during sending
2739
// use the "successful" property to verify
@@ -32,6 +44,7 @@ export async function main() {
3244
console.error("Something went wrong when trying to send this message: ", sendResult);
3345
}
3446
}
47+
3548
console.log("== Done: Send SMS Message ==");
3649
}
3750

sdk/communication/communication-sms/samples-dev/sendSmsWithOptions.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,44 @@
55
* @summary Configure SMS options when sending a message
66
*/
77

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

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

1415
export const main = async () => {
1516
console.log("== Send SMS Message With Options ==");
17+
18+
// You will need to set this environment variable or edit the following values
1619
const connectionString =
17-
process.env["COMMUNICATION_CONNECTION_STRING"] ||
20+
process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING ||
1821
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
22+
23+
// create new client
1924
const client = new SmsClient(connectionString);
20-
const sendResults = await client.send(
21-
{
22-
// Phone numbers must be in E.164 format
23-
from: "<from-phone-number>",
24-
to: ["<to-phone-number-1>", "<to-phone-number-2>"],
25-
message: "Weekly Promotion!"
26-
},
27-
{
28-
//delivery reports are delivered via EventGrid
29-
enableDeliveryReport: true,
30-
//tags are applied to the delivery report
31-
tag: "marketing"
32-
}
33-
);
25+
26+
// construct send request
27+
const sendRequest: SmsSendRequest = {
28+
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
29+
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER!] || [
30+
"<to-phone-number-1>",
31+
"<to-phone-number-2>"
32+
],
33+
message: "Hello World via SMS!"
34+
};
35+
36+
// construct send options
37+
const sendOptions: SmsSendOptions = {
38+
//delivery reports are delivered via EventGrid
39+
enableDeliveryReport: true,
40+
//tags are applied to the delivery report
41+
tag: "marketing"
42+
};
43+
44+
// send sms with request and options
45+
const sendResults = await client.send(sendRequest, sendOptions);
3446

3547
// individual messages can encounter errors during sending
3648
// use the "successful" property to verify
@@ -41,6 +53,7 @@ export const main = async () => {
4153
console.error("Something went wrong when trying to send this message: ", sendResult);
4254
}
4355
}
56+
4457
console.log("== Done: Send SMS Message With Options ==");
4558
};
4659

sdk/communication/communication-sms/samples-dev/usingAadAuth.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,27 @@
22
// Licensed under the MIT Licence.
33

44
/**
5-
* @summary (ONLY AVAILABLE IN NODE.JS RUNTIME) Use AAD token credentials when sending a SMS message.
5+
* @summary Use AAD token credentials when sending a SMS message.
66
*/
77

8-
/*
9-
ONLY AVAILABLE IN NODE.JS RUNTIME
10-
If you are using the browser, you can use the InteractiveBrowserCredential provided via @azure/identity or any other feasible implementation of TokenCredential.
11-
12-
Setup :
13-
Please ensure that your Communication Services resource is in US East, US East 2, or West Europe
14-
region. AAD Role Based Access Control is not supported in other regions yet.
15-
16-
Register a new application in AAD
17-
- See https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app
18-
to register a new application in the Azure Active Directory.
19-
- Note down the CLIENT_ID and TENANT_ID from the above step.
20-
- In the "Certificates & Secrets" tab, create a secret and note that down.
21-
- In the Azure portal, go to your Communication Services resource and click on the Access control (IAM)
22-
tab. Here, assign the "Owner" role to the registered application.
23-
24-
- Environment setup for the sample
25-
- 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.
26-
- 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).
27-
*/
28-
29-
import { SmsClient } from "@azure/communication-sms";
30-
import { DefaultAzureCredential } from "@azure/identity";
8+
import { parseConnectionString } from "@azure/communication-common";
9+
import { SmsClient, SmsSendRequest } from "@azure/communication-sms";
10+
import { isNode } from "@azure/core-http";
11+
import { ClientSecretCredential, DefaultAzureCredential, TokenCredential } from "@azure/identity";
3112

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

3617
export async function main() {
18+
console.log("== Send SMS Message With AAD Authentication ==");
19+
20+
// You will need to set this environment variable or edit the following values
3721
const endpoint =
38-
process.env["COMMUNICATION_ENDPOINT"] || "https://<resource-name>.communication.azure.com";
22+
process.env.COMMUNICATION_ENDPOINT ||
23+
parseConnectionString(process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING!).endpoint ||
24+
"https://<resource-name>.communication.azure.com";
25+
3926
// Azure AD Credential information is required to run this sample:
4027
if (
4128
!process.env.AZURE_TENANT_ID ||
@@ -48,13 +35,30 @@ export async function main() {
4835
return;
4936
}
5037

51-
const client = new SmsClient(endpoint, new DefaultAzureCredential());
52-
const sendResults = await client.send({
53-
// Phone numbers must be in E.164 format
54-
from: "<from-phone-number>",
55-
to: ["<to-phone-number-1>"],
38+
// get credentials
39+
const credential: TokenCredential = isNode
40+
? new DefaultAzureCredential()
41+
: new ClientSecretCredential(
42+
process.env.AZURE_TENANT_ID,
43+
process.env.AZURE_CLIENT_ID,
44+
process.env.AZURE_CLIENT_SECRET
45+
);
46+
47+
// create new client with endpoint and credentials
48+
const client = new SmsClient(endpoint, credential);
49+
50+
// construct send request
51+
const sendRequest: SmsSendRequest = {
52+
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
53+
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER!] || [
54+
"<to-phone-number-1>",
55+
"<to-phone-number-2>"
56+
],
5657
message: "Hello World via SMS!"
57-
});
58+
};
59+
60+
// send sms with request
61+
const sendResults = await client.send(sendRequest);
5862

5963
for (const sendResult of sendResults) {
6064
if (sendResult.successful) {

sdk/communication/communication-sms/samples/v1/javascript/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ urlFragment: communication-sms-javascript
1212

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

15-
| **File Name** | **Description** |
16-
| ------------------------------------------- | ----------------------------------------------------------------------------------------- |
17-
| [sendSms.js][sendsms] | Send an SMS message to 1 or more recipients |
18-
| [sendSmsWithOptions.js][sendsmswithoptions] | Configure SMS options when sending a message |
19-
| [usingAadAuth.js][usingaadauth] | (ONLY AVAILABLE IN NODE.JS RUNTIME) Use AAD token credentials when sending a SMS message. |
15+
| **File Name** | **Description** |
16+
| ------------------------------------------- | ----------------------------------------------------- |
17+
| [sendSms.js][sendsms] | Send an SMS message to 1 or more recipients |
18+
| [sendSmsWithOptions.js][sendsmswithoptions] | Configure SMS options when sending a message |
19+
| [usingAadAuth.js][usingaadauth] | Use AAD token credentials when sending a SMS message. |
2020

2121
## Prerequisites
2222

@@ -51,7 +51,7 @@ node sendSms.js
5151
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):
5252

5353
```bash
54-
npx cross-env COMMUNICATION_CONNECTION_STRING="<communication connection string>" node sendSms.js
54+
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
5555
```
5656

5757
## Next Steps

sdk/communication/communication-sms/samples/v1/javascript/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"dependencies": {
2828
"@azure/communication-sms": "latest",
2929
"dotenv": "latest",
30-
"@azure/identity": "^1.1.0"
30+
"@azure/communication-common": "^1.0.0",
31+
"@azure/core-http": "^1.2.0",
32+
"@azure/identity": "~1.3.0"
3133
}
3234
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1-
# Used in most samples. Retrieve these values from a Communication Services resource
1+
# Used in most samples. Retrieve these values from a Communication Services instance
22
# in the Azure Portal.
3-
COMMUNICATION_CONNECTION_STRING="endpoint=<endpoint>;accessKey=<accessKey>"
3+
COMMUNICATION_SAMPLES_CONNECTION_STRING="endpoint=https://<resource name>.communication.azure.net/;accessKey=<key>"
4+
5+
# The endpoint to the Communication Services resource
6+
COMMUNICATION_ENDPOINT="https://<resource name>.communication.azure.net/"
7+
8+
# The client ID of an Azure Active Directory application.
9+
AZURE_CLIENT_ID="<azure-client-id>"
10+
# The client secret of an Azure Active Directory application.
11+
AZURE_CLIENT_SECRET="<azure-client-secret>"
12+
#The Tenant ID of your organization in Azure Active Directory.
13+
AZURE_TENANT_ID="<azure-tenant-id>"
14+
15+
# The phone number used to send SMS messages
16+
FROM_PHONE_NUMBER="+16135550147"
17+
18+
# Comma separated list of phone numbers to receive SMS messages
19+
TO_PHONE_NUMBERS="+16135550147,+16135550147"

sdk/communication/communication-sms/samples/v1/javascript/sendSms.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,28 @@ const dotenv = require("dotenv");
1212
dotenv.config();
1313

1414
async function main() {
15+
console.log("== Send SMS Message ==");
16+
17+
// You will need to set this environment variable or edit the following values
1518
const connectionString =
16-
process.env["COMMUNICATION_CONNECTION_STRING"] ||
19+
process.env.COMMUNICATION_SAMPLES_CONNECTION_STRING ||
1720
"endpoint=https://<resource-name>.communication.azure.com/;<access-key>";
21+
22+
// create new client
1823
const client = new SmsClient(connectionString);
19-
const sendResults = await client.send({
20-
// Phone numbers must be in E.164 format
21-
from: "<from-phone-number>",
22-
to: ["<to-phone-number-1>", "<to-phone-number-2>"],
24+
25+
// construct send request
26+
const sendRequest = {
27+
from: process.env.FROM_PHONE_NUMBER || process.env.AZURE_PHONE_NUMBER || "<from-phone-number>",
28+
to: process.env.TO_PHONE_NUMBERS?.split(",") || [process.env.AZURE_PHONE_NUMBER] || [
29+
"<to-phone-number-1>",
30+
"<to-phone-number-2>"
31+
],
2332
message: "Hello World via SMS!"
24-
});
33+
};
34+
35+
// send sms with request
36+
const sendResults = await client.send(sendRequest);
2537

2638
// individual messages can encounter errors during sending
2739
// use the "successful" property to verify
@@ -32,6 +44,7 @@ async function main() {
3244
console.error("Something went wrong when trying to send this message: ", sendResult);
3345
}
3446
}
47+
3548
console.log("== Done: Send SMS Message ==");
3649
}
3750

0 commit comments

Comments
 (0)