Skip to content

Commit b69b651

Browse files
authored
Review, edit, raised Acrolinx score from 86 to 99
1 parent eba6650 commit b69b651

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

articles/communication-services/tutorials/includes/twilio-to-acs-chat-java-tutorial.md

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Migrating from Twilio Conversations Chat to ACS Chat Java
2+
title: Migrating from Twilio Conversations Chat to Azure Communication Services Chat Java
33
description: Guide describes how to migrate Java apps from Twilio Conversations Chat to Azure Communication Services Chat SDK.
44
services: azure-communication-services
55
ms.date: 07/22/2024
@@ -16,21 +16,23 @@ ms.custom: mode-other
1616
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
1717
- [Java Development Kit (JDK)](/azure/developer/java/fundamentals/java-jdk-install) version 8 or above.
1818
- [Apache Maven](https://maven.apache.org/download.cgi).
19-
- 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.
20-
- 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.
19+
- Create an Azure Communication Services resource. For details, see [Create an Azure Communication Services resource](../../quickstarts/create-communication-resource.md). You need to record your resource **endpoint and connection string**.
20+
- A [User Access Token](../../quickstarts/identity/access-tokens.md). Be sure to set the scope to **chat**, and **note the token string and the user_id string**. You can also use the Azure CLI and run the following command with your connection string to create a user and an access token.
2121

2222
```azurecli-interactive
2323
az communication identity token issue --scope chat --connection-string "yourConnectionString"
2424
```
2525

26-
For details, see [Use Azure CLI to Create and Manage Access Tokens](../../identity/access-tokens.md?pivots=platform-azcli).
26+
For details, see [Use Azure CLI to Create and Manage Access Tokens](../../quickstarts/identity/access-tokens.md?pivots=platform-azcli).
2727

2828
## Setting up
2929

3030
### Add the package references for the Chat SDK
3131

3232
#### Twilio
33+
3334
To use Twilio Conversations Chat APIs in your Java application, add the following dependency in your pom.xml:
35+
3436
```xml
3537
<dependencies>
3638
<!-- Twilio Java SDK -->
@@ -70,20 +72,22 @@ The following classes and interfaces handle some of the major features of the Az
7072

7173
| Name | Description |
7274
| ------------------------------------- | ------------------------------------------------------------ |
73-
| 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. |
74-
| ChatAsyncClient | This class is needed for the asynchronous Chat functionality. You instantiate it with your subscription information, and use it to create, get and delete threads. |
75-
| 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. |
76-
| ChatThreadAsyncClient | This class is needed for the asynchronous Chat Thread functionality. You obtain an instance via the ChatAsyncClient, and use it to send/receive/update/delete messages, add/remove/get users, send typing notifications and read receipts. |
75+
| `ChatClient` | This class is needed for the Chat function. Instantiate it with your subscription information, and use it to create, get, and delete threads. |
76+
| `ChatAsyncClient` | This class is needed for the asynchronous Chat function. Instantiate it with your subscription information, and use it to create, get, and delete threads. |
77+
| `ChatThreadClient` | This class is needed for the Chat Thread function. You 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. |
78+
| `ChatThreadAsyncClient` | This class is needed for the asynchronous Chat Thread function. You get an instance via the `ChatAsyncClient`, and use it to send/receive/update/delete messages, add/remove/get users, send typing notifications, and read receipts. |
7779

7880
### Import
7981

8082
#### Twilio
83+
8184
```Java
8285
import com.twilio.Twilio;
8386
import com.twilio.rest.conversations.v1.Conversation;
8487
```
8588

8689
#### Azure Communication Services
90+
8791
```Java
8892
package com.communication.quickstart;
8993

@@ -99,7 +103,9 @@ import java.util.*;
99103
### Create a chat client
100104

101105
#### Twilio
106+
102107
In Twilio, you initialize the client using the Account SID and Auth Token. Here's how you typically initialize a client.
108+
103109
```Java
104110
String accountSid = "<YOUR_ACCOUNT_SID>";
105111
String authToken = "<YOUR_AUTH_TOKEN>";
@@ -110,11 +116,11 @@ Twilio.init(accountSid, authToken);
110116

111117
#### Azure Communication Services
112118

113-
To create a chat client, you'll use the Communications Service endpoint and the access token that was generated as part of pre-requisite steps. User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. Once you generate these tokens on your server, pass them back to a client device. You need to use the CommunicationTokenCredential class from the Common SDK to pass the token to your chat client.
119+
To create a chat client, use the Communications Service endpoint and the access token that was generated as part of prerequisite steps. User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. Once you generate these tokens on your server, pass them back to a client device. You need to use the `CommunicationTokenCredentia`l class from the Common SDK to pass the token to your chat client.
114120

115-
Learn more about [Chat Architecture](../../../concepts/chat/concepts.md)
121+
Learn more about [Chat Architecture](../../concepts/chat/concepts.md)
116122

117-
When adding the import statements, be sure to only add imports from the com.azure.communication.chat and com.azure.communication.chat.models namespaces, and not from the com.azure.communication.chat.implementation namespace. In the App.java file that was generated via Maven, you can use the following code to begin with:
123+
When adding the import statements, be sure to only add imports from the `com.azure.communication.chat` and `com.azure.communication.chat.models` namespaces, and not from the `com.azure.communication.chat.implementation` namespace. In the `App.java` file that was generated via Maven, you can use the following code to start:
118124

119125
```Java
120126
// Your unique Azure Communication service endpoint
@@ -137,8 +143,11 @@ final ChatClientBuilder builder = new ChatClientBuilder();
137143
### Start a chat thread
138144

139145
#### Twilio
140-
Creating a conversation in Twilio is straightforward using the Conversation.creator() method.
141-
- Use the `setFriendlyName` to give a topic to this chat.
146+
147+
Creating a conversation in Twilio is straightforward using the `Conversation.creator()` method.
148+
149+
Use the `setFriendlyName` to give a topic to this chat.
150+
142151
```Java
143152
// Create a new conversation
144153
Conversation conversation = Conversation.creator().setFriendlyName("New Conversation").create();
@@ -148,13 +157,13 @@ Creating a conversation in Twilio is straightforward using the Conversation.crea
148157
#### Azure Communication Services
149158

150159
Use the `createChatThread` method to create a chat thread.
151-
`createChatThreadOptions` is used to describe the thread request.
152-
153-
- Use the `topic` parameter of the constructor to give a topic to this chat; Topic can be updated after the chat thread is created using the `UpdateThread` function.
154-
- Use `participants` to list the thread participants to be added to the thread. `ChatParticipant` takes the user you created in the [User Access Token](../../identity/access-tokens.md) quickstart.
160+
- Use`createChatThreadOptions` to describe the thread request.
161+
- Use the `topic` parameter of the constructor to give a topic to this chat; update 'topic' after the chat thread is created using the `UpdateThread` function.
162+
- Use `participants` to list the thread participants to be added to the thread. `ChatParticipant` takes the user you created in the [User Access Token](../../quickstarts/identity/access-tokens.md) quickstart.
155163

156164
`CreateChatThreadResult` is the response returned from creating a chat thread.
157-
It contains a `getChatThread()` method, which returns the `ChatThread` object that can be used to get the thread client from which you can get the `ChatThreadClient` for performing operations on the created thread: add participants, send message, etc.
165+
It contains a `getChatThread()` method, which returns the `ChatThread` object that can be used to get the thread client from which you can get the `ChatThreadClient` for performing operations on the created thread: add participants, send message, and so on.
166+
158167
The `ChatThread` object also contains the `getId()` method, which retrieves the unique ID of the thread.
159168

160169
```Java
@@ -180,7 +189,9 @@ String chatThreadId = result.getChatThread().getId();
180189
### List chat threads
181190

182191
#### Twilio
183-
Here is how you list all conversations in Twilio using Java:
192+
193+
To list all conversations in Twilio using Java:
194+
184195
```Java
185196
public static void main(String[] args) {
186197
// List all conversations
@@ -211,6 +222,7 @@ chatThreads.forEach(chatThread -> {
211222
#### Twilio
212223

213224
Here’s how you can retrieve and interact with a specific conversation in Twilio using Java:
225+
214226
```Java
215227
// Retrieve a specific conversation by its SID
216228
Conversation conversation = Conversation.fetcher(conversationSid).fetch();
@@ -220,7 +232,7 @@ System.out.println("Friendly Name: " + conversation.getFriendlyName())
220232

221233
#### Azure Communication Services
222234

223-
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 participants, send message, etc.
235+
The `getChatThreadClient` method returns a thread client for a thread that already exists. Use it to perform operations on the created thread: add participants, send message, and so on.
224236
`chatThreadId` is the unique ID of the existing chat thread.
225237

226238
```Java
@@ -230,7 +242,8 @@ ChatThreadClient chatThreadClient = chatClient.getChatThreadClient(chatThreadId)
230242
### Send a message to a chat thread
231243

232244
#### Twilio
233-
Sending a message in Twilio involves using the Message.creator() method.
245+
246+
Sending a message in Twilio uses the `Message.creator()` method.
234247

235248
```Java
236249
import com.twilio.rest.conversations.v1.conversation.Message;
@@ -241,7 +254,8 @@ Message message = Message.creator(conversationSid)
241254
System.out.println("Message SID: " + message.getSid());
242255
```
243256

244-
Twilio allows you to send media files by providing a media URL when sending a message.
257+
Twilio enables you to send media files by providing a media URL when sending a message.
258+
245259
```Java
246260
List<URI> mediaUrls = Arrays.asList(URI.create("https://example.com/image.jpg"));
247261
Message message = Message.creator(conversationSid)
@@ -251,16 +265,16 @@ Message message = Message.creator(conversationSid)
251265
System.out.println("Message SID: " + message.getSid());
252266
```
253267

254-
255268
#### Azure Communication Services
256-
Unlike Twilio, Azure Communication Services does not have separate functions to send media.
257-
Use the `sendMessage` method to send a message to the thread you created, identified by chatThreadId.
258-
`sendChatMessageOptions` is used to describe the chat message request.
259269

270+
Unlike Twilio, Azure Communication Services doesn't have separate functions to send media.
271+
Use the `sendMessage` method to send a message to the thread you created, identified by `chatThreadId`.
272+
273+
Use `sendChatMessageOptions` to describe the chat message request.
260274
- Use `content` to provide the chat message content.
261-
- Use `type` to specify the chat message content type, TEXT or HTML.
275+
- Use `type` to specify the chat message content type: `TEXT` or `HTML`.
262276
- Use `senderDisplayName` to specify the display name of the sender.
263-
- 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.
277+
- Use `metadata` optionally to include any data you want to send with the message. This field enables 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 metadata so that recipient's application can parse that and display accordingly.
264278

265279
The response `sendChatMessageResult` contains an `id`, which is the unique ID of the message.
266280

@@ -282,7 +296,9 @@ String chatMessageId = sendChatMessageResult.getId();
282296
### Receive chat messages from a chat thread
283297

284298
#### Twilio
299+
285300
Twilio Conversations uses webhooks to receive messages. You typically set up a webhook URL in the Twilio console.
301+
286302
```Java
287303
// This would be handled by a servlet or similar in a Java web application
288304
public void handleIncomingMessage(HttpServletRequest request, HttpServletResponse response) {
@@ -291,7 +307,8 @@ public void handleIncomingMessage(HttpServletRequest request, HttpServletRespons
291307
}
292308
```
293309

294-
This is how you can receive media file in Twilio.
310+
To receive media file in Twilio.
311+
295312
```Java
296313
private static final Logger logger = Logger.getLogger(TwilioWebhookServlet.class.getName());
297314

@@ -330,18 +347,20 @@ chatThreadClient.listMessages().forEach(message -> {
330347
});
331348
```
332349

333-
`listMessages` returns the latest version of the message, including any edits or deletes that happened to the message using `.editMessage()` and `.deleteMessage()`. For deleted messages, `chatMessage.getDeletedOn()` returns a datetime value indicating when that message was deleted. For edited messages, `chatMessage.getEditedOn()` returns a datetime indicating when the message was edited. The original time of message creation can be accessed using `chatMessage.getCreatedOn()`, and it can be used for ordering the messages.
350+
`listMessages` returns the latest version of the message, including any edits or deletes that happened to the message using `.editMessage()` and `.deleteMessage()`. For deleted messages, `chatMessage.getDeletedOn()` returns a `datetime` value indicating when that message was deleted. For edited messages, `chatMessage.getEditedOn()` returns a datetime indicating when the message was edited. The original time of message creation can be accessed using `chatMessage.getCreatedOn()`, and it can be used for ordering the messages.
334351

335-
Read more about message types here: [Message Types](../../../concepts/chat/concepts.md#message-types).
352+
Read more about message types here: [Message Types](../../concepts/chat/concepts.md#message-types).
336353

337354
### Send read receipt
338355

339356
#### Twilio
340-
Twilio Conversations does not have a direct API for sending read receipts. Twilio Conversations manages read receipts automatically.
357+
358+
Twilio Conversations doesn't have a direct API for sending read receipts. Twilio Conversations manages read receipts automatically.
341359
342360
#### Azure Communication Services
343361
344362
Use the `sendReadReceipt` method to post a read receipt event to a chat thread, on behalf of a user.
363+
345364
`chatMessageId` is the unique ID of the chat message that was read.
346365
347366
```Java
@@ -376,7 +395,8 @@ chatParticipantsResponse.forEach(chatParticipant -> {
376395
377396
#### Twilio
378397
379-
You add participants to a conversation using the Participant.creator() method.
398+
Add participants to a conversation using the Participant.creator() method.
399+
380400
```Java
381401
import com.twilio.rest.conversations.v1.conversation.Participant;
382402
@@ -388,11 +408,11 @@ System.out.println("Participant SID: " + participant.getSid());
388408
389409
#### Azure Communication Services
390410
391-
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. You'll need to start by getting a new access token and identity for that user. Before calling 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.
411+
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. Start by getting a new access token and identity for that user. Before calling the `addParticipants` method, ensure that you acquired a new access token and identity for that user. The user needs that access token to initialize their chat client.
392412
393413
Use the `addParticipants` method to add participants to the thread.
394414
395-
- `communicationIdentifier`, required, is the CommunicationIdentifier you've created by the CommunicationIdentityClient in the [User Access Token](../../identity/access-tokens.md) quickstart.
415+
- `communicationIdentifier`, required, is the `CommunicationIdentifier` you created using `CommunicationIdentityClient` in the [User Access Token](../../quickstart/identity/access-tokens.md) quickstart.
396416
- `displayName`, optional, is the display name for the thread participant.
397417
- `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 required date.
398418

0 commit comments

Comments
 (0)