你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

ChatClient Class

A client to interact with the AzureCommunicationService Chat gateway.

This client provides operations to create chat thread, delete chat thread, get chat thread client by thread id, list chat threads.

Constructor

ChatClient(endpoint: str, credential: CommunicationTokenCredential, **kwargs: Any)

Parameters

Name Description
endpoint
Required
str

The endpoint of the Azure Communication resource.

credential
Required

The credentials with which to authenticate.

Examples

Creating the ChatClient from a URL and token.


   from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

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

Methods

close
create_chat_thread

Creates a chat thread.

delete_chat_thread

Deletes a chat thread.

get_chat_thread_client

Get ChatThreadClient by providing a thread_id.

list_chat_threads

Gets the list of chat threads of a user.

close

async close() -> None

create_chat_thread

Creates a chat thread.

async create_chat_thread(topic: str, **kwargs) -> CreateChatThreadResult

Parameters

Name Description
topic
Required
str

Required. The thread topic.

Keyword-Only Parameters

Name Description
thread_participants

Optional. Participants to be added to the thread.

idempotency_token
str

Optional. If specified, the client directs that the request is repeatable; that is, the client can make the request multiple times with the same Idempotency_Token and get back an appropriate response without the server executing the request multiple times. The value of the Idempotency_Token is an opaque string representing a client-generated, globally unique for all time, identifier for the request. If not specified, a new unique id would be generated.

Returns

Type Description

CreateChatThreadResult

Exceptions

Type Description
<xref:azure.core.exceptions.HttpResponseError>, <xref:ValueError>

Examples

Creating a new chat thread.


   from datetime import datetime
   from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
   from azure.communication.chat import ChatParticipant

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

       topic = "test topic"
       participants = [ChatParticipant(
           identifier=self.user,
           display_name='name',
           share_history_time=datetime.utcnow()
       )]
       # creates a new chat_thread everytime
       create_chat_thread_result = await chat_client.create_chat_thread(topic, thread_participants=participants)

       # creates a new chat_thread if not exists
       idempotency_token = 'b66d6031-fdcc-41df-8306-e524c9f226b8'  # unique identifier
       create_chat_thread_result_w_repeatability_id = await chat_client.create_chat_thread(
           topic,
           thread_participants=participants,
           idempotency_token=idempotency_token)

delete_chat_thread

Deletes a chat thread.

async delete_chat_thread(thread_id: str, **kwargs) -> None

Parameters

Name Description
thread_id
Required
str

Required. Thread id to delete.

Returns

Type Description

None

Exceptions

Type Description
<xref:azure.core.exceptions.HttpResponseError>, <xref:ValueError>

Examples

Deleting a chat 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:
       # set `thread_id` to an existing chat thread id
       await chat_client.delete_chat_thread(thread_id)

get_chat_thread_client

Get ChatThreadClient by providing a thread_id.

get_chat_thread_client(thread_id: str, **kwargs: Any) -> ChatThreadClient

Parameters

Name Description
thread_id
Required
str

Required. The thread id.

Returns

Type Description

ChatThreadClient

Exceptions

Type Description
<xref:azure.core.exceptions.HttpResponseError>, <xref:ValueError>

Examples

Retrieving the ChatThreadClient from an existing chat thread id.


   from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

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

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

list_chat_threads

Gets the list of chat threads of a user.

list_chat_threads(**kwargs: Any) -> AsyncItemPaged[ChatThreadItem]

Keyword-Only Parameters

Name Description
results_per_page
int

The maximum number of chat threads to be returned per page.

start_time

The earliest point in time to get chat threads up to.

Returns

Type Description

An iterator like instance of ChatThreadItem

Exceptions

Type Description
<xref:azure.core.exceptions.HttpResponseError>, <xref:ValueError>

Examples

Listing chat threads.


       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:

           from datetime import datetime, timedelta
           start_time = datetime.utcnow() - timedelta(days=2)
           chat_threads = chat_client.list_chat_threads(results_per_page=5, start_time=start_time)
           print("list_threads succeeded with results_per_page is 5, and were created since 2 days ago.")
           async for chat_thread_item_page in chat_threads.by_page():
               async for chat_thread_item in chat_thread_item_page:
                   print("thread id: ", chat_thread_item.id)