示例#1
0
    def setUp(self):
        super(ChatThreadClientTest, self).setUp()
        self.recording_processors.extend([
            BodyReplacerProcessor(keys=[
                "id", "token", "senderId", "chatMessageId", "nextLink",
                "members", "multipleStatus", "value"
            ]),
            URIIdentityReplacer(),
            ChatURIReplacer()
        ])

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

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

        # create user and issue token
        self.user = self.identity_client.create_user()
        tokenresponse = self.identity_client.issue_token(self.user,
                                                         scopes=["chat"])
        self.token = tokenresponse.token

        # create another user
        self.new_user = self.identity_client.create_user()

        # create ChatClient
        self.chat_client = ChatClient(self.endpoint,
                                      CommunicationUserCredential(self.token))
示例#2
0
    def send_typing_notification(self):
        from azure.communication.chat import ChatThreadClient, CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [START send_typing_notification]
        chat_thread_client.send_typing_notification()
        # [END send_typing_notification]

        print("send_typing_notification succeeded")
示例#3
0
 def delete_message(self):
     from azure.communication.chat import ChatThreadClient
     from azure.communication.chat import CommunicationUserCredential
     chat_thread_client = ChatThreadClient(
         self.endpoint, CommunicationUserCredential(self.token),
         self._thread_id)
     # [START delete_message]
     chat_thread_client.delete_message(self._message_id)
     # [END delete_message]
     print("delete_chat_message succeeded")
示例#4
0
    def delete_thread(self):
        # [START delete_thread]
        from azure.communication.chat import ChatClient, CommunicationUserCredential

        chat_client = ChatClient(self.endpoint,
                                 CommunicationUserCredential(self.token))
        chat_client.delete_chat_thread(self._thread_id)
        # [END delete_thread]

        print("delete_thread succeeded")
示例#5
0
 def list_members(self):
     from azure.communication.chat import ChatThreadClient
     from azure.communication.chat import CommunicationUserCredential
     chat_thread_client = ChatThreadClient(
         self.endpoint, CommunicationUserCredential(self.token),
         self._thread_id)
     # [START list_members]
     chat_thread_members = chat_thread_client.list_members()
     print("list_chat_members succeeded, members: ")
     for chat_thread_member in chat_thread_members:
         print(chat_thread_member)
示例#6
0
 def list_read_receipts(self):
     from azure.communication.chat import ChatThreadClient
     from azure.communication.chat import CommunicationUserCredential
     chat_thread_client = ChatThreadClient(
         self.endpoint, CommunicationUserCredential(self.token),
         self._thread_id)
     # [START list_read_receipts]
     read_receipts = chat_thread_client.list_read_receipts()
     print("list_read_receipts succeeded, receipts:")
     for read_receipt in read_receipts:
         print(read_receipt)
示例#7
0
    def get_thread(self):
        # [START get_thread]
        from azure.communication.chat import ChatClient, CommunicationUserCredential

        chat_client = ChatClient(self.endpoint,
                                 CommunicationUserCredential(self.token))
        chat_thread = 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)
示例#8
0
    def send_read_receipt(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [START send_read_receipt]
        chat_thread_client.send_read_receipt(self._message_id)
        # [END send_read_receipt]

        print("send_read_receipt succeeded")
示例#9
0
    def remove_member(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential, CommunicationUser
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)

        # [START remove_member]
        chat_thread_client.remove_member(self.new_user)
        # [END remove_member]

        print("remove_chat_member succeeded")
示例#10
0
    def update_message(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [START update_message]
        content = "updated content"
        chat_thread_client.update_message(self._message_id, content=content)
        # [END update_message]

        print("update_chat_message succeeded")
示例#11
0
    def get_message(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [START get_message]
        chat_message = chat_thread_client.get_message(self._message_id)
        # [END get_message]

        print("get_chat_message succeeded, message id:", chat_message.id, \
            "content: ", chat_message.content)
示例#12
0
    def get_chat_thread_client(self):
        # [START get_chat_thread_client]
        from azure.communication.chat import ChatClient, CommunicationUserCredential

        chat_client = ChatClient(self.endpoint,
                                 CommunicationUserCredential(self.token))
        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)
示例#13
0
    def update_thread(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [START update_thread]
        topic = "updated thread topic"
        chat_thread_client.update_thread(topic=topic)
        # [END update_thread]

        print("update_chat_thread succeeded")
示例#14
0
    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")
示例#15
0
    def list_messages(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [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"
        )
        for chat_message_page in chat_messages.by_page():
            l = list(chat_message_page)
            print("page size: ", len(l))
示例#16
0
 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")
示例#17
0
    def list_threads(self):
        # [START list_threads]
        from azure.communication.chat import ChatClient, CommunicationUserCredential
        from datetime import datetime, timedelta
        import pytz

        chat_client = ChatClient(self.endpoint,
                                 CommunicationUserCredential(self.token))
        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."
        )
        for info in chat_thread_infos:
            print("thread id:", info.id)
示例#18
0
    def send_message(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationUserCredential
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationUserCredential(self.token),
            self._thread_id)
        # [START send_message]
        from azure.communication.chat import ChatMessagePriority

        priority = ChatMessagePriority.NORMAL
        content = 'hello world'
        sender_display_name = 'sender name'

        send_message_result = chat_thread_client.send_message(
            content,
            priority=priority,
            sender_display_name=sender_display_name)
        # [END send_message]

        self._message_id = send_message_result.id
        print("send_chat_message succeeded, message id:", self._message_id)
示例#19
0
 def create_chat_client(self):
     # [START create_chat_client]
     from azure.communication.chat import ChatClient, CommunicationUserCredential
     chat_client = ChatClient(self.endpoint,
                              CommunicationUserCredential(self.token))