Skip to content

Commit 07a4ae7

Browse files
authored
Merge pull request #155360 from Ja-Dunn/chat-python
edit pass: chat-python
2 parents dffc06d + a4a58fd commit 07a4ae7

File tree

1 file changed

+41
-42
lines changed
  • articles/communication-services/quickstarts/chat/includes

1 file changed

+41
-42
lines changed

articles/communication-services/quickstarts/chat/includes/chat-python.md

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ ms.author: mikben
1818
Before you get started, make sure to:
1919

2020
- 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).
21-
- Install [Python](https://www.python.org/downloads/)
22-
- Create an Azure Communication Services resource. For details, see [Create an Azure Communication Resource](../../create-communication-resource.md). You'll need to record your resource **endpoint** for this quickstart
23-
- A [User Access Token](../../access-tokens.md). Be sure to set the scope to "chat", and note the token string as well as the userId string.
21+
- Install [Python](https://www.python.org/downloads/).
22+
- Create an Azure Communication Services resource. For details, see [Quickstart: Create and manage Communication Services resources](../../create-communication-resource.md). You'll need to record your resource endpoint for this quickstart.
23+
- A [user access token](../../access-tokens.md). Be sure to set the scope to `chat`, and note the `token` string as well as the `userId` string.
2424

2525
## Setting up
2626

2727
### Create a new Python application
2828

29-
Open your terminal or command window create a new directory for your app, and navigate to it.
29+
Open your terminal or command window, create a new directory for your app, and go to it.
3030

3131
```console
3232
mkdir chat-quickstart && cd chat-quickstart
3333
```
3434

35-
Use a text editor to create a file called **start-chat.py** in the project root directory and add the structure for the program, including basic exception handling. You'll add all the source code for this quickstart to this file in the following sections.
35+
Use a text editor to create a file called *start-chat.py* in the project root directory. Add the structure for the program, including basic exception handling. In the following sections, you'll add all the source code for this quickstart to this file.
3636

3737
```python
3838
import os
@@ -48,6 +48,8 @@ except Exception as ex:
4848

4949
### Install SDK
5050

51+
Use the following command to install the SDK:
52+
5153
```console
5254

5355
pip install azure-communication-chat
@@ -60,14 +62,12 @@ The following classes and interfaces handle some of the major features of the Az
6062

6163
| Name | Description |
6264
| ------------------------------------- | ------------------------------------------------------------ |
63-
| 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. |
64-
| 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. |
65+
| `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. |
66+
| `ChatThreadClient` | This class is needed for the chat thread functionality. You obtain an instance via `ChatClient`, and use it to send, receive, update, and delete messages. You can also use it to add, remove, and get users, and send typing notifications and read receipts. |
6567

6668
## Create a chat client
6769

68-
To create a chat client, you'll use Communications Service endpoint and the `Access Token` that was generated as part of pre-requisite steps. Learn more about [User Access Tokens](../../access-tokens.md).
69-
70-
This quickstart does not cover creating a service tier to manage tokens for your chat application, although it is recommended. See the following documentation for more detail [Chat Architecture](../../../concepts/chat/concepts.md)
70+
To create a chat client, use the Communication Services endpoint and the access token you generated as part of prerequisite steps.
7171

7272
```console
7373
pip install azure-communication-identity
@@ -79,18 +79,16 @@ from azure.communication.chat import ChatClient, CommunicationTokenCredential
7979
endpoint = "https://<RESOURCE_NAME>.communication.azure.com"
8080
chat_client = ChatClient(endpoint, CommunicationTokenCredential("<Access Token>"))
8181
```
82+
This quickstart doesn't cover creating a service tier to manage tokens for your chat application, but that's recommended. For more information, see the "Chat architecture" section of [Chat concepts](../../../concepts/chat/concepts.md).
8283

8384
## Start a chat thread
8485

8586
Use the `create_chat_thread` method to create a chat thread.
8687

87-
- Use `topic` to give a thread topic; Topic can be updated after the chat thread is created using the `update_thread` function.
88-
- Use `thread_participants` to list the `ChatParticipant` to be added to the chat thread, the `ChatParticipant` takes `CommunicationUserIdentifier` type as `user`, which is what you got after you
89-
created by [Create a user](../../access-tokens.md#create-an-identity)
88+
- Use `topic` to give the thread a topic. You can update the topic after the chat thread is created by using the `update_thread` function.
89+
- Use `thread_participants` to list the `ChatParticipant` to be added to the chat thread. The `ChatParticipant` takes the `CommunicationUserIdentifier` type as `user`.
9090

91-
`CreateChatThreadResult` is the result returned from creating a thread, you can use it to fetch the `id` of
92-
the chat thread that got created. This `id` can then be used to fetch a `ChatThreadClient` object using
93-
the `get_chat_thread_client` method. `ChatThreadClient` can be used to perform other chat operations to this chat thread.
91+
`CreateChatThreadResult` is the result returned from creating a thread. You can use it to fetch the `id` of the chat thread that got created. This `id` can then be used to fetch a `ChatThreadClient` object by using the `get_chat_thread_client` method. You can use `ChatThreadClient` to perform other chat operations to this chat thread.
9492

9593
```python
9694
topic="test topic"
@@ -100,23 +98,24 @@ chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_resul
10098
```
10199

102100
## Get a chat thread client
103-
The `get_chat_thread_client` 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. thread_id is the unique ID of the existing chat thread.
104101

105-
`ChatThreadClient` can be used to perform other chat operations to this chat thread.
102+
The `get_chat_thread_client` method returns a thread client for a thread that already exists. You can use it to perform operations on the created thread. For example, you can add participants and send messages. `thread_id` is the unique ID of the existing chat thread.
103+
104+
You can use `ChatThreadClient` to perform other chat operations to this chat thread.
106105

107106
```python
108107
thread_id = create_chat_thread_result.chat_thread.id
109108
chat_thread_client = chat_client.get_chat_thread_client(thread_id)
110109
```
111110

112-
113111
## List all chat threads
114-
The `list_chat_threads` method returns a iterator of type `ChatThreadItem`. It can be used for listing all chat threads.
115112

116-
- Use `start_time` to specify the earliest point in time to get chat threads up to.
113+
The `list_chat_threads` method returns an iterator of type `ChatThreadItem`.
114+
115+
- Use `start_time` to specify the earliest point in time to get chat threads.
117116
- Use `results_per_page` to specify the maximum number of chat threads returned per page.
118117

119-
An iterator of `[ChatThreadItem]` is the response returned from listing threads
118+
An iterator of `[ChatThreadItem]` is the response returned from listing threads.
120119

121120
```python
122121
from datetime import datetime, timedelta
@@ -133,13 +132,13 @@ for chat_thread_item_page in chat_threads.by_page():
133132

134133
## Send a message to a chat thread
135134

136-
Use `send_message` method to send a message to a chat thread you just created, identified by thread_id.
135+
Use the `send_message` method to send a message to a chat thread you just created, identified by `thread_id`.
137136

138-
- Use `content` to provide the chat message content;
139-
- Use `chat_message_type` to specify the message content type. Possible values are 'text' and 'html'; if not specified default value of 'text' is assigned.
140-
- Use `sender_display_name` to specify the display name of the sender;
137+
- Use `content` to provide the chat message content.
138+
- Use `chat_message_type` to specify the message content type. Possible values are `text` and `html`. If you don't specify a value, the default is `text`.
139+
- Use `sender_display_name` to specify the display name of the sender.
141140

142-
`SendChatMessageResult` is the response returned from sending a message, it contains an ID, which is the unique ID of the message.
141+
`SendChatMessageResult` is the response returned from sending a message. It contains an ID, which is the unique ID of the message.
143142

144143
```python
145144
from azure.communication.chat import ChatMessageType
@@ -164,9 +163,9 @@ print("Message sent: id: ", send_message_result_w_enum.id)
164163
You can retrieve chat messages by polling the `list_messages` method at specified intervals.
165164

166165
- Use `results_per_page` to specify the maximum number of messages to be returned per page.
167-
- Use `start_time` to specify the earliest point in time to get messages up to.
166+
- Use `start_time` to specify the earliest point in time to get messages.
168167

169-
An iterator of `[ChatMessage]` is the response returned from listing messages
168+
An iterator of `[ChatMessage]` is the response returned from listing messages.
170169

171170
```python
172171
from datetime import datetime, timedelta
@@ -179,14 +178,15 @@ for chat_message_page in chat_messages.by_page():
179178
print("ChatMessage: Id=", chat_message.id, "; Content=", chat_message.content.message)
180179
```
181180

182-
`list_messages` returns the latest version of the message, including any edits or deletes that happened to the message using `update_message` and `delete_message`. For deleted messages `ChatMessage.deleted_on` returns a datetime value indicating when that message was deleted. For edited messages, `ChatMessage.edited_on` returns a datetime indicating when the message was edited. The original time of message creation can be accessed using `ChatMessage.created_on` which can be used for ordering the messages.
181+
`list_messages` returns the latest version of the message, including any edits or deletes that happened to the message by using `update_message` and `delete_message`. For deleted messages, `ChatMessage.deleted_on` returns a `datetime` value indicating when that message was deleted. For edited messages, `ChatMessage.edited_on` returns a `datetime` value indicating when the message was edited. You can access the original time of message creation by using `ChatMessage.created_on`, which can be used for ordering the messages.
183182

184-
`list_messages` returns different types of messages which can be identified by `ChatMessage.type`.
183+
`list_messages` returns different types of messages, which can be identified by `ChatMessage.type`.
185184

186-
Read more about Message Types here: [Message Types](../../../concepts/chat/concepts.md#message-types).
185+
For more information, see [Message types](../../../concepts/chat/concepts.md#message-types).
187186

188187
## Send read receipt
189-
The `send_read_receipt` method can be used to posts a read receipt event to a thread, on behalf of a user.
188+
189+
You use the `send_read_receipt` method to post a read receipt event to a thread, on behalf of a user.
190190

191191
- Use `message_id` to specify the ID of the latest message read by current user.
192192

@@ -200,13 +200,11 @@ chat_thread_client.send_read_receipt(message_id=send_message_result.id)
200200

201201
## Add a user as a participant to the chat thread
202202

203-
Once a chat 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 chat thread, and add/remove other participants. Before calling `add_participants` method, 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.
203+
When you create a chat thread, you can then add and remove users from it. By adding users, you give them access to be able to send messages to the chat thread, and add or remove other participants. Before calling the `add_participants` method, ensure that you have acquired a new access token and identity for that user. The user needs that access token to initialize the chat client.
204204

205-
One or more users can be added to the chat thread using the `add_participants` method, provided new access token and identify is available for all users.
205+
You can add one or more users to the chat thread by using the `add_participants` method, provided that a new access token and identity is available for all users.
206206

207-
A `list(tuple(ChatParticipant, CommunicationError))` is returned. When participant is successfully added,
208-
an empty list is expected. In case of an error encountered while adding participant, the list is populated
209-
with the failed participants along with the error that was encountered.
207+
A `list(tuple(ChatParticipant, CommunicationError))` is returned. When the participant is successfully added, an empty list is expected. If you encounter an error while adding a participant, the list is populated with the failed participants, along with the error that was encountered.
210208

211209
```python
212210
from azure.communication.identity import CommunicationIdentityClient
@@ -257,11 +255,12 @@ if retry:
257255

258256
Similar to adding a participant, you can also list participants from a thread.
259257

260-
Use `list_participants` to retrieve the participants of the thread.
261-
- Use `results_per_page`, optional, The maximum number of participants to be returned per page.
262-
- Use `skip`, optional, to skips participants up to a specified position in response.
258+
Use `list_participants` to retrieve the participants of the thread. Both of the following commands are optional:
259+
260+
- Use `results_per_page` to specify the maximum number of participants to be returned per page.
261+
- Use `skip` to skip participants up to a specified position in the response.
263262

264-
An iterator of `[ChatParticipant]` is the response returned from listing participants
263+
An iterator of `[ChatParticipant]` is the response returned from listing participants.
265264

266265
```python
267266
chat_thread_participants = chat_thread_client.list_participants()

0 commit comments

Comments
 (0)