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/twilio-to-acs-chat-csharp-tutorial.md
+55-39Lines changed: 55 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: Migrating from Twilio Conversations Chat to ACS Chat C#
2
+
title: Migrating from Twilio Conversations Chat to Azure Communication Services Chat C#
3
3
description: Guide describes how to migrate C# apps from Twilio Conversations Chat to Azure Communication Services Chat SDK.
4
4
services: azure-communication-services
5
5
ms.date: 07/22/2024
@@ -12,45 +12,50 @@ ms.custom: mode-other
12
12
---
13
13
14
14
## Prerequisites
15
-
Before you get started, make sure to:
16
-
- Create an Azure account with an active subscription. For details, see [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
- Create an Azure Communication Services resource. For details, see [Create an Azure Communication Services resource](../../create-communication-resource.md). You'll need to record your resource **endpoint and connection string** for this quickstart.
19
-
- A [User Access Token](../../identity/access-tokens.md). Be sure to set the scope to **chat**, and **note the token string as well as the user_id string**. You can also use the Azure CLI and run the command below with your connection string to create a user and an access token.
15
+
16
+
1. Create an Azure account with an active subscription. For details, see [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
3. Create an Azure Communication Services resource. For details, see [Create an Azure Communication Services resource](../../quickstarts/create-communication-resource.md). Record your resource **endpoint and connection string**.
19
+
4. A [User Access Token](../../quickstarts/identity/access-tokens.md). Be sure to set the scope to **chat**, and **note the token string and user_id string**. You can also use the Azure CLI and run the command below with your connection string to create a user and an access token.
20
20
21
21
```azurecli-interactive
22
22
az communication identity token issue --scope chat --connection-string "yourConnectionString"
23
23
```
24
24
25
-
For details, see [Use Azure CLI to Create and Manage Access Tokens](../../identity/access-tokens.md?pivots=platform-azcli).
25
+
For more information, see [Use Azure CLI to Create and Manage Access Tokens](../../quickstarts/identity/access-tokens.md?pivots=platform-azcli).
26
26
27
27
## Conceptual Difference
28
-
While both Twilio Conversations and ACS Chat offer similar functionalities, their implementation differs due to the surrounding ecosystems and underlying platform philosophies. Twilio Conversations provides a multi-channel communication API, whereas ACS Chat is focused primarily on chat within the Azure ecosystem. This migration guide provides a basic mapping between common operations in Twilio and their equivalents in ACS Chat, helping you transition your .NET code more smoothly.
28
+
29
+
Both Twilio Conversations and Azure Communication Services Chat offer similar functions, but their implementation differs due to the surrounding ecosystems and underlying platform philosophies. Twilio Conversations provide a multi-channel communication API. Azure Communication Services Chat is focused primarily on chat within the Azure ecosystem. This migration guide provides a basic mapping between common operations in Twilio and their equivalents in Azure Communication Services Chat, helping you transition your .NET code.
ACS requires creating users through the CommunicationIdentityClient.
39
+
Azure Communication Services requires creating users through the `CommunicationIdentityClient`.
38
40
39
41
## Setting up
42
+
40
43
### Install the package
44
+
41
45
To start the migration from Twilio Conversations chat, the first step is to install the Azure Communication Services Chat SDK for .NET to your project.
42
46
43
47
```PowerShell
44
48
dotnet add package Azure.Communication.Chat
45
49
```
46
50
47
51
## Object model
52
+
48
53
The following classes handle some of the major features of the Azure Communication Services Chat SDK for C#.
| ChatClient | This class is needed for the Chat functionality. You instantiate it with your subscription information, and use it to create, get and delete threads. |
53
-
| ChatThreadClient | This class is needed for the Chat Thread functionality. You obtain an instance via the ChatClient, and use it to send/receive/update/delete messages, add/remove/get participants, send typing notifications and read receipts. |
57
+
|`ChatClient`| This class is needed for the Chat functionality. You instantiate it with your subscription information, and use it to create, get and delete threads. |
58
+
|`ChatThreadClient`| This class is needed for the Chat Thread functionality. You obtain an instance via the ChatClient, and use it to send/receive/update/delete messages, add/remove/get participants, send typing notifications and read receipts. |
54
59
55
60
## Create a chat client
56
61
@@ -64,9 +69,9 @@ var twilio = new TwilioRestClient(accountSid, authToken);
64
69
65
70
#### Azure Communication Services
66
71
67
-
To create a chat client in Azure Communication Services, you'll use your Communication Services endpoint and the access token that was generated as part of the prerequisite steps. You need to use the `CommunicationIdentityClient` class from the Identity SDK to create a user and issue a token to pass to your chat client.
72
+
To create a chat client in Azure Communication Services, use your Communication Services endpoint and the access token that was generated as part of the prerequisite steps. You need to use the `CommunicationIdentityClient` class from the Identity SDK to create a user and issue a token to pass to your chat client.
68
73
69
-
Learn more about [User Access Tokens](../../identity/access-tokens.md).
74
+
Learn more about [User Access Tokens](../../quickstarts/identity/access-tokens.md).
70
75
71
76
```csharp
72
77
// Your unique Azure Communication service endpoint
@@ -88,13 +93,13 @@ var conversation = ConversationResource.Create(
88
93
89
94
#### Azure Communication Services
90
95
91
-
In ACS, you create a thread, which is equivalent to a conversation in Twilio.
96
+
In Azure Communication Services, you create a thread, which is equivalent to a conversation in Twilio.
92
97
93
98
Use the `createChatThread` method on the chatClient to create a chat thread
94
-
- Use `topic` to give a topic to this chat; Topic can be updated after the chat thread is created using the `UpdateTopic` function.
95
-
- Use `participants` property to pass a list of `ChatParticipant` objects to be added to the chat thread. The `ChatParticipant` object is initialized with a `CommunicationIdentifier` object. `CommunicationIdentifier` could be of type `CommunicationUserIdentifier`, `MicrosoftTeamsUserIdentifier` or `PhoneNumberIdentifier`. For example, to get a `CommunicationIdentifier` object, you'll need to pass an Access ID which you created by following instruction to [Create a user](../../identity/access-tokens.md#create-an-identity)
99
+
- Use `topic` to give a topic to this chat; you can update the `topic` after the chat thread is created using the `UpdateTopic` function.
100
+
- Use `participants` property to pass a list of `ChatParticipant` objects to be added to the chat thread. Initialize the `ChatParticipant` object with a `CommunicationIdentifier` object. `CommunicationIdentifier` could be of type `CommunicationUserIdentifier`, `MicrosoftTeamsUserIdentifier`, or `PhoneNumberIdentifier`. For example, to get a `CommunicationIdentifier` object, you need to pass an Access ID created following the instructions to [Create a user](../../quickstarts/identity/access-tokens.md#create-an-identity).
96
101
97
-
The response object from the `createChatThread` method contains the `chatThread` details. To interact with the chat thread operations such as adding participants, sending a message, deleting a message, etc., a `chatThreadClient` client instance needs to instantiated using the `GetChatThreadClient` method on the `ChatClient` client.
102
+
The response object from the `createChatThread` method contains the `chatThread` details. To interact with the chat thread operations such as adding participants, sending a message, deleting a message, and so on, instantiate a `chatThreadClient` client instance using the `GetChatThreadClient` method on the `ChatClient` client.
In Twilio Conversations, you interact directly with a conversation using the conversation's SID (unique identifier). Here's how you would typically get a conversation and interact with it:
117
+
118
+
In Twilio Conversations, you interact directly with a conversation using the conversation's SID (unique identifier). Here's how to typically get a conversation and interact with it:
113
119
114
120
```csharp
115
121
varconversationSid="<CONVERSATION_SID>";
@@ -125,7 +131,7 @@ foreach (var message in messages)
125
131
126
132
#### Azure Communication Services
127
133
128
-
The `GetChatThreadClient` method returns a thread client for a thread that already exists. It can be used for performing operations on the created thread: add members, send message, etc. threadId is the unique ID of the existing chat thread.
134
+
The `GetChatThreadClient` method returns a thread client for a thread that already exists. You can use it to perform operations on the created thread: add members, send message, and so on. `threadId` is the unique ID of the existing chat thread.
In Twilio Conversations, you can retrieve all conversations that a user is a participant in by querying the UserConversations resource. This resource provides a list of conversations for a specific user.
144
+
145
+
In Twilio Conversations, you can retrieve all conversations that a user is a participant in by querying the `UserConversations` resource. This resource provides a list of conversations for a specific user.
139
146
140
147
```csharp
141
148
/InitializeTwilioClient
@@ -173,15 +180,19 @@ await foreach (ChatThreadItem chatThreadItem in chatThreadItems)
173
180
### Send a message to a chat thread
174
181
175
182
##### Twilio
183
+
176
184
The following code snippet shows how to send a text message.
The code snippet below shows how to send a media file.
193
+
194
+
The following code snippet shows how to send a media file.
195
+
185
196
```csharp
186
197
// The SID of the conversation you want to send the media message to
187
198
stringconversationSid="<CONVERSATION_SID>";
@@ -201,16 +212,15 @@ var message = MessageResource.Create(
201
212
);
202
213
```
203
214
204
-
#### Azure Communication Services
205
-
Unlike Twilio, ACS does not have a separate function to send text messages or media.
215
+
#### Azure Communication Services
206
216
217
+
Unlike Twilio, Azure Communication Services does not have a separate function to send text messages or media.
207
218
208
219
Use `SendMessage` to send a message to a thread.
209
-
210
220
- Use `content` to provide the content for the message, it's required.
211
-
- Use `type` for the content type of the message such as 'Text' or 'Html'. If not specified, 'Text' will be set.
212
-
- Use `senderDisplayName` to specify the display name of the sender. If not specified, empty string will be set.
213
-
- Use `metadata` optionally to include any additional data you want to send along with the message. This field provides a mechanism for developers to extend chat message functionality and add custom information for your use case. For example, when sharing a file link in the message, you might want to add 'hasAttachment:true' in metadata so that recipient's application can parse that and display accordingly.
221
+
- Use `type` for the content type of the message such as `Text` or `Html`. If not specified, `Text` is the default.
222
+
- Use `senderDisplayName` to specify the display name of the sender. If not specified, empty string is the default.
223
+
- Use `metadata` optionally to include any additional data you want to send along with the message. This field provides a mechanism for developers to extend chat message function and add custom information for your use case. For example, when sharing a file link in the message, you might want to add `hasAttachment:true` in the metadata so that recipient's application can parse that and display accordingly.
Twilio typically uses webhooks to notify your server of incoming messages:
232
244
233
245
The following code snippet shows how to receive a text message.
246
+
234
247
```csharp
235
248
publicIActionResultReceiveMessage()
236
249
{
@@ -253,10 +266,11 @@ The following code snippet shows how to receive a media file.
253
266
}
254
267
```
255
268
269
+
#### Azure Communication Services
256
270
257
-
#### Azure Communication Services
258
-
Unlike Twilio, ACS does not have a separate function to receive text messages or media.
259
-
ACS Chat allows you to subscribe to events directly within the application.
271
+
Unlike Twilio, Azure Communication Services does not have a separate function to receive text messages or media.
272
+
273
+
Azure Communication Services Chat enables you to subscribe to events directly within the application.
260
274
261
275
You can retrieve chat messages by polling the `GetMessages` method on the chat thread client at specified intervals.
262
276
@@ -268,25 +282,26 @@ await foreach (ChatMessage message in allMessages)
268
282
}
269
283
```
270
284
271
-
`GetMessages` takes an optional `DateTimeOffset` parameter. If that offset is specified, you'll receive messages that were received, updated or deleted after it. Note that messages received before the offset time but edited or removed after it will also be returned.
285
+
`GetMessages` takes an optional `DateTimeOffset` parameter. If that offset is specified, you receive messages that were received, updated, or deleted after it. Note that messages received before the offset time but edited or removed after it are also be returned.
272
286
273
-
`GetMessages` returns the latest version of the message, including any edits or deletes that happened to the message using `UpdateMessage` and `DeleteMessage`. For deleted messages, `chatMessage.DeletedOn` returns a datetime value indicating when that message was deleted. For edited messages, `chatMessage.EditedOn` returns a datetime indicating when the message was edited. The original time of message creation can be accessed using `chatMessage.CreatedOn`, and it can be used for ordering the messages.
287
+
`GetMessages` returns the latest version of the message, including any edits or deletes that happened to the message using `UpdateMessage` and `DeleteMessage`. For deleted messages, `chatMessage.DeletedOn` returns a datetime value indicating when that message was deleted. For edited messages, `chatMessage.EditedOn` returns a datetime indicating when the message was edited. You can access the original time of message creation using `chatMessage.CreatedOn`, and use it for ordering the messages.
274
288
275
289
`GetMessages` returns different types of messages which can be identified by `chatMessage.Type`. These types are:
276
290
277
291
-`Text`: Regular chat message sent by a thread member.
278
292
279
-
-`Html`: A formatted text message. Note that Communication Services users currently can't send RichText messages. This message type is supported by messages sent from Teams users to Communication Services users in Teams Interop scenarios.
293
+
-`Html`: A formatted text message. Note that Communication Services users currently can't send `RichText` messages. This message type is supported by messages sent from Teams users to Communication Services users in Teams Interop scenarios.
280
294
281
295
-`TopicUpdated`: System message that indicates the topic has been updated. (readonly)
282
296
283
-
-`ParticipantAdded`: System message that indicates one or more participants have been added to the chat thread.(readonly)
297
+
-`ParticipantAdded`: System message that indicates one or more participants have been added to the chat thread.(readonly)
284
298
285
299
-`ParticipantRemoved`: System message that indicates a participant has been removed from the chat thread.
286
300
287
-
For more details, see [Message Types](../../../concepts/chat/concepts.md#message-types).
301
+
For more information, see [Message Types](../../concepts/chat/concepts.md#message-types).
288
302
289
303
### Add a user as a participant to the chat thread
304
+
290
305
#### Twilio
291
306
292
307
```csharp
@@ -298,8 +313,9 @@ var participant = ParticipantResource.Create(
298
313
299
314
#### Azure Communication Services
300
315
301
-
In ACS, you add participants when creating the chat thread or afterwards:
302
-
Once a thread is created, you can then add and remove users from it. By adding users, you give them access to be able to send messages to the thread, and add/remove other participant. Before calling `AddParticipants`, ensure that you have acquired a new access token and identity for that user. The user will need that access token in order to initialize their chat client.
316
+
In Azure Communication Services, you add participants when creating the chat thread or afterwards:
317
+
318
+
Once you create a thread, you can add and remove users. Adding users gives them access to send messages to the thread, and add/remove other participants. Before calling `AddParticipants`, ensure that you acquire a new access token and identity for that user. The user needs the access token to initialize their chat client.
303
319
304
320
Use `AddParticipants` to add one or more participants to the chat thread. The following are the supported attributes for each thread participant(s):
305
321
-`communicationUser`, required, is the identity of the thread participant.
In Twilio Conversations, you use the ConversationResource to retrieve the participants of a specific conversation. You can then list all participants associated with that conversation.
343
+
344
+
In Twilio Conversations, you use the `ConversationResource` to retrieve the participants of a specific conversation. You can then list all participants associated with that conversation.
328
345
329
346
```csharp
330
347
// The SID of the conversation you want to retrieve participants from
@@ -378,4 +395,3 @@ Use `SendReadReceipt` to notify other participants that the message is read by t
0 commit comments