-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 5 commits
bc40a99
72fda91
a1f42b9
a43bae8
6e221df
58c4556
8e835cb
76657f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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 |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); |
There was a problem hiding this comment.
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