Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.64k stars 2.84k forks source link

Enabling chat between ACS user and Teams user #38135

Open deshneni-akhil opened 1 month ago

deshneni-akhil commented 1 month ago

Describe the bug I am building a web application where I create a chat thread using ACS SDK and looking to enable communication between ACS user and Microsoft team's user, currently when trying to add Microsoft teams users to the chat thread I am facing the following error:

Error

Traceback (most recent call last): File "c:\Users\030821958\UTS-Project\experiments\add_teams_user_acs_chat\chat_teams_user.py", line 400, in sample.add_participants_w_check() File "c:\Users\030821958\UTS-Project\experiments\add_teams_user_acs_chat\chat_teams_user.py", line 315, in add_participants_w_check result = chat_thread_client.add_participants(thread_participants) File "C:\Users\030821958SA\UTS-Project\experiments\add_teams_user_acs_chat\venv\lib\site-packages\azure\core\tracing\decorator.py", line 94, in wrapper_use_tracer return func(*args, **kwargs) File "C:\Users\030821958\UTS-Project\experiments\add_teams_user_acs_chat\venv\lib\site-packages\azure\communication\chat_chat_thread_client.py", line 547, in add_participants response = CommunicationErrorResponseConverter.convert( File "C:\Users\030821958\UTS-Project\experiments\add_teams_user_acs_chat\venv\lib\site-packages\azure\communication\chat_utils.py", line 56, in convert _thread_participants_dict = create_dict(participants=participants) File "C:\Users\030821958\UTS-Project\experiments\add_teams_user_acs_chat\venv\lib\site-packages\azure\communication\chat_utils.py", line 53, in create_dict result[participant.identifier.properties['id']] = participant KeyError: 'id'

code

 def add_participants_w_check(self):
        from azure.communication.chat import CommunicationUserIdentifier, CommunicationIdentifier, MicrosoftTeamsUserIdentifier
        # initially remove already added user
        thread_id = self._thread_id
        chat_client = self._chat_client
        user = self.new_user
        chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)

        chat_thread_client.remove_participant(user)

        # [START add_participants]
        from azure.communication.chat import ChatParticipant
        from datetime import datetime

        def decide_to_retry(error):
            """
            Custom logic to decide whether to retry to add or not
            """
            return True

        # set `thread_id` to an existing thread id
        chat_thread_client = chat_client.get_chat_thread_client(thread_id=thread_id)

        # create `user` using CommunicationIdentityClient.create_user method for new users;
        # else for existing users set `user` = CommunicationUserIdentifier(some_user_id)
        new_participant = ChatParticipant(
            identifier=user,
            display_name='name',
            share_history_time=datetime.utcnow())

        teams_user = ChatParticipant(
            identifier = MicrosoftTeamsUserIdentifier("<teams-user-id>"),
            display_name='name',
            share_history_time=datetime.utcnow())

        # create list containing one or more participants
        thread_participants = [new_participant, teams_user]
        result = chat_thread_client.add_participants(thread_participants)

        # list of participants which were unsuccessful to be added to chat thread
        retry = [p for p, e in result if decide_to_retry(e)]
        if retry:
            chat_thread_client.add_participants(retry)
        # [END add_participants]
        print("add_participants_w_check succeeded")

Additional context

I am trying to replicate this behavior as mentioned in Q&A is it possible to bridge a chat solution between ACS user and Teams user without creating a meeting link ?

github-actions[bot] commented 1 month ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @acsdevx-msft.