azure.communication.chat.aio package¶
-
class
azure.communication.chat.aio.
ChatClient
(endpoint: str, credential: azure.communication.chat._shared.user_credential_async.CommunicationUserCredential, **kwargs)[source]¶ A client to interact with the AzureCommunicationService Chat gateway.
This client provides operations to create a chat thread, delete a chat thread, get chat thread by id, list chat threads.
- Parameters
endpoint (str) – The endpoint of the Azure Communication resource.
credential (CommunicationUserCredential) – The credentials with which to authenticate.
Example:
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token))
-
async
create_chat_thread
(topic: str, thread_members: List[azure.communication.chat._models.ChatThreadMember], **kwargs) → azure.communication.chat.aio._chat_thread_client_async.ChatThreadClient[source]¶ Creates a chat thread.
- Parameters
topic (str) – Required. The thread topic.
thread_members (list[ChatThreadMember]) – Required. Members to be added to the thread.
- Returns
ChatThreadClient
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
topic = "test topic" members = [ChatThreadMember( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] chat_thread_client = await chat_client.create_chat_thread(topic, members)
-
async
delete_chat_thread
(thread_id: str, **kwargs) → None[source]¶ Deletes a thread.
- Parameters
thread_id (str) – Required. Thread id to delete.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
await chat_client.delete_chat_thread(self._thread_id)
-
async
get_chat_thread
(thread_id: str, **kwargs) → azure.communication.chat._models.ChatThread[source]¶ Gets a chat thread.
- Parameters
thread_id (str) – Required. Thread id to get.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
ChatThread, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
chat_thread = await chat_client.get_chat_thread(self._thread_id)
-
get_chat_thread_client
(thread_id: str, **kwargs) → azure.communication.chat.aio._chat_thread_client_async.ChatThreadClient[source]¶ Get ChatThreadClient by providing a thread_id.
- Parameters
thread_id (str) – Required. The thread id.
- Returns
ChatThreadClient
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
from azure.communication.chat.aio import ChatClient, CommunicationUserCredential chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token)) chat_thread_client = chat_client.get_chat_thread_client(self._thread_id)
-
list_chat_threads
(**kwargs) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._generated.models._models_py3.ChatThreadInfo][source]¶ Gets the list of chat threads of a user.
- Keyword Arguments
- Returns
AsyncItemPaged[
ChatThreadInfo
]- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
from datetime import datetime, timedelta import pytz start_time = datetime.utcnow() - timedelta(days=2) start_time = start_time.replace(tzinfo=pytz.utc) chat_thread_infos = 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 info in chat_thread_infos: print("thread id: ", info.id)
-
class
azure.communication.chat.aio.
ChatThreadClient
(endpoint: str, credential: azure.communication.chat._shared.user_credential_async.CommunicationUserCredential, thread_id: str, **kwargs)[source]¶ A client to interact with the AzureCommunicationService Chat gateway. Instances of this class is normally created by ChatClient.create_chat_thread()
This client provides operations to add member to chat thread, remove member from chat thread, send message, delete message, update message, send typing notifications, send and list read receipt
- Variables
- Parameters
endpoint (str) – The endpoint of the Azure Communication resource.
credential (CommunicationUserCredential) – The credentials with which to authenticate. The value contains a User Access Token
thread_id (str) – The unique thread id.
Example:
from datetime import datetime from azure.communication.chat.aio import ChatClient, CommunicationUserCredential from azure.communication.chat import ChatThreadMember, CommunicationUser chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token)) async with chat_client: topic = "test topic" members = [ChatThreadMember( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] chat_thread_client = await chat_client.create_chat_thread(topic, members)
-
async
add_members
(thread_members: List[azure.communication.chat._models.ChatThreadMember], **kwargs) → None[source]¶ Adds thread members to a thread. If members already exist, no change occurs.
- Parameters
thread_members (list[ChatThreadMember]) – Required. Thread members to be added to the thread.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
from azure.communication.chat import ChatThreadMember, CommunicationUser from datetime import datetime new_member = ChatThreadMember( user=self.new_user, display_name='name', share_history_time=datetime.utcnow()) members = [new_member] await chat_thread_client.add_members(members)
-
async
delete_message
(message_id: str, **kwargs) → None[source]¶ Deletes a message.
- Parameters
message_id (str) – Required. The message id.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
await chat_thread_client.delete_message(self._message_id)
-
async
get_message
(message_id: str, **kwargs) → azure.communication.chat._models.ChatMessage[source]¶ Gets a message by id.
- Parameters
message_id (str) – Required. The message id.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
ChatMessage, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
chat_message = await chat_thread_client.get_message(self._message_id)
-
list_members
(**kwargs) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._models.ChatThreadMember][source]¶ Gets the members of a thread.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
AsyncItemPaged[
ChatThreadMember
]- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
chat_thread_members = chat_thread_client.list_members() print("list_members succeeded, members:") async for chat_thread_member in chat_thread_members: print(chat_thread_member)
-
list_messages
(**kwargs) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._models.ChatMessage][source]¶ Gets a list of messages from a thread.
- Keyword Arguments
- Returns
AsyncItemPaged[
ChatMessage
]- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
from datetime import datetime, timedelta 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(): l = [ i async for i in chat_message_page] print("page size: ", len(l))
-
list_read_receipts
(**kwargs) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._models.ReadReceipt][source]¶ Gets read receipts for a thread.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
AsyncItemPaged[
ReadReceipt
]- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
read_receipts = chat_thread_client.list_read_receipts()
-
async
remove_member
(user: azure.communication.chat._shared.models.CommunicationUser, **kwargs) → None[source]¶ Remove a member from a thread.
- Parameters
user (CommunicationUser) – Required. User identity of the thread member to remove from the thread.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
await chat_thread_client.remove_member(self.new_user)
-
async
send_message
(content: str, **kwargs) → azure.communication.chat._generated.models._models_py3.SendChatMessageResult[source]¶ Sends a message to a thread.
- Parameters
content (str) – Required. Chat message content.
- Keyword Arguments
priority (str or ChatMessagePriority) – Message priority.
sender_display_name (str) – The display name of the message sender. This property is used to populate sender name for push notifications.
cls (callable) – A custom type or function that will be passed the direct response
- Returns
SendChatMessageResult, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
from azure.communication.chat import ChatMessagePriority priority=ChatMessagePriority.NORMAL content='hello world' sender_display_name='sender name' send_message_result = await chat_thread_client.send_message( content, priority=priority, sender_display_name=sender_display_name)
-
async
send_read_receipt
(message_id: str, **kwargs) → None[source]¶ Posts a read receipt event to a thread, on behalf of a user.
- Parameters
message_id (str) – Required. Id of the latest message read by current user.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
await chat_thread_client.send_read_receipt(self._message_id)
-
async
send_typing_notification
(**kwargs) → None[source]¶ Posts a typing event to a thread, on behalf of a user.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
await chat_thread_client.send_typing_notification()
-
async
update_message
(message_id: str, *, content: str = None, **kwargs) → None[source]¶ Updates a message.
- Parameters
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
content = "updated message content" await chat_thread_client.update_message(self._message_id, content=content)
-
async
update_thread
(*, topic: str = None, **kwargs) → None[source]¶ Updates a thread’s properties.
- Parameters
topic (str) – Thread topic. If topic is not specified, the update will succeeded but chat thread properties will not be changed.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
None, or the result of cls(response)
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
topic = "updated thread topic" await chat_thread_client.update_thread(topic=topic)