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-js-tutorial.md
+66-37Lines changed: 66 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: include file
3
-
description: include file
2
+
title: Migrating from Twilio Conversations Chat to Azure Communication Services Chat JavaScript
3
+
description: Guide describes how to migrate JavaScript apps from Twilio Conversations Chat to Azure Communication Services Chat SDK.
4
4
services: azure-communication-services
5
5
ms.date: 07/22/2024
6
6
ms.topic: include
@@ -16,8 +16,9 @@ ms.custom: mode-other
16
16
1.**Azure Account:** Make sure that your Azure account is active. New users can create a free account at [Microsoft Azure](https://azure.microsoft.com/free/).
17
17
2.**Node.js 18:** Ensure Node.js 18 is installed on your system. Download from [Node.js](https://nodejs.org/en).
18
18
3.**Communication Services Resource:** Set up a [Communication Services Resource](../../quickstarts/create-communication-resource.md?tabs=windows&pivots=platform-azp) via your Azure portal and note your connection string.
19
-
4.**Azure CLI:** Follow the instructions to [Install Azure CLI on Windows](/cli/azure/install-azure-cli-windows?tabs=azure-cli)..
19
+
4.**Azure CLI:** Follow the instructions to [Install Azure CLI on Windows](/cli/azure/install-azure-cli-windows?tabs=azure-cli).
20
20
5.**User Access Token:** Generate a user access token to instantiate the chat client. You can create one using the Azure CLI as follows:
21
+
21
22
```console
22
23
az communication identity token issue --scope voip --connection-string "yourConnectionString"
23
24
```
@@ -41,21 +42,24 @@ The `--save` option adds the library as a dependency in your **package.json** fi
41
42
### Remove the Twilio SDK from the project
42
43
43
44
You can remove the Twilio SDK from your project by uninstalling the package.
45
+
44
46
```console
45
47
npm uninstall twilio-conversations
46
48
```
47
49
48
50
## Object model
51
+
49
52
The following classes and interfaces handle some of the major features of the Azure Communication Services Chat SDK for JavaScript.
| ChatClient | This class is needed for the Chat functionality. You instantiate it with your subscription information, and use it to create, get, delete threads, and subscribe to chat events. |
54
-
| 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 users, send typing notifications and read receipts. |
56
+
|`ChatClient`| This class is needed for the Chat function. Instantiate it with your subscription information, and use it to create, get, delete threads, and subscribe to chat events. |
57
+
|`ChatThreadClient`| This class is needed for the Chat Thread function. Get an instance via the `ChatClient`, and use it to send/receive/update/delete messages, add/remove/get users, send typing notifications, and read receipts. |
Similar to Twilio, the first step is to get an **access token** as well as the Communication Service **endpoint** that was generated as part of the prerequisite steps. Replace those in the code.
81
+
82
+
Similar to Twilio, the first step is to get an **access token** and the Communication Service **endpoint** that was generated as part of the prerequisite steps. Replace the placeholders in the code.
@@ -92,7 +97,8 @@ console.log('Azure Communication Chat client created!');
92
97
### Start a chat thread
93
98
94
99
#### Twilio
95
-
This is how you start a chat thread in Twilio Conversations. Use `FriendlyName` to give a human-readable name to this conversation.
100
+
101
+
Start a chat thread in Twilio Conversations. Use `FriendlyName` to give a human-readable name to this conversation.
96
102
97
103
```JavaScript
98
104
let conversation =awaitclient.createConversation({
@@ -102,14 +108,15 @@ await conversation.join();
102
108
```
103
109
104
110
#### Azure Communication Services
111
+
105
112
Use the `createThread` method to create a chat thread.
106
113
107
-
`createThreadRequest` is used to describe the thread request:
114
+
Use `createThreadRequest` to describe the thread request:
108
115
109
-
- Use `topic` to give a topic to this chat. Topics can be updated after the chat thread is created using the `UpdateThread` function.
116
+
- Use `topic` to give a topic to this chat. update `topic` after you create the chat thread using the `UpdateThread` function.
110
117
- Use `participants` to list the participants to be added to the chat thread.
111
118
112
-
When resolved, `createChatThread` method returns a `CreateChatThreadResult`. This model contains a `chatThread` property where you can access the `id` of the newly created thread. You can then use the `id` to get an instance of a `ChatThreadClient`. The `ChatThreadClient` can then be used to perform operation within the thread such as sending messages or listing participants.
119
+
When resolved, `createChatThread` method returns a `CreateChatThreadResult`. This model contains a `chatThread` property where you can access the `id` of the newly created thread. Then use the `id` to get an instance of a `ChatThreadClient`. Then use the `ChatThreadClient` to perform operation within the thread such as sending messages or listing participants.
113
120
114
121
```JavaScript
115
122
asyncfunctioncreateChatThread() {
@@ -136,7 +143,9 @@ async function createChatThread() {
136
143
### Get a chat thread client
137
144
138
145
#### Twilio
139
-
This is how you get a chat thread (conversation) in Twilio.
146
+
147
+
Get a chat thread (conversation) in Twilio.
148
+
140
149
```JavaScript
141
150
if (selectedConversation) {
142
151
conversationContent = (
@@ -151,24 +160,30 @@ if (selectedConversation) {
151
160
conversationContent ="";
152
161
}
153
162
```
163
+
154
164
#### Azure Communication Services
155
-
The `getChatThreadClient` method returns a `chatThreadClient` for a thread that already exists. It can be used for performing operations on the created thread: add participants, send message, etc. threadId is the unique ID of the existing chat thread.
165
+
166
+
The `getChatThreadClient` method returns a `chatThreadClient` for a thread that already exists. Use it to perform operations on the created thread: add participants, send message, and so on. The `threadId` is the unique ID of the existing chat thread.
156
167
157
168
```JavaScript
158
169
let chatThreadClient =chatClient.getChatThreadClient(threadId);
159
170
console.log(`Chat Thread client for threadId:${threadId}`);
160
-
161
171
```
162
-
Add this code in place of the `<CREATE CHAT THREAD CLIENT>` comment in **client.js**, refresh your browser tab and check the console, you should see:
172
+
173
+
Add this code in place of the `<CREATE CHAT THREAD CLIENT>` comment in **client.js**, refresh your browser tab, and check the console. You should see:
174
+
163
175
```console
164
176
Chat Thread client for threadId: <threadId>
165
177
```
166
178
167
179
### Add a user as a participant to the chat thread
168
-
Once a chat thread is created, you can then add and remove users from it. By adding users, you give them access to send messages to the chat thread and add/remove other participants.
180
+
181
+
Once you create a chat thread, you can then add and remove users from it. By adding users, you give them access to send messages to the chat thread and add/remove other participants.
169
182
170
183
#### Twilio
171
-
This is how you add a participant to a chat thread.
184
+
185
+
Add a participant to a chat thread.
186
+
172
187
```JavaScript
173
188
// add chat participant to the conversation by its identity
Before calling the `addParticipants` method, ensure that you've acquired a new access token and identity for that user. The user will need that access token in order to initialize their chat client.
187
201
188
-
`addParticipantsRequest` describes the request object wherein `participants` lists the participants to be added to the chat thread;
202
+
Before calling the `addParticipants` method, be sure to acquire a new access token and identity for that user. The user needs that access token to initialize their chat client.
203
+
204
+
`addParticipantsRequest` describes the request object wherein `participants` lists the participants to be added to the chat thread:
189
205
-`id`, required, is the communication identifier to be added to the chat thread.
190
206
-`displayName`, optional, is the display name for the thread participant.
191
207
-`shareHistoryTime`, optional, is the time from which the chat history is shared with the participant. To share history since the inception of the chat thread, set this property to any date equal to, or less than the thread creation time. To share no history previous to when the participant was added, set it to the current date. To share partial history, set it to the date of your choice.
Use `sendMessage` method to send a message to a thread identified by threadId.
246
268
247
269
`sendMessageRequest` is used to describe the message request:
248
270
249
-
- Use `content` to provide the chat message content;
271
+
- Use `content` to provide the chat message content.
250
272
251
-
`sendMessageOptions` is used to describe the operation optional params:
273
+
Use `sendMessageOptions` to describe the operation optional params:
252
274
253
-
- Use `senderDisplayName` to specify the display name of the sender;
254
-
- Use `type` to specify the message type, such as 'text' or 'html';
255
-
This is how you accomplish the "Media" property in Twilio.
256
-
- Use `metadata` optionally to include any other 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. Please refer to the [following tutorial](../file-sharing-tutorial-acs-chat.md).
275
+
- Use `senderDisplayName` to specify the display name of the sender.
276
+
- Use `type` to specify the message type, such as `text` or `html`.
257
277
258
-
`SendChatMessageResult` is the response returned from sending a message, it contains an ID, which is the unique ID of the message.
278
+
To recreate the "Media" property in Twilio.
279
+
- Use `metadata` optionally to include any other data you want to send along with the message. This field enables developers to extend the 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 metadata so that recipient's application can parse that and display accordingly. For more information, see [File sharing](../file-sharing-tutorial-acs-chat.md).
280
+
281
+
`SendChatMessageResult` is the response returned from sending a message. It contains an ID, which is the unique ID of the message.
Unlike Twilio, ACS does not have separate functions to receive text messages or media. Azure Communication Services uses Azure Event Grid to handle events. To learn more, please refer to [following tutorial](MicrosoftDocs/azure-docs-pr/articles/event-grid/event-schema-communication-services.md)
303
+
304
+
Unlike Twilio, Azure Communication Services doesn't have separate functions to receive text messages or media. Azure Communication Services uses Azure Event Grid to handle events. For more information, see [Event Handling](/azure/event-grid/event-schema-communication-services.md).
281
305
282
306
#### Twilio
283
-
This is how you receive a text message in Twilio.
307
+
308
+
To receive a text message in Twilio.
309
+
284
310
```JavaScript
285
311
// Receive text message
286
312
let paginator =
@@ -290,7 +316,8 @@ This is how you receive a text message in Twilio.
With real-time signaling, you can subscribe to listen for new incoming messages and update the current messages in memory accordingly. Azure Communication Services supports a [list of events that you can subscribe to](../../../concepts/chat/concepts.md#real-time-notifications).
332
+
333
+
With real-time signaling, you can subscribe to listen for new incoming messages and update the current messages in memory accordingly. Azure Communication Services supports a [list of events that you can subscribe to](../../concepts/chat/concepts.md#real-time-notifications).
306
334
307
335
```JavaScript
308
336
// open notifications channel
@@ -325,15 +353,16 @@ for await (const message of messages) {
325
353
}
326
354
327
355
```
328
-
`listMessages` returns different types of messages that can be identified by `chatMessage.type`.
329
356
330
-
For more details, see [Message Types](../../../concepts/chat/concepts.md#message-types).
357
+
`listMessages` returns different types of messages that you can identify by `chatMessage.type`.
331
358
359
+
For more information, see [Message Types](../../concepts/chat/concepts.md#message-types).
332
360
333
361
#### Subscribe to connection status of real time notifications
334
-
Similar to Twilio, Azure Communication Services allows you to subscribe to event notifications.
335
362
336
-
Subscription to events `realTimeNotificationConnected` and `realTimeNotificationDisconnected` allows you to know when the connection to the call server is active.
363
+
Similar to Twilio, Azure Communication Services enables you to subscribe to event notifications.
364
+
365
+
Subscribing to events `realTimeNotificationConnected` and `realTimeNotificationDisconnected` lets you know when the connection to the call server is active.
337
366
338
367
```JavaScript
339
368
// subscribe to realTimeNotificationConnected event
0 commit comments