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/quickstarts/chat/includes/chat-python.md
+41-42Lines changed: 41 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,21 +18,21 @@ ms.author: mikben
18
18
Before you get started, make sure to:
19
19
20
20
- 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 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.
- 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.
24
24
25
25
## Setting up
26
26
27
27
### Create a new Python application
28
28
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.
30
30
31
31
```console
32
32
mkdir chat-quickstart && cd chat-quickstart
33
33
```
34
34
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.
36
36
37
37
```python
38
38
import os
@@ -48,6 +48,8 @@ except Exception as ex:
48
48
49
49
### Install SDK
50
50
51
+
Use the following command to install the SDK:
52
+
51
53
```console
52
54
53
55
pip install azure-communication-chat
@@ -60,14 +62,12 @@ The following classes and interfaces handle some of the major features of the Az
| 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. |
65
67
66
68
## Create a chat client
67
69
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.
71
71
72
72
```console
73
73
pip install azure-communication-identity
@@ -79,18 +79,16 @@ from azure.communication.chat import ChatClient, CommunicationTokenCredential
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).
82
83
83
84
## Start a chat thread
84
85
85
86
Use the `create_chat_thread` method to create a chat thread.
86
87
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`.
90
90
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.
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.
104
101
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.
The `list_chat_threads` method returns a iterator of type `ChatThreadItem`. It can be used for listing all chat threads.
115
112
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.
117
116
- Use `results_per_page` to specify the maximum number of chat threads returned per page.
118
117
119
-
An iterator of `[ChatThreadItem]` is the response returned from listing threads
118
+
An iterator of `[ChatThreadItem]` is the response returned from listing threads.
120
119
121
120
```python
122
121
from datetime import datetime, timedelta
@@ -133,13 +132,13 @@ for chat_thread_item_page in chat_threads.by_page():
133
132
134
133
## Send a message to a chat thread
135
134
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`.
137
136
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.
141
140
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.
143
142
144
143
```python
145
144
from azure.communication.chat import ChatMessageType
`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 datetimeindicating 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.
183
182
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`.
185
184
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).
187
186
188
187
## 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.
190
190
191
191
- Use `message_id` to specify the ID of the latest message read by current user.
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.
204
204
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.
206
206
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.
210
208
211
209
```python
212
210
from azure.communication.identity import CommunicationIdentityClient
@@ -257,11 +255,12 @@ if retry:
257
255
258
256
Similar to adding a participant, you can also list participants from a thread.
259
257
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.
263
262
264
-
An iterator of `[ChatParticipant]` is the response returned from listing participants
263
+
An iterator of `[ChatParticipant]` is the response returned from listing participants.
0 commit comments