Skip to content

Latest commit

 

History

History
156 lines (100 loc) · 7.03 KB

File metadata and controls

156 lines (100 loc) · 7.03 KB
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
include file
sfi-ropc-nochange

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.

Prerequisites

Prerequisite check

  • In a terminal or command window, run az --version to check that Azure CLI is installed.

Setting up

Install the communication module

To install the communication module, run the following command in a terminal or command window.

az extension add --name communication

Sign in to Azure CLI

You need to sign in to Azure CLI. You can sign in running the az login command from the terminal and providing your credentials.

Make sure you're using the correct subscription

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.

(Optional) Use Azure CLI sms operations without passing in a connection string

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.


Operations

Send a 1:1 SMS message

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.

Send a 1:N SMS message

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.

Send an SMS message with DeliveryReport and Tag

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.