Skip to content

Commit f96a07b

Browse files
committed
acrolinx fixes
1 parent b4c8748 commit f96a07b

File tree

5 files changed

+112
-66
lines changed

5 files changed

+112
-66
lines changed

articles/communication-services/tutorials/includes/url-shortener-csharp.md

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,30 @@ ms.topic: include
1010
ms.service: azure-communication-services
1111
---
1212

13-
## Pre-requisites
13+
## Sample Code
14+
15+
You can find the completed code for this tutorial on [GitHub](https://github.com/Azure-Samples/communication-services-dotnet-quickstarts/tree/main/sms-url-shortener).
16+
17+
## Prerequisites
1418

1519
- An active Azure subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
1620
- An active Azure Communication Services resource. For more information, see [Create an Azure Communication Services resource](../../quickstarts/create-communication-resource).
17-
- An Azure Communication Services phone number. [Get a phone number](../../quickstarts/telephony/get-phone-number). You will need to [verify your phone number](../../quickstarts/sms/apply-for-toll-free-verification) so it can send messages with URLs.
21+
- An Azure Communication Services phone number. [Get a phone number](../../quickstarts/telephony/get-phone-number). You need to [verify your phone number](../../quickstarts/sms/apply-for-toll-free-verification) so it can send messages with URLs.
1822
- Deployed [AzUrlShortener](https://github.com/microsoft/AzUrlShortener). Click [Deploy to Azure](https://github.com/microsoft/AzUrlShortener/wiki/How-to-deploy-your-AzUrlShortener) button for quick deploy.
1923
- [*Optional*] Deploy the [Admin web app](https://github.com/microsoft/AzUrlShortener/blob/main/src/Cloud5mins.ShortenerTools.TinyBlazorAdmin/README.md) to manage and monitor links in UI.
20-
- For this tutorial, we will be leveraging an Azure Function serve as an endpoint we can call to request SMS to be sent with a shortened URL. You could always use an existing service, different framework like express or just run this as a Node.JS console app. To follow this instructions to set up an [Azure Function for C#](https://learn.microsoft.com/azure/azure-functions/create-first-function-vs-code-csharp).
24+
- For this tutorial, SMS requests are routed to an Azure Function. You could always use an existing service, different framework like express or just run the tutorial as a C# console app. To follow this tutorial with an Azure Function, see instructions to set it up: [Azure Function for C#](https://learn.microsoft.com/azure/azure-functions/create-first-function-vs-code-csharp).
2125

2226
## Architecture overview
2327

24-
In this tutorial our focus will be in setting up a middleware that orchestrates requests to send SMS and the shortening of URLs through the Azure URL Shortener service. It will interact with Azure Communication Services to complete the sending of the SMS.
28+
In this tutorial, the focus is to set up a middleware that orchestrates requests to send SMS and the shortening of URLs through the Azure URL Shortener service. It interacts with Azure Communication Services to complete the sending of the SMS.
2529

26-
![Diagram for architecture overview](../media/url-shortener/url-shortener-architecture.png)
30+
![Diagram for architecture overview](../media/url-shortener/url-shortener-architecture.svg)
2731

2832
## Set up the Azure Function
2933

30-
To get started, you will need to create a new Azure Function. You can do this by following the steps in the [Azure Functions documentation](https://learn.microsoft.com/azure/azure-functions/create-first-function-vs-code-typescript). If you are not using an Azure Function and instead are using a different framework, skip this step and continue to the next section.
34+
To get started, you need to create a new Azure Function. You can can create the Azure Function by following the steps in the [Azure Functions documentation](https://learn.microsoft.com/azure/azure-functions/create-first-function-vs-code-csharp). If you aren't using an Azure Function and instead are using a different framework, skip this step and continue to the next section.
3135

32-
Once the Azure Function is setup, go to the `local.settings.json` file and add three additional values which we will use to store the Azure Communication Services connection string, phone number and URL Shortener endpoint. This are all values you generated from the pre-requisites above.
36+
Once the Azure Function is set up, go to the `local.settings.json` file and add three more values that you need to store: the Azure Communication Services connection string, phone number (Ex. +15555555555) and URL Shortener endpoint (Ex. https://<Azure Function URL>/api/UrlCreate). These variables are all values you generated from the prerequisites at the beginning of the document.
3337

3438
```json
3539

@@ -39,16 +43,16 @@ Once the Azure Function is setup, go to the `local.settings.json` file and add t
3943
"AzureWebJobsStorage": "",
4044
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
4145
"ACS_CONNECTIONSTRING": "<ACS CONNECTION STRING>",
42-
"ACS_PHONE_NUMBER": "<ACS PHONE NUMBER>", // Ex. +15555555555
43-
"URL_SHORTENER": "<URL SHORTENER ENDPOINT>" // Ex. https://<Azure Function URL>/api/UrlCreate
46+
"ACS_PHONE_NUMBER": "<ACS PHONE NUMBER>",
47+
"URL_SHORTENER": "<URL SHORTENER ENDPOINT>"
4448
}
4549
}
4650

4751
```
4852

4953
## Configure query parameters
5054

51-
Now that we have created our function, we will not configure the query parameters we will use to trigger it. The function will expect a phone number and a URL. The phone number is used as the recipient of the SMS message. The URL is the link we want to shorten and send to the recipient.
55+
Now that you've created the Azure Function, you need to configure the query parameters needed to trigger it. The function expects a phone number and a URL. The phone number is used as the recipient of the SMS message. The URL is the link that is shortened and sent to the recipient.
5256

5357
```csharp
5458

@@ -66,7 +70,7 @@ namespace Company.Function
6670
string phoneNumberTo = req.Query["phoneNumber"]; // get phone number query parameter
6771
string urlToShorten = req.Query["url"]; // get url to shorten query parameter
6872
69-
73+
return new OkObjectResult(null);
7074
}
7175
}
7276
}
@@ -75,9 +79,13 @@ namespace Company.Function
7579

7680
## Shorten the URL
7781

78-
Now that we have the phone number and URL, we can use the Azure URL Shortener service to shorten the URL. Ensure you have [deployed](https://github.com/microsoft/AzUrlShortener/wiki/How-to-deploy-your-AzUrlShortener) this service already. The service contains several endpoints, but for this tutorial we will focus on the `UrlCreate` endpoint. We will use the `PostAsync` method to place a `POST` request to the Azure URL Shortener service with the URL we want to shorten. The service will return a JSON object with the shortened URL. We will store this in a variable called `shortUrl`. In the snippet below, insert the endpoint of your deployed Azure URL Shortener service. For information on how to to get the endpoint, see [Validate the deployment](https://github.com/microsoft/AzUrlShortener/wiki/How-to-deploy-your-AzUrlShortener#validate-the-deployment).
82+
Now that you have the phone number and URL, you can use the Azure URL Shortener service to shorten the URL. Ensure you have [deployed](https://github.com/microsoft/AzUrlShortener/wiki/How-to-deploy-your-AzUrlShortener) this service already. The service contains several endpoints, but for this tutorial the focus is on the `UrlCreate` endpoint. Use the `PostAsync` method to place a `POST` request to the Azure URL Shortener service with the URL you want to shorten. The service returns a JSON object with the shortened URL. Store the shortened URL in a variable called `shortUrl`. In the snippet, insert the endpoint of your deployed Azure URL Shortener service. For information on how to to get the endpoint, see [Validate the deployment](https://github.com/microsoft/AzUrlShortener/wiki/How-to-deploy-your-AzUrlShortener#validate-the-deployment).
7983

8084
```csharp
85+
...
86+
using System.Net.Http;
87+
using System.Text.Json;
88+
using System.Text;
8189

8290
namespace Company.Function
8391
{
@@ -97,9 +105,11 @@ namespace Company.Function
97105
{
98106
log.LogInformation("C# HTTP trigger function processed a request.");
99107

108+
//Parse Query Parameters
100109
string phoneNumberTo = req.Query["phoneNumber"]; // get phone number query parameter
101110
string urlToShorten = req.Query["url"]; // get url to shorten query parameter
102-
111+
112+
//Get short URL from Azure URL Shortener
103113
using var client = new HttpClient();
104114
var requestData = new
105115
{
@@ -112,6 +122,8 @@ namespace Company.Function
112122
var data = System.Text.Json.JsonSerializer.Deserialize<ShortenedUrl>(content); // Parse content to ShortenedUrl object
113123
var url = data.ShortUrl;
114124
log.LogInformation("Shortened URL " + url);
125+
126+
return new OkObjectResult(null);
115127
}
116128
}
117129
}
@@ -120,51 +132,69 @@ namespace Company.Function
120132

121133
## Send SMS
122134

123-
Now that we have the shortened URL, we can use Azure Communication Services to send the SMS. We will use the `send` method from the `SmsClient` class from the `Azure.Communication.Sms` package. This method will send the SMS to the phone number we provided in the query parameters. The SMS will contain the shortened URL. For more information on how to send SMS, see [Send SMS](https://learn.microsoft.com/azure/communication-services/quickstarts/sms/send?pivots=programming-language-csharp).
135+
Now that the URL is shortened, you can use Azure Communication Services to send the SMS. Use the `send` method from the `SmsClient` class from the `Azure.Communication.Sms` package.
136+
137+
```bash
138+
139+
dotnet add package Azure.Communication.Sms
140+
141+
```
142+
143+
This method sends the SMS to the phone number provided in the query parameters. The SMS contains the shortened URL. For more information on how to send SMS, see [Send SMS](https://learn.microsoft.com/azure/communication-services/quickstarts/sms/send?pivots=programming-language-csharp).
124144

125145
```csharp
146+
...
147+
using Azure;
148+
using Azure.Communication;
149+
using Azure.Communication.Sms;
126150

127151
namespace Company.Function
128152
{
153+
// Class to deserialize URL Shortener response
129154
public class ShortenedUrl
130155
{
131156
public string ShortUrl { get; set; }
132157
}
133158

134159
public static class SendSMSUrlShortener
135160
{
136-
static string connectionString = Environment.GetEnvironmentVariable("ACS_CONNECTIONSTRING", EnvironmentVariableTarget.Process);
137-
static string phoneNumberFrom = Environment.GetEnvironmentVariable("ACS_PHONE_NUMBER", EnvironmentVariableTarget.Process); // Ex. +15555555555
138-
static string urlShortener = Environment.GetEnvironmentVariable("URL_SHORTENER", EnvironmentVariableTarget.Process); // Ex. https://<Azure Function URL>/api/UrlCreate
139-
140161
[FunctionName("SendSMSUrlShortener")]
141162
public static async Task<IActionResult> Run(
142163
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
143164
ILogger log)
144165
{
145166
log.LogInformation("C# HTTP trigger function processed a request.");
146-
167+
168+
//Parse Query Parameters
147169
string phoneNumberTo = req.Query["phoneNumber"]; // get phone number query parameter
148170
string urlToShorten = req.Query["url"]; // get url to shorten query parameter
149-
171+
172+
//Get short URL from Azure URL Shortener
150173
using var client = new HttpClient();
151174
var requestData = new
152175
{
153176
Url = urlToShorten //Body request for the POST call
154177
};
155178
var requestBody = JsonSerializer.Serialize(requestData);
156179
var httpContent = new StringContent(requestBody, Encoding.UTF8, "application/json");
180+
string urlShortener = Environment.GetEnvironmentVariable("URL_SHORTENER", EnvironmentVariableTarget.Process); // Ex. https://<Azure Function URL>/api/UrlCreate
157181
var response = await client.PostAsync(urlShortener, httpContent); // POST call to the Azure URL Shortener
158182
var content = await response.Content.ReadAsStringAsync();
159183
var data = System.Text.Json.JsonSerializer.Deserialize<ShortenedUrl>(content); // Parse content to ShortenedUrl object
160184
var url = data.ShortUrl;
161185
log.LogInformation("Shortened URL " + url);
162-
186+
187+
//Send SMS with Azure Communication Services
188+
string connectionString = Environment.GetEnvironmentVariable("ACS_CONNECTIONSTRING", EnvironmentVariableTarget.Process);
189+
string phoneNumberFrom = Environment.GetEnvironmentVariable("ACS_PHONE_NUMBER", EnvironmentVariableTarget.Process); // Ex. +15555555555
190+
SmsClient smsClient = new SmsClient(connectionString);
163191
SmsSendResult sendResult = smsClient.Send(
164192
from: phoneNumberFrom,
165193
to: phoneNumberTo,
166194
message: "Here is your shortened URL: " + url
167195
); // Send SMS message with shortened URL
196+
197+
return new OkObjectResult(sendResult);
168198
}
169199
}
170200
}
@@ -174,7 +204,7 @@ namespace Company.Function
174204
## Test locally
175205

176206
>[!NOTE]
177-
> You will need to [verify your phone number](https://learn.microsoft.com/azure/communication-services/quickstarts/sms/apply-for-toll-free-verification) to send SMS messages with URLs. Once you have submitted your verification application, it might take a couple days for the phone number to be enabled to send URLs before it gets full verified (full verification takes 5-6 weeks). For more information on toll-free number verification, see [Apply for toll-free verification](https://learn.microsoft.com/azure/communication-services/quickstarts/sms/apply-for-toll-free-verification).
207+
> You need to [verify your phone number](https://learn.microsoft.com/azure/communication-services/quickstarts/sms/apply-for-toll-free-verification) to send SMS messages with URLs. Once you have submitted your verification application, it might take a couple days for the phone number to be enabled to send URLs before it gets full verified (full verification takes 5-6 weeks). For more information on toll-free number verification, see [Apply for toll-free verification](https://learn.microsoft.com/azure/communication-services/quickstarts/sms/apply-for-toll-free-verification).
178208
179209
You can now run your Azure Function locally by pressing `F5` in Visual Studio Code or by running the following command in the terminal:
180210

@@ -184,10 +214,10 @@ func host start
184214

185215
```
186216

187-
Then using a tool like [Postman](https://www.postman.com/), you can test your function by making a `POST` request to the endpoint of your Azure Function. You will need to provide the phone number and URL as query parameters. For example, if your Azure Function is running locally, you can make a request to `http://localhost:7071/api/<FUNCTION NAME>?phoneNumber=%2B15555555555&url=https://www.microsoft.com`. You should receive a response with the shortened URL and a status of `Success`.
217+
Then using a tool like [Postman](https://www.postman.com/), you can test your function by making a `POST` request to the endpoint of your Azure Function. You need to provide the phone number and URL as query parameters. For example, if your Azure Function is running locally, you can make a request to `http://localhost:7071/api/<FUNCTION NAME>?phoneNumber=%2B15555555555&url=https://www.microsoft.com`. You should receive a response with the shortened URL and a status of `Success`.
188218

189219
## Deploy to Azure
190220

191221
To deploy your Azure Function, you can follow [step by step instructions](https://learn.microsoft.com/azure/azure-functions/create-first-function-vs-code-csharp?pivots=programming-language-dotnet#sign-in-to-azure).
192222

193-
Once deployed, you can access the function through a similar method as you did when testing locally. You will need to provide the phone number and URL as query parameters. For example, if your Azure Function is deployed to Azure, you can make a request to `https://<YOUR AZURE FUNCTION NAME>.azurewebsites.net/api/<FUNCTION NAME>?phoneNumber=%2B15555555555&url=https://www.microsoft.com`. You should receive a response with the shortened URL and a status of `Success`.
223+
Once deployed, you can access the function through a similar method as you did when testing locally. You need to provide the phone number and URL as query parameters. For example, if your Azure Function is deployed to Azure, you can make a request to `https://<YOUR AZURE FUNCTION NAME>.azurewebsites.net/api/<FUNCTION NAME>?phoneNumber=%2B15555555555&url=https://www.microsoft.com`. You should receive a response with the shortened URL and a status of `Success`.

0 commit comments

Comments
 (0)