async def remove_participant_async(self): thread_id = self._thread_id chat_client = self._chat_client identity_client = self.identity_client from azure.communication.chat import ChatThreadParticipant from azure.communication.identity import CommunicationUserIdentifier from datetime import datetime async with chat_client: # create 2 new users using CommunicationIdentityClient.create_user method user1 = identity_client.create_user() user2 = identity_client.create_user() # set `thread_id` to an existing thread id chat_thread_client = chat_client.get_chat_thread_client( thread_id=thread_id) async with chat_thread_client: # add user1 and user2 to chat thread participant1 = ChatThreadParticipant( user=user1, display_name='Fred Flinstone', share_history_time=datetime.utcnow()) participant2 = ChatThreadParticipant( user=user2, display_name='Wilma Flinstone', share_history_time=datetime.utcnow()) thread_participants = [participant1, participant2] await chat_thread_client.add_participants(thread_participants) # [START remove_participant] # Option 1 : Iterate through all participants, find and delete Fred Flinstone chat_thread_participants = chat_thread_client.list_participants( ) async for chat_thread_participant_page in chat_thread_participants.by_page( ): async for chat_thread_participant in chat_thread_participant_page: print("ChatThreadParticipant: ", chat_thread_participant) if chat_thread_participant.user.identifier == user1.identifier: print("Found Fred!") await chat_thread_client.remove_participant( chat_thread_participant.user) print("Fred has been removed from the thread...") break # Option 2: Directly remove Wilma Flinstone unique_identifier = user2.identifier # in real scenario the identifier would need to be retrieved from elsewhere await chat_thread_client.remove_participant( CommunicationUserIdentifier(unique_identifier)) print("Wilma has been removed from the thread...") # [END remove_participant] # clean up temporary users self.identity_client.delete_user(user1) self.identity_client.delete_user(user2) print("remove_participant_async succeeded")
def communication_chat_add_participant(client, thread_id, user_id, display_name=None, start_time=None): from azure.communication.chat import ChatParticipant from azure.communication.identity import CommunicationUserIdentifier chat_thread_client = client.get_chat_thread_client(thread_id) participant = ChatParticipant( identifier=CommunicationUserIdentifier(user_id), display_name=display_name, share_history_time=start_time) res = chat_thread_client.add_participants([participant]) return [r[1] for r in res]
def issue_access_token(client, scopes, userid=None): user_token_data = {"user_id": userid, "token": "", "expires_on": ""} if userid is not None: user = CommunicationUserIdentifier(userid) token_data = client.get_token(user, scopes) user_token_data["token"] = token_data.token user_token_data["expires_on"] = str(token_data.expires_on) else: identity_token_result = client.create_user_and_token(scopes) if len(identity_token_result) >= 2: user_token_data["user_id"] = identity_token_result[0].properties[ 'id'] user_token_data["token"] = identity_token_result[1].token user_token_data["expires_on"] = str( identity_token_result[1].expires_on) return user_token_data
# Issue an access token with the "voip" scope for an identity token_result = client.get_token(identity, ["voip"]) expires_on = token_result.expires_on.strftime("%d/%m/%y %I:%M %S %p") print("\nIssued an access token with 'voip' scope that expires at " + expires_on + ":") print(token_result.token) # Create an identity and issue an access token within the same request identity_token_result = client.create_user_and_token(["voip"]) identity = identity_token_result[0].properties['id'] token = identity_token_result[1].token expires_on = identity_token_result[1].expires_on.strftime("%d/%m/%y %I:%M %S %p") print("\nCreated an identity with ID: " + identity) print("\nIssued an access token with 'voip' scope that expires at " + expires_on + ":") print(token) # Refresh access tokens - existingIdentity represents identity of Azure Communication Services stored during identity creation identity = CommunicationUserIdentifier(existingIdentity.properties['id']) token_result = client.get_token( identity, ["voip"]) # Revoke access tokens client.revoke_tokens(identity) print("\nSuccessfully revoked all access tokens for identity with ID: " + identity.properties['id']) # Delete an identity client.delete_user(identity) print("\nDeleted the identity with ID: " + identity.properties['id']) except Exception as ex: print("Exception:") print(ex)
def communication_chat_remove_participant(client, thread_id, user_id): from azure.communication.identity import CommunicationUserIdentifier chat_thread_client = client.get_chat_thread_client(thread_id) return chat_thread_client.remove_participant( CommunicationUserIdentifier(user_id))
def communication_identity_revoke_access_tokens(client, user_id): from azure.communication.identity import CommunicationUserIdentifier return client.revoke_tokens(CommunicationUserIdentifier(user_id))
def communication_identity_delete_user(client, user_id): from azure.communication.identity import CommunicationUserIdentifier return client.delete_user(CommunicationUserIdentifier(user_id))