File: chat_thread_client_sample_async.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (414 lines) | stat: -rw-r--r-- 19,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: chat_thread_client_sample_async.py
DESCRIPTION:
    These samples demonstrate create a chat thread client, to update
    chat thread, get chat message, list chat messages, update chat message, send
    read receipt, list read receipts, delete chat message, add participants, remove
    participants, list participants, send typing notification
    You need to use azure.communication.configuration module to get user access
    token and user identity before run this sample

USAGE:
    python chat_thread_client_sample_async.py
    Set the environment variables with your own values before running the sample:
    1) AZURE_COMMUNICATION_SERVICE_ENDPOINT - Communication Service endpoint url
    2) TOKEN - the user access token, from token_response.token
    3) USER_ID - the user id, from token_response.identity
"""


import os
import asyncio


class ChatThreadClientSamplesAsync(object):
    from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
    from azure.communication.identity import CommunicationIdentityClient

    connection_string = os.environ.get("COMMUNICATION_SAMPLES_CONNECTION_STRING", None)
    if not connection_string:
        raise ValueError("Set COMMUNICATION_SAMPLES_CONNECTION_STRING env before run this sample.")

    identity_client = CommunicationIdentityClient.from_connection_string(connection_string)
    user = identity_client.create_user()
    tokenresponse = identity_client.get_token(user, scopes=["chat"])
    token = tokenresponse.token

    endpoint = os.environ.get("AZURE_COMMUNICATION_SERVICE_ENDPOINT", None)
    if not endpoint:
        raise ValueError("Set AZURE_COMMUNICATION_SERVICE_ENDPOINT env before run this sample.")

    _thread_id = None
    _message_id = None
    new_user = identity_client.create_user()

    _chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))

    async def create_chat_thread_client_async(self):
        token = self.token
        endpoint = self.endpoint
        user = self.user
        # [START create_chat_thread_client]
        from datetime import datetime
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
        from azure.communication.chat import ChatParticipant, CommunicationUserIdentifier

        # set `endpoint` to an existing ACS endpoint
        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))

        async with chat_client:
            topic = "test topic"
            participants = [ChatParticipant(identifier=user, display_name="name", share_history_time=datetime.utcnow())]
            create_chat_thread_result = await chat_client.create_chat_thread(topic, thread_participants=participants)
            chat_thread_client = chat_client.get_chat_thread_client(create_chat_thread_result.chat_thread.id)
        # [END create_chat_thread_client]

        self._thread_id = create_chat_thread_result.chat_thread.id
        print("thread created, id: " + self._thread_id)
        print("create_chat_thread_client_async succeeded")

    async def get_chat_thread_properties_async(self):
        thread_id = self._thread_id
        token = self.token
        endpoint = self.endpoint
        # [START get_thread]
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

        # set `endpoint` to an existing ACS endpoint
        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
        async with chat_client:
            chat_thread_client = chat_client.get_chat_thread_client(thread_id)

            async with chat_thread_client:
                chat_thread_properties = chat_thread_client.get_properties()
                print("Expected Thread Id: ", thread_id, " Actual Value: ", chat_thread_properties.id)
            # [END get_thread]
            print(
                "get_chat_thread_properties_async succeeded, thread id: "
                + chat_thread.id
                + ", thread topic: "
                + chat_thread.topic
            )

    async def update_topic_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client

        # [START update_topic]
        # set `thread_id` to an existing thread id
        async with chat_client:
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)

            async with chat_thread_client:
                chat_thread_properties = await chat_thread_client.get_properties()
                previous_topic = chat_thread_properties.topic

                topic = "updated thread topic"
                await chat_thread_client.update_topic(topic=topic)

                chat_thread_properties = await chat_thread_client.get_properties()
                updated_topic = chat_thread_properties.topic

                print("Chat Thread Topic Update: Previous value: ", previous_topic, ", Current value: ", updated_topic)
        # [END update_topic]

        print("update_topic_async succeeded")

    async def send_message_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client

        # [START send_message]
        from azure.communication.chat import ChatMessageType

        async with chat_client:
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                # Scenario 1: Send message without specifying chat_message_type
                send_message_result = await chat_thread_client.send_message(
                    "Hello! My name is Fred Flinstone", sender_display_name="Fred Flinstone", metadata={"tags": "tags"}
                )
                send_message_result_id = send_message_result.id

                # Scenario 2: Send message specifying chat_message_type
                send_message_result_w_type = await chat_thread_client.send_message(
                    "Hello! My name is Wilma Flinstone",
                    sender_display_name="Wilma Flinstone",
                    chat_message_type=ChatMessageType.TEXT,
                )  # equivalent to setting chat_message_type='text'
                send_message_result_w_type_id = send_message_result_w_type.id

                # Verify message content
                chat_message_1 = await chat_thread_client.get_message(send_message_result_id)
                print("First Message:", chat_message_1.content.message, chat_message_1.metadata)
                print(
                    "Second Message:",
                    (await chat_thread_client.get_message(send_message_result_w_type_id)).content.message,
                )
                # [END send_message]
                self._message_id = send_message_result_id
            print("send_message succeeded, message id:", self._message_id)
            print("send_message succeeded with type specified, message id:", send_message_result_w_type_id)
        print("send_message_async succeeded")

    async def get_message_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        message_id = self._message_id

        # [START get_message]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                # set `message_id` to an existing message id
                chat_message = await chat_thread_client.get_message(message_id)

                print("Message received: ChatMessage: content=", chat_message.content.message, ", id=", chat_message.id)
        # [END get_message]
        print("get_message_async succeeded")

    async def list_messages_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client

        # [START list_messages]
        from datetime import datetime, timedelta

        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                start_time = datetime.utcnow() - timedelta(days=1)
                chat_messages = chat_thread_client.list_messages(results_per_page=1, start_time=start_time)
                print("list_messages succeeded with results_per_page is 1, and start time is yesterday UTC")
                async for chat_message_page in chat_messages.by_page():
                    async for chat_message in chat_message_page:
                        print("ChatMessage: message=", chat_message.content.message)
                        for attachment in chat_message.content.attachments:
                            if attachment.attachment_type == "image":
                                print("image attachment: ", attachment.name, " with ID: ", attachment.id, "received.")
                                # render `attachment.preview_url` as the thumbnail
                                # render `attachment.url` as the full image
                            elif attachment.attachment_type == "file":
                                print("file attachment: ", attachment.name, " with ID: ", attachment.id, "received.")
                                # render a button that will navigate user to the URL provided in `attachment.preview_url`
        # [END list_messages]
        print("list_messages_async succeeded")

    async def update_message_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        message_id = self._message_id

        # [START update_message]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                # set `message_id` to an existing message id
                previous_content = (await chat_thread_client.get_message(message_id)).content.message

                content = "updated message content"
                await chat_thread_client.update_message(self._message_id, content=content)

                current_content = (await chat_thread_client.get_message(message_id)).content.message

                print("Chat Message Updated: Previous value: ", previous_content, ", Current value: ", current_content)
        # [END update_message]
        print("update_message_async succeeded")

    async def send_read_receipt_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        message_id = self._message_id
        # [START send_read_receipt]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                # set `message_id` to an existing message id
                await chat_thread_client.send_read_receipt(message_id)
        # [END send_read_receipt]

        print("send_read_receipt_async succeeded")

    async def list_read_receipts_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client

        # [START list_read_receipts]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                read_receipts = chat_thread_client.list_read_receipts()
                print("list_read_receipts succeeded, receipts:")
                async for read_receipt_page in read_receipts.by_page():
                    async for read_receipt in read_receipt_page:
                        print(read_receipt)
        # [END list_read_receipts]
        print("list_read_receipts_async succeeded")

    async def delete_message_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        message_id = self._message_id
        # [START delete_message]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                # set `message_id` to an existing message id
                await chat_thread_client.delete_message(message_id)
        # [END delete_message]
        print("delete_message_async succeeded")

    async def list_participants_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        # [START list_participants]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                chat_thread_participants = chat_thread_client.list_participants()
                print("list_participants succeeded, participants:")
                async for chat_thread_participant_page in chat_thread_participants.by_page():
                    async for chat_thread_participant in chat_thread_participant_page:
                        print("ChatParticipant: ", chat_thread_participant)
        # [END list_participants]
        print("list_participants_async succeeded")

    async def add_participants_w_check_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        user = self.new_user

        # [START add_participants]
        def decide_to_retry(error):
            """
            Custom logic to decide whether to retry to add or not
            """
            return True

        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                from azure.communication.chat import ChatParticipant
                from datetime import datetime

                new_participant = ChatParticipant(
                    identifier=self.new_user, display_name="name", share_history_time=datetime.utcnow()
                )
                thread_participants = [new_participant]
                result = await chat_thread_client.add_participants(thread_participants)

                # list of participants which were unsuccessful to be added to chat thread
                retry = [p for p, e in result if decide_to_retry(e)]
                if retry:
                    await chat_thread_client.add_participants(retry)

        # [END add_participants]
        print("add_participants_w_check_async succeeded")

    async def remove_participant_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        identity_client = self.identity_client

        from azure.communication.chat import ChatParticipant, CommunicationUserIdentifier

        from datetime import datetime

        async with chat_client:
            # create 2 new users using CommunicationIdentityClient.create_user method
            user1 = identity_client.create_user()
            user2 = identity_client.create_user()

            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)

            async with chat_thread_client:
                # add user1 and user2 to chat thread
                participant1 = ChatParticipant(
                    identifier=user1, display_name="Fred Flinstone", share_history_time=datetime.utcnow()
                )

                participant2 = ChatParticipant(
                    identifier=user2, display_name="Wilma Flinstone", share_history_time=datetime.utcnow()
                )

                thread_participants = [participant1, participant2]
                await chat_thread_client.add_participants(thread_participants)
                # [START remove_participant]
                # Option 1 : Iterate through all participants, find and delete Fred Flinstone
                chat_thread_participants = chat_thread_client.list_participants()

                async for chat_thread_participant_page in chat_thread_participants.by_page():
                    async for chat_thread_participant in chat_thread_participant_page:
                        print("ChatParticipant: ", chat_thread_participant)
                        if chat_thread_participant.identifier.properties["id"] == user1.properties["id"]:
                            print("Found Fred!")
                            await chat_thread_client.remove_participant(chat_thread_participant.identifier)
                            print("Fred has been removed from the thread...")
                            break

                # Option 2: Directly remove Wilma Flinstone
                unique_identifier = user2.properties[
                    "id"
                ]  # in real scenario the identifier would need to be retrieved from elsewhere
                await chat_thread_client.remove_participant(CommunicationUserIdentifier(unique_identifier))
                print("Wilma has been removed from the thread...")
                # [END remove_participant]

        # clean up temporary users
        self.identity_client.delete_user(user1)
        self.identity_client.delete_user(user2)
        print("remove_participant_async succeeded")

    async def send_typing_notification_async(self):
        thread_id = self._thread_id
        chat_client = self._chat_client
        # [START send_typing_notification]
        async with chat_client:
            # set `thread_id` to an existing thread id
            chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)
            async with chat_thread_client:
                await chat_thread_client.send_typing_notification()
        # [END send_typing_notification]
        print("send_typing_notification_async succeeded")

    def clean_up(self):
        print("cleaning up: deleting created users.")
        self.identity_client.delete_user(self.user)
        self.identity_client.delete_user(self.new_user)


async def main():
    sample = ChatThreadClientSamplesAsync()
    await sample.create_chat_thread_client_async()
    await sample.update_topic_async()
    await sample.send_message_async()
    await sample.get_message_async()
    await sample.list_messages_async()
    await sample.update_message_async()
    await sample.send_read_receipt_async()
    await sample.list_read_receipts_async()
    await sample.delete_message_async()
    await sample.add_participants_w_check_async()
    await sample.list_participants_async()
    await sample.remove_participant_async()
    await sample.send_typing_notification_async()
    sample.clean_up()


if __name__ == "__main__":
    asyncio.run(main())