def test_remove_member(self): self._create_thread() # add member first share_history_time = datetime.utcnow() share_history_time = share_history_time.replace(tzinfo=TZ_UTC) new_member = ChatThreadMember(user=self.new_user, display_name='name', share_history_time=share_history_time) members = [new_member] self.chat_thread_client.add_members(members) # test remove member self.chat_thread_client.remove_member(self.new_user)
def add_members(self): from azure.communication.chat import ChatThreadClient, CommunicationUserCredential chat_thread_client = ChatThreadClient( self.endpoint, CommunicationUserCredential(self.token), self._thread_id) # [START add_members] from azure.communication.chat import ChatThreadMember from datetime import datetime new_member = ChatThreadMember(user=self.new_user, display_name='name', share_history_time=datetime.utcnow()) thread_members = [new_member] chat_thread_client.add_members(thread_members) # [END add_members] print("add_chat_members succeeded")
async def test_add_members(self): async with self.chat_client: await self._create_thread() async with self.chat_thread_client: share_history_time = datetime.utcnow() share_history_time = share_history_time.replace(tzinfo=TZ_UTC) new_member = ChatThreadMember( user=self.new_user, display_name='name', share_history_time=share_history_time) members = [new_member] await self.chat_thread_client.add_members(members) if not self.is_playback(): await self.chat_client.delete_chat_thread(self.thread_id)
def create_chat_thread_client(self): # [START create_chat_thread_client] from datetime import datetime from azure.communication.chat import (ChatClient, ChatThreadMember, CommunicationUser, CommunicationUserCredential) chat_client = ChatClient(self.endpoint, CommunicationUserCredential(self.token)) topic = "test topic" members = [ ChatThreadMember(user=self.user, display_name='name', share_history_time=datetime.utcnow()) ] chat_thread_client = chat_client.create_chat_thread(topic, members) # [END create_chat_thread_client] self._thread_id = chat_thread_client.thread_id print("chat_thread_client created")
async def add_members_async(self): from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions refresh_options = CommunicationTokenRefreshOptions(self.token) chat_thread_client = ChatThreadClient( self.endpoint, CommunicationTokenCredential(refresh_options), self._thread_id) async with chat_thread_client: # [START add_members] 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) # [END add_members] print("add_members succeeded")
async def create_chat_thread_client_async(self): # [START create_chat_thread_client] from datetime import datetime from azure.communication.chat.aio import ChatClient, CommunicationUserCredential from azure.communication.chat import ChatThreadMember, CommunicationUserIdentifier 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) # [END create_chat_thread_client] self._thread_id = chat_thread_client.thread_id print("thread created, id: " + self._thread_id)
def test_add_members(self): thread_id = "19:[email protected]" new_member_id="8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041" raised = False def mock_send(*_, **__): return mock_response(status_code=207) chat_thread_client = ChatThreadClient("https://endpoint", TestChatThreadClient.credential, thread_id, transport=Mock(send=mock_send)) new_member = ChatThreadMember( user=CommunicationUserIdentifier(new_member_id), display_name='name', share_history_time=datetime.utcnow()) members = [new_member] try: chat_thread_client.add_members(members) except: raised = True self.assertFalse(raised, 'Expected is no excpetion raised')
def test_create_chat_thread_raises_error(self): def mock_send(*_, **__): return mock_response(status_code=400, json_payload={"msg": "some error"}) chat_client = ChatClient("https://endpoint", TestChatClient.credential, transport=Mock(send=mock_send)) topic = "test topic", user = CommunicationUser( "8:acs:57b9bac9-df6c-4d39-a73b-26e944adf6ea_9b0110-08007f1041") thread_members = [ ChatThreadMember(user=user, display_name='name', share_history_time=datetime.utcnow()) ] self.assertRaises(HttpResponseError, chat_client.create_chat_thread, topic=topic, thread_members=thread_members)
def create_thread(self): # [START create_thread] from datetime import datetime from azure.communication.chat import (ChatClient, ChatThreadMember, CommunicationUserIdentifier, CommunicationTokenCredential, CommunicationTokenRefreshOptions) refresh_options = CommunicationTokenRefreshOptions(self.token) chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options)) topic = "test topic" members = [ ChatThreadMember(user=self.user, display_name='name', share_history_time=datetime.utcnow()) ] chat_thread_client = chat_client.create_chat_thread(topic, members) # [END create_thread] self._thread_id = chat_thread_client.thread_id print("thread created, id: " + self._thread_id)