Skip to content

Commit 06ef713

Browse files
authored
Merge pull request #264437 from pgrandhi/AddEventsDocs
Adding Events documentation to quickstart
2 parents 25e6785 + 0f6bcd9 commit 06ef713

23 files changed

+551
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
author: pgrandhi
3+
ms.service: azure-communication-services
4+
ms.custom: devx-track-azurecli
5+
ms.topic: include
6+
ms.date: 01/27/2024
7+
ms.author: pgrandhi
8+
---
9+
10+
## Prerequisites
11+
12+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/dotnet/).
13+
- Install [Azure CLI](/cli/azure/install-azure-cli-windows?tabs=azure-cli)
14+
- An [Azure Communication Services resource](../../create-communication-resource.md)
15+
- Create a Webhook to receive events. [Webhook Event Delivery](../../../../../articles/event-grid/webhook-event-delivery.md)
16+
17+
18+
[!INCLUDE [register-provider-cli.md](register-provider-cli.md)]
19+
20+
## Create event subscription
21+
22+
To create event subscriptions for Azure Communication Services resource, [sign in to Azure CLI](/cli/azure/authenticate-azure-cli). You can sign in running the ```az login``` command from the terminal and providing your credentials. Run the following command to create the event subscription for the resource:
23+
24+
To create an event subscription by using [the Azure CLI](/cli/azure/get-started-with-azure-cli), use the [`az eventgrid event-subscription create`](/cli/azure/eventgrid/event-subscription#az-eventgrid-event-subscription-create) command.
25+
26+
```azurecli-interactive
27+
az eventgrid event-subscription create
28+
--name EventsWebhookSubscription
29+
--source-resource-id /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>
30+
--included-event-types Microsoft.Communication.SMSReceived Microsoft.Communication.SMSDeliveryReportReceived
31+
--endpoint-type webhook
32+
--endpoint https://azureeventgridviewer.azurewebsites.net/api/updates
33+
```
34+
35+
For a list of Communication Services events, see [Communication Services Events](../../../../../articles/event-grid/event-schema-communication-services.md).
36+
37+
## List event subscriptions
38+
39+
To list all the existing event subscriptions set up for an Azure Communication Services resource, by using [the Azure CLI](/cli/azure/get-started-with-azure-cli), use the [`az eventgrid event-subscription list`](/cli/azure/eventgrid/event-subscription#az-eventgrid-event-subscription-list) command.
40+
41+
```azurecli-interactive
42+
az eventgrid event-subscription update
43+
--name EventsWebhookSubscription
44+
--source-resource-id /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>
45+
--included-event-types Microsoft.Communication.SMSReceived Microsoft.Communication.SMSDeliveryReportReceived Microsoft.Communication.ChatMessageReceived
46+
--endpoint-type webhook
47+
--endpoint https://azureeventgridviewer.azurewebsites.net/api/updates
48+
```
49+
50+
## Update event subscription
51+
52+
To update an existing event subscription by using [the Azure CLI](/cli/azure/get-started-with-azure-cli), use the [`az eventgrid event-subscription update`](/cli/azure/eventgrid/event-subscription#az-eventgrid-event-subscription-update) command.
53+
54+
```azurecli-interactive
55+
az eventgrid event-subscription list
56+
--source-resource-id /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>
57+
```
58+
59+
## Delete event subscription
60+
61+
To delete an existing event subscription by using [the Azure CLI](/cli/azure/get-started-with-azure-cli), use the [`az eventgrid event-subscription delete`](/cli/azure/eventgrid/event-subscription#az-eventgrid-event-subscription-delete) command.
62+
63+
```azurecli-interactive
64+
az eventgrid event-subscription delete
65+
--name EventsWebhookSubscription
66+
--source-resource-id /subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>
67+
```
68+
69+
## Next steps
70+
* For information on other commands, see [Azure Event Grid CLI](/cli/azure/eventgrid/event-subscription).
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
author: pgrandhi
3+
ms.service: azure-communication-services
4+
ms.topic: include
5+
ms.date: 01/26/2024
6+
ms.author: pgrandhi
7+
---
8+
9+
## Prerequisites
10+
11+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/dotnet/).
12+
- An [Azure Communication Services resource](../../create-communication-resource.md).
13+
- Create a Webhook to receive events. [Webhook Event Delivery](/azure/event-grid/webhook-event-delivery).
14+
15+
[!INCLUDE [register-event-grid-resource-provider.md](register-event-grid-resource-provider.md)]
16+
17+
## Create event subscription
18+
19+
To create an Event subscription for Azure Communication Services, use the following steps. This article shows how to create an Event subscription for Azure Communication Services to receive events via Webhook.
20+
21+
To create an Event subscription for Azure Communication Services resource, first sign in to the [Azure portal](https://portal.azure.com). In the upper-left corner of the page, select the Communication Services resource.
22+
* Select on the Events tab on the left menu.
23+
* Select on **+ Event Subscription**.
24+
25+
:::image type="content" source="../media/subscribe-through-portal/add-subscription.png" alt-text="Screenshot highlighting the create event subscription button in the Azure portal." lightbox="../media/subscribe-through-portal/add-subscription.png":::
26+
27+
* On the **Create Event Subscription** page, follow these steps:
28+
1. Enter a name for the event subscription.
29+
1. Enter a name for the System topic name.
30+
1. Select the event types that you want to receive on the event subscription.
31+
32+
:::image type="content" source="../media/subscribe-through-portal/select-event-types.png" alt-text="Screenshot that shows the selection of event types." lightbox="../media/subscribe-through-portal/select-event-types.png":::
33+
34+
For more information on Communication Services events, see [Communication Services Events](/azure/event-grid/event-schema-communication-services)
35+
36+
1. Select the Endpoint Type as Web Hook.
37+
38+
:::image type="content" source="../media/subscribe-through-portal/select-endpoint-type.png" alt-text="Screenshot that shows the selection of endpoint type." lightbox="../media/subscribe-through-portal/select-endpoint-type.png":::
39+
40+
1. Select **Configure an Endpoint**
41+
42+
:::image type="content" source="../media/subscribe-through-portal/create-event-subscription.png" alt-text="Screenshot highlighting the create event page in the Azure portal." lightbox="../media/subscribe-through-portal/create-event-subscription.png":::
43+
44+
1. Enter the link to the webhook and Select **Confirm Selection**.
45+
46+
:::image type="content" source="../media/subscribe-through-portal/select-web-hook-endpoint.png" alt-text="Screenshot highlighting the select webhook endpoint page in the Azure portal." lightbox="../media/subscribe-through-portal/select-web-hook-endpoint.png":::
47+
48+
1. In the **Filters** tab, add the names of the event types you want to filter in the subscription and add context attribute filters you want to use in the subscription. Then, select **Next: Additional features** at the bottom of the page.
49+
50+
:::image type="content" source="../media/subscribe-through-portal/create-event-filters.png" alt-text="Screenshot highlighting Event Grid create filters page in the Azure portal." lightbox="../media/subscribe-through-portal/create-event-filters.png":::
51+
52+
1. To enable dead lettering and customize retry policies, select **Additional Features**.
53+
54+
:::image type="content" source="../media/subscribe-through-portal/select-additional-features.png" alt-text="Screenshot that shows the Additional features tab of the Create Event Subscription page." lightbox="../media/subscribe-through-portal/select-additional-features.png":::
55+
56+
1. When done, select **Create**.
57+
58+
59+
## Update event subscription
60+
61+
To update an Event subscription for Azure Communication Services, use the following steps. This section shows how to update an Event subscription for Azure Communication Services to update the events you want to receive via Webhook.
62+
63+
To update an Event subscription for Azure Communication Services resource, first sign in to the [Azure portal](https://portal.azure.com). In the upper-left corner of the page, select the Communication Services resource.
64+
65+
1. Select the Events tab on the left menu.
66+
1. Select the **Event Subscriptions** and select on the Event subscription you want to update.
67+
68+
:::image type="content" source="../media/subscribe-through-portal/event-subscriptions.png" alt-text="Screenshot highlighting the event subscription button in the Azure portal." lightbox="../media/subscribe-through-portal/event-subscriptions.png":::
69+
70+
1. On the **Event Subscription** page, select on **Filters** tab. Select the event types that you want to receive on the event subscription.
71+
72+
:::image type="content" source="../media/subscribe-through-portal/update-event-types.png" alt-text="Screenshot that shows the selection of event types to update." lightbox="../media/subscribe-through-portal/update-event-types.png":::
73+
74+
1. To enable dead lettering and customize retry policies, select **Additional Features**.
75+
76+
:::image type="content" source="../media/subscribe-through-portal/select-additional-features.png" alt-text="Screenshot that shows the Additional features tab of the Update Event Subscription page." lightbox="../media/subscribe-through-portal/select-additional-features.png":::
77+
78+
1. To update the webhook to receive events, select on **Change** next to the webhook link and enter the new webhook endpoint.
79+
:::image type="content" source="../media/subscribe-through-portal/update-web-hook-endpoint.png" alt-text="Screenshot that shows the Change the webhook endpoint link in the Event Subscription page." lightbox="../media/subscribe-through-portal/update-web-hook-endpoint.png":::
80+
81+
1. When done, select **Save**,
82+
:::image type="content" source="../media/subscribe-through-portal/save-update.png" alt-text="Screenshot that shows the save button in the Azure portal." lightbox="../media/subscribe-through-portal/save-update.png":::
83+
84+
## Delete event subscription
85+
86+
To delete an Event subscription for Azure Communication Services, use the following steps.
87+
88+
To delete an Event subscription for Azure Communication Services resource, first sign in to the [Azure portal](https://portal.azure.com). In the upper-left corner of the page, select the Communication Services resource.
89+
90+
1. Select on the Events tab on the left menu.
91+
1. Select on **Event Subscriptions** and Select on the Event subscription you want to delete.
92+
93+
:::image type="content" source="../media/subscribe-through-portal/event-subscriptions.png" alt-text="Screenshot highlighting the event subscriptions button to access event subscription to be deleted in the Azure portal." lightbox="../media/subscribe-through-portal/event-subscriptions.png":::
94+
95+
1. On the Event Subscription page, Select on **Delete** button at the top.
96+
97+
:::image type="content" source="../media/subscribe-through-portal/delete-subscription.png" alt-text="Screenshot highlighting the delete button in the Azure portal." lightbox="../media/subscribe-through-portal/delete-subscription.png":::
98+
99+
## Next steps
100+
* For a list of Communication Services events, see [Communication Services Events](/azure/event-grid/event-schema-communication-services).
101+
* For a list of supported event handlers, see [Event handlers](/azure/event-grid/event-handlers).
102+
* For information about event delivery and retries, [Event Grid message delivery and retry](/azure/event-grid/delivery-and-retry).
103+
* For an introduction to Event Grid, see [About Event Grid](/azure/event-grid/overview).
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
author: pgrandhi
3+
ms.service: azure-communication-services
4+
ms.topic: include
5+
ms.date: 01/28/2024
6+
ms.author: pgrandhi
7+
---
8+
9+
## Prerequisites
10+
11+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/dotnet/).
12+
- The latest version [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core) for your operating system.
13+
- Get the latest version of the [.NET Microsoft Azure Event Grid Management SDK](/azure/event-grid/sdk-overview).
14+
- Get the latest version of the [Azure Identity](/dotnet/api/overview/azure/identity-readme) library.
15+
- An [Azure Communication Services resource](../../create-communication-resource.md).
16+
17+
[!INCLUDE [register-event-grid-resource-provider.md](register-event-grid-resource-provider.md)]
18+
19+
## Installing the SDK
20+
21+
First, install the Microsoft Azure Event Grid Management library for .NET with [NuGet](https://www.nuget.org/):
22+
23+
```csharp
24+
dotnet add package Azure.ResourceManager.EventGrid;
25+
```
26+
Include the Event Grid Management SDK in your C# project:
27+
28+
```csharp
29+
using Microsoft.Azure.Management.EventGrid;
30+
using Microsoft.Azure.Management.EventGrid.Models;
31+
```
32+
33+
## Authenticating with Azure Identity library
34+
35+
### Prerequisites:
36+
1. Create a Microsoft Entra application and Service Principal and set up a client secret or trusted certificate issued by certificate authority by following the instructions [here](/entra/identity-platform/howto-create-service-principal-portal).
37+
1. Store the secret or the certificate in the Azure Keyvault.
38+
1. Give contributor or owner access to the subscription to that application following the instructions [here](/azure/role-based-access-control/quickstart-assign-role-user-portal).
39+
1. Read more about authorizing access to Event Grid resources [here](/azure/event-grid/security-authorization).
40+
41+
The Azure Identity library provides Microsoft Entra ID (Formerly Azure Active Directory) token authentication support across the Azure SDK. It provides a set of TokenCredential implementations, which can be used to construct Azure SDK clients that support Microsoft Entra token authentication. You can read more about it [here](/dotnet/api/overview/azure/identity-readme).
42+
43+
1. Include the Azure Identity client library for .NET with [NuGet](https://www.nuget.org/):
44+
45+
```csharp
46+
dotnet add package Azure.Identity;
47+
dotnet add package Azure.Security.KeyVault.Secrets
48+
```
49+
2. Include the Azure Identity library in your C# project
50+
51+
```csharp
52+
using Microsoft.Azure.Identity;
53+
using Azure.Security.KeyVault.Secrets
54+
```
55+
56+
3. You can either pass the secret credentials or certificate credentials to get access token, based on how your Service Principal is configured.
57+
58+
* Get access token using secret credential
59+
60+
To get the secret credentials, you need to read it from the Keyvault you created in Prerequisite #2 using [SecretClient](/azure/key-vault/secrets/quick-create-net).
61+
62+
```csharp
63+
// Authenticate the Keyvault client with DefaultAzureCredential and get the secret.
64+
SecretClient secretClient = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());
65+
string clientSecret = await secretClient.GetSecretAsync(secretName).Value;
66+
67+
// Get access token using secret credentials
68+
string[] scopes = { "https://management.azure.com/.default" };
69+
var application = ConfidentialClientApplicationBuilder
70+
.Create('your-servicePrincipal-appId')
71+
.WithAuthority(authorityUri: new Uri(authority), validateAuthority: true)
72+
.WithTenantId('your-tenant_id')
73+
.WithClientSecret(clientSecret)
74+
.Build();
75+
76+
var token = await application
77+
.AcquireTokenForClient(scopes)
78+
.ExecuteAsync();
79+
```
80+
81+
* Get access token using certificate credential
82+
83+
To get the certificate credentials, you need to read it from the Keyvault you created in Prerequisite #2 using [CertificateClient](/azure/key-vault/certificates/quick-create-net).
84+
85+
Read more about the Microsoft Entra application configuration authority [here](/entra/identity-platform/msal-client-application-configuration)
86+
87+
```csharp
88+
// Authenticate the certificate client with DefaultAzureCredential and get the certificate.
89+
CertificateClient certificateClient = new SecretClient(new Uri("https://myvault.vault.azure.net/"), new DefaultAzureCredential());
90+
X509Certificat2 cert = await certificateClient.DownloadCertificateAsync(certificateName);
91+
92+
// Get access token using certificate credentials
93+
string[] scopes = { "https://management.azure.com/.default" };
94+
string authority = "https://login.microsoftonline.com/<tenant>/";
95+
var application = ConfidentialClientApplicationBuilder
96+
.Create('<servicePrincipal-appId>')
97+
.WithAuthority(authorityUri: new Uri(authority), validateAuthority: true)
98+
.WithTenantId("<tenantId>")
99+
.WithCertificate(cert)
100+
.Build();
101+
102+
var token = await application
103+
.AcquireTokenForClient(scopes)
104+
.WithSendX5C(true)
105+
.ExecuteAsync();
106+
```
107+
108+
4. Authenticate EventGridManagementClient with access token using secret or certificate credentials
109+
110+
111+
```csharp
112+
// Authenticate EventGridManagementClient with Microsoft Entra ID access token credential
113+
eventGridClient = new EventGridManagementClient(new Uri("https://management.azure.com/"),
114+
new TokenCredentials(token.AccessToken));
115+
116+
eventGridClient.SubscriptionId = 'your_subscripiton_id';
117+
```
118+
119+
## Create Event Subscription
120+
This code sample shows how to create the event subscription for the webhook subscriber endpoint.
121+
122+
```csharp
123+
string webhookUri = $"<webhookUri>";
124+
string resourceId="/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>";
125+
string[] includedEventTypes = new string[]{ "Microsoft.Communication.SMSReceived",
126+
"Microsoft.Communication.SMSDeliveryReportReceived"
127+
};
128+
129+
EventSubscription eventSubscription = new EventSubscription(
130+
name: "<eventSubscriptionName>",
131+
eventDeliverySchema: "EventGridSchema",
132+
filter: new EventSubscriptionFilter(
133+
includedEventTypes: includedEventTypes),
134+
destination: new WebHookEventSubscriptionDestination(webhookUri));
135+
136+
await eventGridClient.EventSubscriptions.CreateOrUpdateAsync(
137+
scope: resourceId,
138+
eventSubscriptionName: "<eventSubscriptionName>",
139+
eventSubscriptionInfo: eventSubscription);
140+
```
141+
142+
## Update event subscription
143+
This code sample shows how to update the event subscription to add more events, you want to receive on the webhook subscriber endpoint.
144+
145+
```csharp
146+
string webhookUri = $"<webhookUri>";
147+
string resourceId="/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>";
148+
string[] additionalEventTypes = new string[]{
149+
"Microsoft.Communication.ChatMessageReceived"
150+
};
151+
152+
await eventGridClient.EventSubscriptions.UpdateAsync(
153+
scope: resourceId,
154+
eventSubscriptionName: "<eventSubscriptionName>",
155+
eventSubscriptionUpdateParameters: new EventSubscriptionUpdateParameters(
156+
filter: new EventSubscriptionFilter(includedEventTypes: additionalEventTypes)));
157+
```
158+
159+
## Delete event Subscription
160+
This code sample shows how to delete the event subscription for the webhook subscriber endpoint.
161+
162+
```csharp
163+
string webhookUri = $"<webhookUri>";
164+
string resourceId="/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Communication/CommunicationServices/<acsResourceName>";
165+
166+
await eventGridClient.EventSubscriptions.DeleteAsync(
167+
scope: resourceId,
168+
eventSubscriptionName: "<eventSubscriptionName>");
169+
```
170+

0 commit comments

Comments
 (0)