def send_message(self):
        from azure.communication.chat import ChatThreadClient
        from azure.communication.chat import CommunicationTokenCredential, CommunicationTokenRefreshOptions
        refresh_options = CommunicationTokenRefreshOptions(self.token)
        chat_thread_client = ChatThreadClient(
            self.endpoint, CommunicationTokenCredential(refresh_options),
            self._thread_id)
        # [START send_message]
        from azure.communication.chat import ChatMessagePriority

        content = 'hello world'
        sender_display_name = 'sender name'

        send_message_result_id = chat_thread_client.send_message(
            content, sender_display_name=sender_display_name)

        send_message_result_w_type_id = 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_chat_message succeeded, message id:", self._message_id)
        print("send_message succeeded with type specified, message id:",
              send_message_result_w_type_id)
Ejemplo n.º 2
0
    def test_send_message(self):
        thread_id = "19:[email protected]"
        message_id = '1596823919339'
        raised = False

        def mock_send(*_, **__):
            return mock_response(status_code=201,
                                 json_payload={"id": message_id})

        chat_thread_client = ChatThreadClient("https://endpoint",
                                              TestChatThreadClient.credential,
                                              thread_id,
                                              transport=Mock(send=mock_send))

        create_message_result = None
        try:
            priority = ChatMessagePriority.NORMAL
            content = 'hello world'
            sender_display_name = 'sender name'

            create_message_result = chat_thread_client.send_message(
                content,
                priority=priority,
                sender_display_name=sender_display_name)
        except:
            raised = True

        self.assertFalse(raised, 'Expected is no excpetion raised')
        assert create_message_result.id == message_id
Ejemplo n.º 3
0
    def test_send_message_w_invalid_type_throws_error(self):
        thread_id = "19:[email protected]"
        message_id='1596823919339'
        raised = False
        message_str = "Hi I am Bob."

        # the payload is irrelevant - it'll fail before
        def mock_send(*_, **__):
            return mock_response(status_code=201, json_payload={
                        "id": message_id
                    })
        chat_thread_client = ChatThreadClient("https://endpoint", TestChatThreadClient.credential, thread_id, transport=Mock(send=mock_send))

        create_message_result = None

        chat_message_types = [ChatMessageType.PARTICIPANT_ADDED, ChatMessageType.PARTICIPANT_REMOVED,
                              ChatMessageType.TOPIC_UPDATED, "participant_added", "participant_removed", "topic_updated",
                              "ChatMessageType.TEXT", "ChatMessageType.HTML",
                              "ChatMessageType.PARTICIPANT_ADDED", "ChatMessageType.PARTICIPANT_REMOVED",
                              "ChatMessageType.TOPIC_UPDATED"]
        for chat_message_type in chat_message_types:
            try:
                content='hello world'
                sender_display_name='sender name'
                create_message_result_id = chat_thread_client.send_message(
                    content=content,
                    chat_message_type=chat_message_type,
                    sender_display_name=sender_display_name)
            except:
                raised = True

            self.assertTrue(raised, 'Expected is excpetion raised')
Ejemplo n.º 4
0
    def test_send_message_w_type(self):
        thread_id = "19:[email protected]"
        message_id='1596823919339'
        raised = False
        message_str = "Hi I am Bob."

        chat_message_types = [ChatMessageType.TEXT, ChatMessageType.HTML, "text", "html"]

        for chat_message_type in chat_message_types:

            def mock_send(*_, **__):
                return mock_response(status_code=201, json_payload={
                    "id": message_id,
                    "type": chat_message_type,
                    "sequenceId": "3",
                    "version": message_id,
                    "content": {
                        "message": message_str,
                        "topic": "Lunch Chat thread",
                        "participants": [
                            {
                                "id": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_0e59221d-0c1d-46ae-9544-c963ce56c10b",
                                "displayName": "Bob",
                                "shareHistoryTime": "2020-10-30T10:50:50Z"
                            }
                        ],
                        "initiator": "8:acs:8540c0de-899f-5cce-acb5-3ec493af3800_0e59221d-0c1d-46ae-9544-c963ce56c10b"
                    },
                    "senderDisplayName": "Bob",
                    "createdOn": "2021-01-27T01:37:33Z",
                    "senderId": "8:acs:46849534-eb08-4ab7-bde7-c36928cd1547_00000007-e155-1f06-1db7-3a3a0d00004b"
                })

            chat_thread_client = ChatThreadClient("https://endpoint", TestChatThreadClient.credential, thread_id,
                                                  transport=Mock(send=mock_send))

            try:
                content='hello world'
                sender_display_name='sender name'
                create_message_result_id = chat_thread_client.send_message(
                    content=content,
                    chat_message_type=chat_message_type,
                    sender_display_name=sender_display_name)
            except:
                raised = True

            self.assertFalse(raised, 'Expected is no excpetion raised')
            assert create_message_result_id == message_id
Ejemplo n.º 5
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)