Exemple #1
0
    def setUp(self):
        super(ChatThreadClientTestAsync, self).setUp()

        self.recording_processors.extend([
            BodyReplacerProcessor(keys=["id", "token", "senderId", "chatMessageId", "nextLink", "participants", "multipleStatus", "value"]),
            URIIdentityReplacer(),
            ResponseReplacerProcessor(keys=[self._resource_name]),
            ChatURIReplacer()])

        endpoint, _ = parse_connection_str(self.connection_str)
        self.endpoint = endpoint

        self.identity_client = CommunicationIdentityClient.from_connection_string(self.connection_str)

        self.users = []
        self.user_tokens = []
        self.chat_clients = []

        # create user 1
        self.user = self.identity_client.create_user()
        token_response = self.identity_client.get_token(self.user, scopes=["chat"])
        self.token = token_response.token

        # create user 2
        self.new_user = self.identity_client.create_user()
        token_response = self.identity_client.get_token(self.new_user, scopes=["chat"])
        self.token_new_user = token_response.token

        # create ChatClient
        refresh_option = CommunicationTokenRefreshOptions(self.token)
        refresh_option_new_user = CommunicationTokenRefreshOptions(self.token_new_user)
        self.chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option))
        self.chat_client_new_user = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_option_new_user))
Exemple #2
0
 def create_chat_client(self):
     # [START create_chat_client]
     from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
     chat_client = ChatClient(self.endpoint,
                              CommunicationTokenCredential(self.token))
     # [END create_chat_client]
     print("chat_client created")
    def setUp(self):
        super(ChatClientTestAsync, self).setUp()

        self.recording_processors.extend([
            BodyReplacerProcessor(keys=[
                "id", "token", "createdBy", "participants", "multipleStatus",
                "value"
            ]),
            URIIdentityReplacer(),
            ResponseReplacerProcessor(keys=[self._resource_name]),
            ChatURIReplacer()
        ])

        endpoint, _ = parse_connection_str(self.connection_str)
        self.endpoint = endpoint

        self.identity_client = CommunicationIdentityClient.from_connection_string(
            self.connection_str)

        # create user
        self.user = self.identity_client.create_user()
        token_response = self.identity_client.get_token(self.user,
                                                        scopes=["chat"])
        self.token = token_response.token

        # create ChatClient
        self.chat_client = ChatClient(self.endpoint,
                                      CommunicationTokenCredential(self.token))
Exemple #4
0
    async def send_message_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 send_message]
            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)
            # [END send_message]
            self._message_id = send_message_result_id
            print("send_message succeeded, message id:", self._message_id)
            print("send_message succeeded with type specified, message id:",
                  send_message_result_w_type_id)
Exemple #5
0
    async def create_thread_async(self):
        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:
            # [START create_thread]
            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)
            # [END create_thread]

            self._thread_id = chat_thread_client.thread_id
            print("thread created, id: " + self._thread_id)
    async def create_thread_async(self):
        token = self.token
        endpoint = self.endpoint
        thread_id = self._thread_id

        # [START create_thread]
        from datetime import datetime
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
        from azure.communication.chat import ChatParticipant

        # set `endpoint` to an existing ACS endpoint
        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
        async with chat_client:

            topic = "test topic"
            participants = [ChatParticipant(
                identifier=self.user,
                display_name='name',
                share_history_time=datetime.utcnow()
            )]
            # creates a new chat_thread everytime
            create_chat_thread_result = await chat_client.create_chat_thread(topic, thread_participants=participants)

            # creates a new chat_thread if not exists
            idempotency_token = 'b66d6031-fdcc-41df-8306-e524c9f226b8'  # unique identifier
            create_chat_thread_result_w_repeatability_id = await chat_client.create_chat_thread(
                topic,
                thread_participants=participants,
                idempotency_token=idempotency_token)
            # [END create_thread]

            self._thread_id = create_chat_thread_result.chat_thread.id
            print("thread created, id: " + self._thread_id)
Exemple #7
0
    async def create_chat_thread_client_async(self):
        token = self.token
        endpoint = self.endpoint
        user = self.user
        # [START create_chat_thread_client]
        from datetime import datetime
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential
        from azure.communication.chat import ChatParticipant, CommunicationUserIdentifier
        # set `endpoint` to an existing ACS endpoint
        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))

        async with chat_client:
            topic = "test topic"
            participants = [
                ChatParticipant(identifier=user,
                                display_name='name',
                                share_history_time=datetime.utcnow())
            ]
            create_chat_thread_result = await chat_client.create_chat_thread(
                topic, thread_participants=participants)
            chat_thread_client = chat_client.get_chat_thread_client(
                create_chat_thread_result.chat_thread.id)
        # [END create_chat_thread_client]

        self._thread_id = create_chat_thread_result.chat_thread.id
        print("thread created, id: " + self._thread_id)
        print("create_chat_thread_client_async succeeded")
Exemple #8
0
    def create_chat_client(self):
        # [START create_chat_client]
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions

        refresh_options = CommunicationTokenRefreshOptions(self.token)
        chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
        # [END create_chat_client]
        print("chat_client created")
    async def send_typing_notification_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START send_typing_notification]
            await chat_thread_client.send_typing_notification()
            # [END send_typing_notification]
        print("send_typing_notification succeeded")
    async def remove_member_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START remove_member]
            await chat_thread_client.remove_member(self.new_user)
            # [END remove_member]
            print("remove_member_async succeeded")
    async def delete_message_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START delete_message]
            await chat_thread_client.delete_message(self._message_id)
            # [END delete_message]
            print("delete_message succeeded")
 async def create_credential_with_refreshing_callback(self):
     # Alternatively, for long-lived clients, you can create a `CommunicationTokenCredential` with a callback to renew tokens if expired.
     # Here we assume that we have a function `fetch_token_from_server` that makes a network request to retrieve a token string for a user.
     # It's necessary that the `fetch_token_from_server` function returns a valid token (with an expiration date set in the future) at all times.
     fetch_token_from_server = lambda: None
     async with CommunicationTokenCredential(
             self.token,
             token_refresher=fetch_token_from_server) as credential:
         token_response = await credential.get_token()
         print("Token issued with value: " + token_response.token)
    async def list_members_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START list_members]
            chat_thread_members = chat_thread_client.list_members()
            print("list_members succeeded, members:")
            async for chat_thread_member in chat_thread_members:
                print(chat_thread_member)
    async def send_read_receipt_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START send_read_receipt]
            await chat_thread_client.send_read_receipt(self._message_id)
            # [END send_read_receipt]

        print("send_read_receipt succeeded")
    async def update_message_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START update_message]
            content = "updated message content"
            await chat_thread_client.update_message(self._message_id, content=content)
            # [END update_message]
            print("update_message succeeded")
    async def get_message_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START get_message]
            chat_message = await chat_thread_client.get_message(self._message_id)
            # [END get_message]
            print("get_message succeeded, message id:", chat_message.id, \
                "content: ", chat_message.content)
Exemple #17
0
    async def get_thread_async(self):
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions

        refresh_options = CommunicationTokenRefreshOptions(self.token)
        chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
        async with chat_client:
            # [START get_thread]
            chat_thread = await chat_client.get_chat_thread(self._thread_id)
            # [END get_thread]
            print("get_thread succeeded, thread id: " + chat_thread.id + ", thread topic: " + chat_thread.topic)
Exemple #18
0
    def get_chat_thread_client(self):
        # [START get_chat_thread_client]
        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)
        # [END get_chat_thread_client]

        print("chat_thread_client created with thread id: ", chat_thread_client.thread_id)
 async def create_credential_with_proactive_refreshing_callback(self):
     # Optionally, you can enable proactive token refreshing where a fresh token will be acquired as soon as the
     # previous token approaches expiry. Using this method, your requests are less likely to be blocked to acquire a fresh token
     fetch_token_from_server = lambda: None
     async with CommunicationTokenCredential(
             self.token,
             token_refresher=fetch_token_from_server,
             proactive_refresh=True) as credential:
         token_response = await credential.get_token()
         print("Token issued with value: " + token_response.token)
Exemple #20
0
    async def delete_thread_async(self):
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions

        refresh_options = CommunicationTokenRefreshOptions(self.token)
        chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
        async with chat_client:
            # [START delete_thread]
            await chat_client.delete_chat_thread(self._thread_id)
            # [END delete_thread]
            print("delete_thread succeeded")
    async def update_thread_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START update_thread]
            topic = "updated thread topic"
            await chat_thread_client.update_thread(topic=topic)
            # [END update_thread]

        print("update_thread succeeded")
    async def list_read_receipts_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START list_read_receipts]
            read_receipts = chat_thread_client.list_read_receipts()
            # [END list_read_receipts]
            print("list_read_receipts succeeded, receipts:")
            async for read_receipt in read_receipts:
                print(read_receipt)
    def create_chat_client(self):
        token = self.token
        endpoint = self.endpoint
        thread_id = self._thread_id

        # [START create_chat_client]
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

        # set `endpoint` to an existing ACS endpoint
        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
        # [END create_chat_client]
        print("chat_client created")
    async def list_messages_async(self):
        from azure.communication.chat.aio import ChatThreadClient, CommunicationTokenCredential
        chat_thread_client = ChatThreadClient(self.endpoint, CommunicationTokenCredential(self.token), self._thread_id)

        async with chat_thread_client:
            # [START list_messages]
            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))
Exemple #25
0
    async def remove_participant_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 remove_participant]
            await chat_thread_client.remove_participant(self.new_user)
            # [END remove_participant]
            print("remove_participant_async succeeded")
Exemple #26
0
    async def list_participants_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 list_participants]
            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)
Exemple #27
0
    def get_chat_thread_client(self):
        token = self.token
        endpoint = self.endpoint
        thread_id = self._thread_id

        # [START get_chat_thread_client]
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
        chat_thread_client = chat_client.get_chat_thread_client(thread_id)
        # [END get_chat_thread_client]

        print("chat_thread_client created with thread id: ",
              chat_thread_client.thread_id)
Exemple #28
0
    async def delete_thread_async(self):
        token = self.token
        endpoint = self.endpoint
        thread_id = self._thread_id

        # [START delete_thread]
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential

        chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
        async with chat_client:

            await chat_client.delete_chat_thread(self._thread_id)
            # [END delete_thread]
            print("delete_thread succeeded")
Exemple #29
0
    async def list_threads_async(self):
        from azure.communication.chat.aio import ChatClient, CommunicationTokenCredential, CommunicationTokenRefreshOptions

        refresh_options = CommunicationTokenRefreshOptions(self.token)
        chat_client = ChatClient(self.endpoint, CommunicationTokenCredential(refresh_options))
        async with chat_client:
            # [START list_threads]
            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)
Exemple #30
0
    async def update_topic_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 update_topic]
            topic = "updated thread topic"
            await chat_thread_client.update_topic(topic=topic)
            # [END update_topic]

        print("update_topic succeeded")