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
|
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 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
close() -> None
create_chat_thread
Creates a chat thread.
create_chat_thread(topic: str, **kwargs: Any) -> CreateChatThreadResult
Parameters
| Name | Description |
|---|---|
|
topic
Required
|
Required. The thread topic. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
thread_participants
|
Optional. Participants to be added to the thread. |
|
idempotency_token
|
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 import(
ChatClient,
ChatParticipant,
CommunicationUserIdentifier,
CommunicationTokenCredential
)
# set `endpoint` to an existing ACS endpoint
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
topic = "test topic"
participants = [ChatParticipant(
identifier=user,
display_name='name',
share_history_time=datetime.utcnow()
)]
# creates a new chat_thread everytime
create_chat_thread_result = 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 = chat_client.create_chat_thread(
topic,
thread_participants=participants,
idempotency_token=idempotency_token
)
delete_chat_thread
Deletes a chat thread.
delete_chat_thread(thread_id: str, **kwargs: Any) -> None
Parameters
| Name | Description |
|---|---|
|
thread_id
Required
|
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 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_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
|
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 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) -> ItemPaged[ChatThreadItem]
Keyword-Only Parameters
| Name | Description |
|---|---|
|
results_per_page
|
The maximum number of chat threads 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 import ChatClient, CommunicationTokenCredential
from datetime import datetime, timedelta
# set `endpoint` to an existing ACS endpoint
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
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.")
for chat_thread_item_page in chat_threads.by_page():
for chat_thread_item in chat_thread_item_page:
print("thread id:", chat_thread_item.id)