Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions sdk/communication/azure-communication-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ tokenresponse = identity_client.issue_token(user, scopes=["chat"])
token = tokenresponse.token
```

The `user` created above will be used later, because that user should be added as a member of new chat thread when you creating
it with this token. It is because the initiator of the create request must be in the list of the members of the chat thread.
The `user` created above will be used later, because that user should be added as a participant of new chat thread when you creating
it with this token. It is because the initiator of the create request must be in the list of the participants of the chat thread.

## Create the Chat Client

Expand All @@ -54,12 +54,12 @@ chat_client = ChatClient(endpoint, CommunicationUserCredential(token))
## Create Chat Thread Client

The ChatThreadClient will allow you to perform operations specific to a chat thread, like send message, get message, update
the chat thread topic, add members to chat thread, etc.
the chat thread topic, add participants to chat thread, etc.

You can get it by creating a new chat thread using ChatClient:

```python
chat_thread_client = chat_client.create_chat_thread(topic, thread_members)
chat_thread_client = chat_client.create_chat_thread(topic, thread_participants)
```

Alternatively, if you have created a chat thread before and you have its thread_id, you can create it by:
Expand All @@ -70,14 +70,14 @@ chat_thread_client = chat_client.get_chat_thread_client(thread_id)

# Key concepts

A chat conversation is represented by a chat thread. Each user in the thread is called a thread member. Thread members can chat with one another privately in a 1:1 chat or huddle up in a 1:N group chat. Users also get near real-time updates for when others are typing and when they have read the messages.
A chat conversation is represented by a chat thread. Each user in the thread is called a thread participant. Thread participants can chat with one another privately in a 1:1 chat or huddle up in a 1:N group chat. Users also get near real-time updates for when others are typing and when they have read the messages.

Once you initialized a `ChatClient` class, you can do the following chat operations:

## Create, get, update, and delete threads

```Python
create_chat_thread(topic, thread_members, **kwargs)
create_chat_thread(topic, thread_participants, **kwargs)
get_chat_thread(thread_id, **kwargs)
list_chat_threads(**kwargs)
delete_chat_thread(thread_id, **kwargs)
Expand All @@ -88,7 +88,7 @@ Once you initialized a `ChatThreadClient` class, you can do the following chat o
## Update thread

```python
update_thread(topic, **kwargs)
update_topic(topic, **kwargs)
```

## Send, get, update, and delete messages
Expand All @@ -101,12 +101,12 @@ update_message(message_id, content, **kwargs)
delete_message(message_id, **kwargs)
```

## Get, add, and remove members
## Get, add, and remove participants

```Python
list_members(**kwargs)
add_members(thread_members, **kwargs)
remove_member(member_id, **kwargs)
list_participants(**kwargs)
add_participants(thread_participants, **kwargs)
remove_participant(participant_id, **kwargs)
```

## Send typing notification
Expand All @@ -130,7 +130,7 @@ The following sections provide several code snippets covering some of the most c

<!-- - [Thread Operations](#thread-operations)
- [Message Operations](#message-operations)
- [Thread Member Operations](#thread-member-operations)
- [Thread Participant Operations](#thread-participant-operations)
- [Events Operations](#events-operations) -->

## Thread Operations
Expand All @@ -140,24 +140,24 @@ The following sections provide several code snippets covering some of the most c
Use the `create_chat_thread` method to create a chat thread client object.

- Use `topic` to give a thread topic;
- Use `thread_members` to list the `ChatThreadMember` to be added to the thread;
- Use `thread_participants` to list the `ChatThreadParticipant` to be added to the thread;
- `user`, required, it is the `CommunicationUser` you created by CommunicationIdentityClient.create_user() from User Access Tokens
<!-- [User Access Tokens](#user-access-tokens) -->
- `display_name`, optional, is the display name for the thread member.
- `share_history_time`, optional, time from which the chat history is shared with the member.
- `display_name`, optional, is the display name for the thread participant.
- `share_history_time`, optional, time from which the chat history is shared with the participant.

`ChatThreadClient` is the result returned from creating a thread, you can use it to perform other chat operations to this chat thread

```Python
from azure.communication.chat import ChatThreadMember
from azure.communication.chat import ChatThreadParticipant
topic = "test topic"
thread_members = [ChatThreadMember(
thread_participants = [ChatThreadParticipant(
user='<user>',
display_name='name',
share_history_time=datetime.utcnow()
)]

chat_thread_client = chat_client.create_chat_thread(topic, thread_members)
chat_thread_client = chat_client.create_chat_thread(topic, thread_participants)
thread_id = chat_thread_client.thread_id
```

Expand Down Expand Up @@ -281,50 +281,50 @@ Use `delete_message` to delete a message.
chat_thread_client.delete_message(message_id)
```

## Thread Member Operations
## Thread Participant Operations

### Get thread members
### Get thread participants

Use `list_members` to retrieve the members of the thread.
Use `list_participants` to retrieve the participants of the thread.

An iterator of `[ChatThreadMember]` is the response returned from listing members
An iterator of `[ChatThreadParticipant]` is the response returned from listing participants

```python
chat_thread_members = chat_thread_client.list_members()
for chat_thread_member in chat_thread_members:
print(chat_thread_member)
chat_thread_participants = chat_thread_client.list_participants()
for chat_thread_participant in chat_thread_participants:
print(chat_thread_participant)
```

### Add thread members
### Add thread participants

Use `add_members` method to add thread members to the thread.
Use `add_participants` method to add thread participants to the thread.

- Use `thread_members` to list the `ChatThreadMember` to be added to the thread;
- Use `thread_participants` to list the `ChatThreadParticipant` to be added to the thread;
- `user`, required, it is the `CommunicationUser` you created by CommunicationIdentityClient.create_user() from User Access Tokens
<!-- [User Access Tokens](#user-access-tokens) -->
- `display_name`, optional, is the display name for the thread member.
- `share_history_time`, optional, time from which the chat history is shared with the member.
- `display_name`, optional, is the display name for the thread participant.
- `share_history_time`, optional, time from which the chat history is shared with the participant.

```Python
from azure.communication.chat import ChatThreadMember
from azure.communication.chat import ChatThreadParticipant
from datetime import datetime
member = ChatThreadMember(
participant = ChatThreadParticipant(
user='<user>',
display_name='name',
share_history_time=datetime.utcnow())
thread_members = [member]
chat_thread_client.add_members(thread_members)
thread_participants = [participant]
chat_thread_client.add_participants(thread_participants)
```

### Remove thread member
### Remove thread participant

Use `remove_member` method to remove thread member from the thread identified by threadId.
Use `remove_participant` method to remove thread participant from the thread identified by threadId.
`user` is the `CommunicationUser` you created by CommunicationIdentityClient.create_user() from User Access Tokens
<!-- [User Access Tokens](#user-access-tokens) -->
and was added into this chat thread.

```python
chat_thread_client.remove_member(user)
chat_thread_client.remove_participant(user)
```

## Events Operations
Expand All @@ -349,7 +349,7 @@ chat_thread_client.send_read_receipt(message_id)

`list_read_receipts` method retrieves read receipts for a thread.

An iterator of `[ReadReceipt]` is the response returned from listing read receipts
An iterator of `[ChatMessageReadReceipt]` is the response returned from listing read receipts

```python
read_receipts = chat_thread_client.list_read_receipts()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
)
from ._shared.user_credential import CommunicationUserCredential
from ._models import (
ChatThreadMember,
ChatThreadParticipant,
ChatMessage,
ChatThread,
ReadReceipt,
ChatMessageReadReceipt,
)
from ._shared.models import CommunicationUser

Expand All @@ -20,12 +20,12 @@
'ChatThreadClient',
'ChatMessage',
'ChatMessagePriority',
'ReadReceipt',
'ChatMessageReadReceipt',
'SendChatMessageResult',
'ChatThread',
'ChatThreadInfo',
'CommunicationUserCredential',
'ChatThreadMember',
'ChatThreadParticipant',
'CommunicationUser',
]
__version__ = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
from urlparse import urlparse # type: ignore

from azure.core.tracing.decorator import distributed_trace
from azure.core.exceptions import HttpResponseError

from ._chat_thread_client import ChatThreadClient
from ._common import CommunicationUserCredentialPolicy
from ._shared.user_credential import CommunicationUserCredential
from ._generated import AzureCommunicationChatService
from ._generated.models import CreateChatThreadRequest
from ._models import ChatThread
from ._utils import _to_utc_datetime # pylint: disable=unused-import
from ._models import (
ChatThread,
ChatThreadParticipant
)
from ._utils import _to_utc_datetime, return_response # pylint: disable=unused-import
from ._version import SDK_MONIKER

if TYPE_CHECKING:
Expand Down Expand Up @@ -117,16 +119,16 @@ def get_chat_thread_client(
@distributed_trace
def create_chat_thread(
self, topic, # type: str
thread_members, # type: list[ChatThreadMember]
thread_participants, # type: list[ChatThreadParticipant]
**kwargs # type: Any
):
# type: (...) -> ChatThreadClient
"""Creates a chat thread.

:param topic: Required. The thread topic.
:type topic: str
:param thread_members: Required. Members to be added to the thread.
:type thread_members: list[~azure.communication.chat.ChatThreadMember]
:param thread_participants: Required. Participants to be added to the thread.
:type thread_participants: list[~azure.communication.chat.ChatThreadParticipant]
:return: ChatThreadClient
:rtype: ~azure.communication.chat.ChatThreadClient
:raises: ~azure.core.exceptions.HttpResponseError, ValueError
Expand All @@ -142,28 +144,29 @@ def create_chat_thread(
"""
if not topic:
raise ValueError("topic cannot be None.")
if not thread_members:
raise ValueError("List of ThreadMember cannot be None.")

members = [m._to_generated() for m in thread_members] # pylint:disable=protected-access
create_thread_request = CreateChatThreadRequest(topic=topic, members=members)

create_chat_thread_result = self._client.create_chat_thread(create_thread_request, **kwargs)

multiple_status = create_chat_thread_result.multiple_status
thread_status = [status for status in multiple_status if status.type == "Thread"]
if not thread_status:
raise HttpResponseError(message="Can not find chat thread status result from: {}".format(thread_status))
if thread_status[0].status_code != 201:
raise HttpResponseError(message="Chat thread creation failed with status code {}, message: {}.".format(
thread_status[0].status_code, thread_status[0].message))

thread_id = thread_status[0].id

if not thread_participants:
raise ValueError("List of ChatThreadParticipant cannot be None.")

participants = [m._to_generated() for m in thread_participants] # pylint:disable=protected-access
create_thread_request = CreateChatThreadRequest(topic=topic, participants=participants)

response, create_chat_thread_result = self._client.create_chat_thread(
create_thread_request, cls=return_response, **kwargs)
if response is not None:
response_header = response.http_response.headers
if ('Azure-Acs-InvalidParticipants' in response_header.keys() and
response_header['Azure-Acs-InvalidParticipants'] is not None):
invalid_participant_and_reason_list = response_header['Azure-Acs-InvalidParticipants'].split('|')
errors = []
for invalid_participant_and_reason in invalid_participant_and_reason_list:
participant, reason = invalid_participant_and_reason.split(',', 1)
errors.append('participant ' + participant + ' failed to join thread '
+ create_chat_thread_result.id + ' return statue code ' + reason)
raise ValueError(errors)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be a ValueError

return ChatThreadClient(
endpoint=self._endpoint,
credential=self._credential,
thread_id=thread_id,
thread_id=create_chat_thread_result.id,
**kwargs
)

Expand Down
Loading