Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Communication] SMS 1:N Messages - Update Samples #14125

Merged
merged 8 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 52 additions & 14 deletions sdk/communication/communication-sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<Host>;accessKey=<Base64-Encoded-Key>`;
const connectionString = `endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>`;
const client = new SmsClient(connectionString);
```

Expand All @@ -49,8 +49,9 @@ const client = new SmsClient(connectionString);
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("<Host>", credential);
const client = new SmsClient(endpoint, credential);
```

### Using Azure Active Directory managed identity
Expand All @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not really the case. Environment variables are only one way of how to use the DefaultAzureCredential


```typescript
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);
```

## 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("<Base64-Encoded-Key>");
const client = new SmsClient("<Host>", 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: "<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
},
{
enableDeliveryReport: true,
tag: "TypeScriptSMSSample"
}
);

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: "<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);
}
```

## Next steps

Please take a look at the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"sideEffects": false,
"dependencies": {
"@azure/communication-sms": "latest",
"@azure/identity": "^1.1.0",
"dotenv": "^8.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -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://<resource name>.communication.azure.net/;accessKey=<key>"
COMMUNICATION_CONNECTION_STRING="endpoint=https://<resource name>.communication.azure.com/;accessKey=<key>"

# For token authentication
COMMUNICATION_ENDPOINT="https://<resource name>.communication.azure.com",
AZURE_CLIENT_ID: "YourClientId",
AZURE_TENANT_ID: "YourTenantId",
AZURE_CLIENT_SECRET: "YourClientSecret"
70 changes: 57 additions & 13 deletions sdk/communication/communication-sms/samples/javascript/sendSms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"] || "<communication service 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://<resource-name>.communication.azure.com/;<access-key>";
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://<resource-name>.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: "<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
});

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: "<phone number>", // Your E.164 formatted phone number used to send SMS
to: ["<phone number>", "<phone number>", "<phone number>"], // The list of E.164 formatted phone numbers to which message is being 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
},
{
enableDeliveryReport: true,
tag: "customTag"
tag: "JavaScriptSMSSample"
}
);

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: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"sideEffects": false,
"dependencies": {
"@azure/communication-sms": "latest",
"@azure/identity": "^1.1.0",
"dotenv": "^8.2.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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://<resource name>.communication.azure.net/;accessKey=<key>"
COMMUNICATION_CONNECTION_STRING="endpoint=https://<resource name>.communication.azure.com/;accessKey=<key>"

# For token authentication
COMMUNICATION_ENDPOINT="https://<resource name>.communication.azure.com",
AZURE_CLIENT_ID: "YourClientId",
AZURE_TENANT_ID: "YourTenantId",
AZURE_CLIENT_SECRET: "YourClientSecret"
Original file line number Diff line number Diff line change
Expand Up @@ -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"] || "<communication service 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://<resource-name>.communication.azure.com/;<access-key>";
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://<resource-name>.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: "<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
});

// 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: "<phone number>", // Your E.164 formatted phone number used to send SMS
to: ["<phone number>", "<phone number>", "<phone number>"], // The list of E.164 formatted phone numbers to which message is being 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
},
{
enableDeliveryReport: true,
tag: "customTag"
tag: "TypeScriptSMSSample"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C#, the message and tag are related so that users can get an idea of how to use the tag. (e.g. Message is "Weekly Promotion", and tag is "marketing". Wondering if we should do something here as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do, @lsundaralingam can you do that for python too then?

}
);

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);
});