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.

Inheritance
builtins.object
ChatClient

Constructor

ChatClient(endpoint: str, credential: azure.communication.chat._shared.user_credential_async.CommunicationTokenCredential, **kwargs: Any)

Parameters

endpoint
str
Required

The endpoint of the Azure Communication resource.

credential
CommunicationTokenCredential
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) -> azure.communication.chat._models.CreateChatThreadResult

Parameters

topic
str
Required

Required. The thread topic.

thread_participants
<xref:List>[ChatParticipant]

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

CreateChatThreadResult

Return type

Exceptions

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

Examples

Creating a new chat thread.


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

   t `endpoint` to an existing ACS endpoint
   _client = ChatClient(endpoint, CommunicationTokenCredential(token))
   c 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

thread_id
str
Required

Required. Thread id to delete.

Returns

None

Return type

Exceptions

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

Examples

Deleting a chat thread.


    azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

   t `endpoint` to an existing ACS endpoint
   _client = ChatClient(endpoint, CommunicationTokenCredential(token))
   c 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) -> azure.communication.chat.aio._chat_thread_client_async.ChatThreadClient

Parameters

thread_id
str
Required

Required. The thread id.

Returns

ChatThreadClient

Return type

Exceptions

<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) -> azure.core.async_paging.AsyncItemPaged[azure.communication.chat._generated.models._models_py3.ChatThreadItem]

Parameters

results_per_page
int

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

start_time
datetime

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

Returns

An iterator like instance of ChatThreadItem

Return type

Exceptions

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

Examples

Listing chat threads.


    azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

   t `endpoint` to an existing ACS endpoint
   _client = ChatClient(endpoint, CommunicationTokenCredential(token))
   c 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)