| title | include file | ||
|---|---|---|---|
| description | include file | ||
| services | azure-communication-services | ||
| author | mayssamm | ||
| manager | ankita | ||
| ms.service | azure-communication-services | ||
| ms.subservice | azure-communication-services | ||
| ms.date | 06/12/2022 | ||
| ms.topic | include | ||
| ms.author | mayssamm | ||
| ms.custom |
|
Get started with Azure Communication Services by using the Communication module in Azure CLI to send SMS messages.
Completing this article incurs a small cost of a few USD cents or less in your Azure account.
- An Azure account with an active subscription. Create an account for free.
- An active Communication Services resource and connection string. Create a Communication Services resource.
- An SMS-enabled telephone number, short code, or alphanumeric sender ID. Get a phone number.
- The latest Azure CLI version for your operating system.
- In a terminal or command window, run
az --versionto check that Azure CLI is installed.
To install the communication module, run the following command in a terminal or command window.
az extension add --name communication
You need to sign in to Azure CLI. You can sign in running the az login command from the terminal and providing your credentials.
If you have multiple subscriptions in your account, make sure that you're using the correct one for this tutorial.
In a terminal or command windows, run the following command to check the current subscription.
az account show
If you need to change subscription, you can do that by running the following command.
az account set --subscription "<yourSubscriptionId>"
You need to replace <yourSubscriptionId> with your actual subscription ID, which you can find in the Subscriptions section in Azure portal.
You can configure the AZURE_COMMUNICATION_CONNECTION_STRING environment variable to use Azure CLI sms operations without having to use --connection_string to pass in the connection string. To configure an environment variable, open a console window and select your operating system from the below tabs. Replace <yourConnectionString> with your actual connection string.
Open a console window and enter the following command:
setx AZURE_COMMUNICATION_CONNECTION_STRING "<yourConnectionString>"After you add the environment variable, you may need to restart any running programs that will need to read the environment variable, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example.
Edit your .zshrc, and add the environment variable:
export AZURE_COMMUNICATION_CONNECTION_STRING="<yourConnectionString>"After you add the environment variable, run source ~/.zshrc from your console window to make the changes effective. If you created the environment variable with your IDE open, you may need to close and reopen the editor, IDE, or shell in order to access the variable.
Edit your .bash_profile, and add the environment variable:
export AZURE_COMMUNICATION_CONNECTION_STRING="<yourConnectionString>"After you add the environment variable, run source ~/.bash_profile from your console window to make the changes effective. If you created the environment variable with your IDE open, you may need to close and reopen the editor, IDE, or shell in order to access the variable.
To send an SMS message to a single recipient, call the send method from the sms module with a single recipient phone number.
az communication sms send --sender "<fromPhoneNumber>" --recipient "<toPhoneNumber>" --message "Hello world via SMS for Azure CLI!" --connection-string "<yourConnectionString>"
Make these replacements in the code:
- Replace
<fromPhoneNumber>with an SMS-enabled phone number associated with your Communication Services resource. - Replace
<toPhoneNumber>with a phone number that you'd like to send a message to. - Replace
<yourConnectionString>with your connection string.
Warning
Provide phone numbers in E.164 international standard format, for example, +14255550123. The value for <fromPhoneNumber> can also be a short code, for example, 23456 or an alphanumeric sender ID, for example, CONTOSO.
To send an SMS message to a list of recipients, call the send method from the sms module with multiple recipient phone numbers.
az communication sms send --sender "<fromPhoneNumber>" --recipient "<toPhoneNumber1>" "<toPhoneNumber2>" "<toPhoneNumber3>" --message "Hello world via SMS for Azure CLI!" --connection-string "<yourConnectionString>"
Make these replacements in the code:
- Replace
<fromPhoneNumber>with an SMS-enabled phone number associated with your Communication Services resource. - Replace
<toPhoneNumberN>with the N'th phone number that you'd like to send a message to. - Replace
<yourConnectionString>with your connection string.
Warning
Provide phone numbers in E.164 international standard format, such as +14255550123. The value for <fromPhoneNumber> can also be a short code, such as 23456 or an alphanumeric sender ID, such as CONTOSO.
az communication sms send --sender "<fromPhoneNumber>" --recipient "<toPhoneNumber>" --message "Hello world via SMS for Azure CLI!" --connection-string "<yourConnectionString>" --deliveryReport --tag "<yourCustomTag>"
The deliveryReport parameter is an optional parameter you can use to configure delivery reporting. Use this function if you want to emit events when SMS messages are delivered. See Handle SMS Events to configure delivery reporting for your SMS messages.
The tag parameter is an optional parameter that you can use to apply a tag to the delivery report.
Make these replacements in the code:
- Replace
<fromPhoneNumber>with an SMS-enabled phone number associated with your Communication Services resource. - Replace
<toPhoneNumber>with a phone number that you'd like to send a message to. - Replace
<yourConnectionString>with your connection string. - Replace
<yourCustomTag>with your custom tag.
Warning
Provide phone numbers in E.164 international standard format, for example, +14255550123. The value for <fromPhoneNumber> can also be a short code, for example, 23456 or an alphanumeric sender ID, for example, CONTOSO.