azure.communication.chat.aio package¶
-
class
azure.communication.chat.aio.
ChatClient
(endpoint: str, credential: azure.communication.chat._shared.user_credential_async.CommunicationTokenCredential, **kwargs: Any)[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 (CommunicationTokenCredential) – The credentials with which to authenticate.
Example:
from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
-
async
create_chat_thread
(topic: str, thread_participants: List[azure.communication.chat._models.ChatThreadParticipant], repeatability_request_id: Optional[str] = None, **kwargs) → azure.communication.chat.aio._chat_thread_client_async.ChatThreadClient[source]¶ Creates a chat thread.
- Parameters
topic (str) – Required. The thread topic.
thread_participants (list[ChatThreadParticipant]) – Required. Participants to be added to the thread.
repeatability_request_id (str) – If specified, the client directs that the request is repeatable; that is, that the client can make the request multiple times with the same Repeatability-Request-ID and get back an appropriate response without the server executing the request multiple times. The value of the Repeatability-Request-ID 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
ChatThreadClient
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
topic = "test topic" participants = [ChatThreadParticipant( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] # creates a new chat_thread everytime chat_thread_client = await chat_client.create_chat_thread(topic, participants) # creates a new chat_thread if not exists repeatability_request_id = 'b66d6031-fdcc-41df-8306-e524c9f226b8' # unique identifier chat_thread_client_w_repeatability_id = await chat_client.create_chat_thread(topic, participants, repeatability_request_id)
-
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: Any) → 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, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) chat_thread_client = chat_client.get_chat_thread_client(self._thread_id)
-
list_chat_threads
(**kwargs: Any) → 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
An iterator like instance of 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.CommunicationTokenCredential, thread_id: str, **kwargs: Any)[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 participant to chat thread, remove participant 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 (CommunicationTokenCredential) – 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, CommunicationTokenCredential, CommunicationTokenRefreshOptions from azure.communication.chat import ChatThreadParticipant, CommunicationUserIdentifier refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) async with chat_client: topic = "test topic" participants = [ChatThreadParticipant( user=self.user, display_name='name', share_history_time=datetime.utcnow() )] chat_thread_client = await chat_client.create_chat_thread(topic, participants)
-
async
add_participant
(thread_participant: azure.communication.chat._models.ChatThreadParticipant, **kwargs) → None[source]¶ Adds single thread participant to a thread. If participant already exist, no change occurs.
- Parameters
thread_participant (ChatThreadParticipant) – Required. Single thread participant 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 ChatThreadParticipant, CommunicationUserIdentifier from datetime import datetime new_chat_thread_participant = ChatThreadParticipant( user=self.new_user, display_name='name', share_history_time=datetime.utcnow()) await chat_thread_client.add_participant(new_chat_thread_participant)
-
async
add_participants
(thread_participants: List[azure.communication.chat._models.ChatThreadParticipant], **kwargs) → None[source]¶ Adds thread participants to a thread. If participants already exist, no change occurs.
- Parameters
thread_participants (list[ChatThreadParticipant]) – Required. Thread participants 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 ChatThreadParticipant, CommunicationUserIdentifier from datetime import datetime new_participant = ChatThreadParticipant( user=self.new_user, display_name='name', share_history_time=datetime.utcnow()) participants = [new_participant] await chat_thread_client.add_participants(participants)
-
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_messages
(**kwargs: Any) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._models.ChatMessage][source]¶ Gets a list of messages from a thread.
- Keyword Arguments
- Returns
An iterator like instance of 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_participants
(**kwargs: Any) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._models.ChatThreadParticipant][source]¶ Gets the participants of a thread.
- Keyword Arguments
- Returns
An iterator like instance of ChatThreadParticipant
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
chat_thread_participants = chat_thread_client.list_participants() print("list_participants succeeded, participants:") async for chat_thread_participant in chat_thread_participants: print(chat_thread_participant)
-
list_read_receipts
(**kwargs: Any) → azure.core.async_paging.AsyncItemPaged[azure.communication.chat._models.ChatMessageReadReceipt][source]¶ Gets read receipts for a thread.
- Keyword Arguments
- Returns
An iterator like instance of ChatMessageReadReceipt
- Return type
- Raises
~azure.core.exceptions.HttpResponseError, ValueError
Example:
read_receipts = chat_thread_client.list_read_receipts()
-
async
remove_participant
(user: azure.communication.chat._shared.models.CommunicationUserIdentifier, **kwargs) → None[source]¶ Remove a participant from a thread.
- Parameters
user (CommunicationUserIdentifier) – Required. User identity of the thread participant 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_participant(self.new_user)
-
async
send_message
(content: str, **kwargs) → str[source]¶ Sends a message to a thread.
- Parameters
content (str) – Required. Chat message content.
chat_message_type – The chat message type. Possible values include: “text”, “html”.
Default: ChatMessageType.TEXT :type chat_message_type: str or ~azure.communication.chat.models.ChatMessageType :keyword str sender_display_name: The display name of the message sender. This property is used to
populate sender name for push notifications.
- Keyword Arguments
cls (callable) – A custom type or function that will be passed the direct response
- Returns
str, 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_id = await chat_thread_client.send_message( content, priority=priority, sender_display_name=sender_display_name) send_message_result_w_type_id = await chat_thread_client.send_message( content, sender_display_name=sender_display_name, chat_message_type=ChatMessageType.TEXT)
-
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_topic
(*, 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_topic(topic=topic)
-
class
azure.communication.chat.aio.
CommunicationTokenCredential
(communication_token_refresh_options)[source]¶ Credential type used for authenticating to an Azure Communication service. :param communication_token_refresh_options: The token used to authenticate to an Azure Communication service :type communication_token_refresh_options: ~azure.communication.chat.aio.CommunicationTokenRefreshOptions :raises: TypeError
-
ON_DEMAND_REFRESHING_INTERVAL_MINUTES
= 2¶
-
-
class
azure.communication.chat.aio.
CommunicationTokenRefreshOptions
[source]¶ Options for refreshing CommunicationTokenCredential. :param str token: The token used to authenticate to an Azure Communication service :param token_refresher: The token refresher to provide capacity to fetch fresh token :raises: TypeError