You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/communication-services/tutorials/includes/url-shortener-csharp.md
+54-24Lines changed: 54 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,26 +10,30 @@ ms.topic: include
10
10
ms.service: azure-communication-services
11
11
---
12
12
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
14
18
15
19
- 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).
16
20
- 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.
18
22
- 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.
19
23
- [*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).
21
25
22
26
## Architecture overview
23
27
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.
25
29
26
-

30
+

27
31
28
32
## Set up the Azure Function
29
33
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.
31
35
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://<AzureFunctionURL>/api/UrlCreate). These variables are all values you generated from the prerequisites at the beginning of the document.
33
37
34
38
```json
35
39
@@ -39,16 +43,16 @@ Once the Azure Function is setup, go to the `local.settings.json` file and add t
"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>"
44
48
}
45
49
}
46
50
47
51
```
48
52
49
53
## Configure query parameters
50
54
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.
52
56
53
57
```csharp
54
58
@@ -66,7 +70,7 @@ namespace Company.Function
66
70
stringphoneNumberTo=req.Query["phoneNumber"]; // get phone number query parameter
67
71
stringurlToShorten=req.Query["url"]; // get url to shorten query parameter
68
72
69
-
73
+
returnnewOkObjectResult(null);
70
74
}
71
75
}
72
76
}
@@ -75,9 +79,13 @@ namespace Company.Function
75
79
76
80
## Shorten the URL
77
81
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).
79
83
80
84
```csharp
85
+
...
86
+
usingSystem.Net.Http;
87
+
usingSystem.Text.Json;
88
+
usingSystem.Text;
81
89
82
90
namespaceCompany.Function
83
91
{
@@ -97,9 +105,11 @@ namespace Company.Function
97
105
{
98
106
log.LogInformation("C# HTTP trigger function processed a request.");
99
107
108
+
//Parse Query Parameters
100
109
stringphoneNumberTo=req.Query["phoneNumber"]; // get phone number query parameter
101
110
stringurlToShorten=req.Query["url"]; // get url to shorten query parameter
102
-
111
+
112
+
//Get short URL from Azure URL Shortener
103
113
usingvarclient=newHttpClient();
104
114
varrequestData=new
105
115
{
@@ -112,6 +122,8 @@ namespace Company.Function
112
122
vardata=System.Text.Json.JsonSerializer.Deserialize<ShortenedUrl>(content); // Parse content to ShortenedUrl object
113
123
varurl=data.ShortUrl;
114
124
log.LogInformation("Shortened URL "+url);
125
+
126
+
returnnewOkObjectResult(null);
115
127
}
116
128
}
117
129
}
@@ -120,51 +132,69 @@ namespace Company.Function
120
132
121
133
## Send SMS
122
134
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).
> 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).
178
208
179
209
You can now run your Azure Function locally by pressing `F5` in Visual Studio Code or by running the following command in the terminal:
180
210
@@ -184,10 +214,10 @@ func host start
184
214
185
215
```
186
216
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`.
188
218
189
219
## Deploy to Azure
190
220
191
221
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).
192
222
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