def test_create_context(self):
        # Setup Expected Response
        name = 'name3373707'
        lifespan_count = 1178775510
        expected_response = {'name': name, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')
        context = {}

        response = client.create_context(parent, context)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.CreateContextRequest(parent=parent,
                                                            context=context)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#2
0
    def test_list_contexts(self):
        # Setup Expected Response
        next_page_token = ''
        contexts_element = {}
        contexts = [contexts_element]
        expected_response = {
            'next_page_token': next_page_token,
            'contexts': contexts
        }
        expected_response = context_pb2.ListContextsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.contexts[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = context_pb2.ListContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_contexts(self):
        # Setup Expected Response
        next_page_token = ""
        contexts_element = {}
        contexts = [contexts_element]
        expected_response = {
            "next_page_token": next_page_token,
            "contexts": contexts
        }
        expected_response = context_pb2.ListContextsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        parent = "parent-995424086"

        paged_list_response = client.list_contexts(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.contexts[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = context_pb2.ListContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_create_context(self):
        # Setup Expected Response
        name = "name3373707"
        lifespan_count = 1178775510
        expected_response = {"name": name, "lifespan_count": lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        parent = "parent-995424086"
        context = {}

        response = client.create_context(parent, context)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.CreateContextRequest(parent=parent,
                                                            context=context)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_contexts(self):
        # Setup Expected Response
        next_page_token = ''
        contexts_element = {}
        contexts = [contexts_element]
        expected_response = {
            'next_page_token': next_page_token,
            'contexts': contexts
        }
        expected_response = context_pb2.ListContextsResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.contexts[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = context_pb2.ListContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#6
0
    def __init__(self,
                 project: str,
                 platform: int = enums.Intent.Message.Platform.LINE):
        self.project = project
        self.platform = platform
        self.intents_client = dialogflow.IntentsClient()
        self.contexts_client = dialogflow.ContextsClient()

        payload = Struct()
        payload.update({
            self.PLATFORMS[self.platform].lower():
            self.MORE_QUESTION_PAYLOAD
        })
        self.more_question_message = Intent.Message(
            payload=payload,
            platform=self.platform,
        )

        self.more_question_context = Context(
            name=self.contexts_client.context_path(
                project=self.project,
                session='-',
                context='more_question',
            ),
            lifespan_count=1,
        )
示例#7
0
    def test_update_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup request
        context = {}

        with pytest.raises(CustomException):
            client.update_context(context)
示例#8
0
    def test_delete_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        with pytest.raises(CustomException):
            client.delete_context(name)
示例#9
0
    def test_delete_all_contexts_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        with pytest.raises(CustomException):
            client.delete_all_contexts(parent)
示例#10
0
    def test_list_contexts_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
示例#11
0
def delete_context(PROJECT_ID, session_id, context_id):
    """
    Delete a context from the agent
    :param PROJECT_ID: the project id of the agent
    :param session_id: the current session id
    :param context_id: the context id
    """
    contexts_client = dialogflow.ContextsClient()
    context_name = contexts_client.context_path(PROJECT_ID, session_id,
                                                context_id)

    contexts_client.delete_context(context_name)
    def test_list_contexts_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup request
        parent = "parent-995424086"

        paged_list_response = client.list_contexts(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_delete_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup request
        name = "name3373707"

        with pytest.raises(CustomException):
            client.delete_context(name)
示例#14
0
    def test_delete_context(self):
        channel = ChannelStub()
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup Request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        client.delete_context(name)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_contexts_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        paged_list_response = client.list_contexts(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_update_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup request
        context = {}

        with pytest.raises(CustomException):
            client.update_context(context)
    def test_delete_context_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        with pytest.raises(CustomException):
            client.delete_context(name)
示例#18
0
    def test_delete_all_contexts(self):
        channel = ChannelStub()
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        client.delete_all_contexts(parent)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteAllContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_delete_all_contexts_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup request
        parent = client.session_path("[PROJECT]", "[SESSION]")

        with pytest.raises(CustomException):
            client.delete_all_contexts(parent)
    def test_delete_all_contexts(self):
        channel = ChannelStub()
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        parent = client.session_path('[PROJECT]', '[SESSION]')

        client.delete_all_contexts(parent)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteAllContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_delete_all_contexts(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        parent = "parent-995424086"

        client.delete_all_contexts(parent)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteAllContextsRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_delete_context(self):
        channel = ChannelStub()
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        name = client.context_path("[PROJECT]", "[SESSION]", "[CONTEXT]")

        client.delete_context(name)

        assert len(channel.requests) == 1
        expected_request = context_pb2.DeleteContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#23
0
def trigger_event(PROJECT_ID, session_id, language_code='en-US'):
    """Returns the result of detect intent with texts as inputs.

        called by post /welcome
    :parameter PROJECT_ID: the project id of the agent
    :parameter session_id: Using the same `session_id` maintains continuous conversation.
    :parameter language_code: language kind, default as en-US
    :return text_dict as response, invalid input returns msg
    """

    # session_client = dialogflow.SessionsClient()
    pathname = dirname(__file__) + '/tmp.json'
    session_client = dialogflow.SessionsClient.from_service_account_json(
        pathname)

    session = session_client.session_path(PROJECT_ID, session_id)
    print('Session path: {}\n'.format(session))

    # for text in texts:
    #     text_input = dialogflow.types.TextInput(
    #         text=text, language_code=language_code)

    text_input = dialogflow.types.TextInput(text=texts,
                                            language_code=language_code)
    contexts_client = dialogflow.ContextsClient()
    query_input = dialogflow.types.QueryInput(text=text_input)
    context_name = contexts_client.context_path(PROJECT_ID, session_id,
                                                context_id)
    context = dialogflow.types.Context(name=context_name, lifespan_count=1)
    query_params = dialogflow.types.QueryParameters(contexts=[context])
    try:
        response = session_client.detect_intent(session=session,
                                                query_input=query_input,
                                                query_params=query_params)
    except InvalidArgument:
        return "invalidArgument"

    print('Query text: {}'.format(response.query_result.query_text))
    print('Detected intent: {} (confidence: {})\n'.format(
        response.query_result.intent.display_name,
        response.query_result.intent_detection_confidence))
    print('Fulfillment text: {}\n'.format(
        response.query_result.fulfillment_text))
    list_contexts(PROJECT_ID, session_id)

    text_resp = str(response.query_result.fulfillment_text)
    return {'text': text_resp}
示例#24
0
def create_context(PROJECT_ID, session_id, context_id, lifespan_count):
    """
     Create a new context.
    :param PROJECT_ID: the project id of the agent
    :param session_id: the current session id
    :param context_id: the context id
    :param lifespan_count: the lifespan of this context
    """
    contexts_client = dialogflow.ContextsClient()

    session_path = contexts_client.session_path(PROJECT_ID, session_id)
    context_name = contexts_client.context_path(PROJECT_ID, session_id,
                                                context_id)

    context = dialogflow.types.Context(name=context_name,
                                       lifespan_count=lifespan_count)

    response = contexts_client.create_context(session_path, context)
    print('Context created: \n{}'.format(response))
示例#25
0
def list_contexts(PROJECT_ID, session_id):
    """
    Returns the a list of context in current session.
    :param PROJECT_ID: the project id of the agent
    :param session_id: the current session id
    :return: a list of contexts with each items in the form of a dict constituted of lifespan and field.
    """
    contexts_client = dialogflow.ContextsClient()
    session_path = contexts_client.session_path(PROJECT_ID, session_id)
    contexts = contexts_client.list_contexts(session_path)

    print('Contexts for session {}:\n'.format(session_path))
    for context in contexts:
        print('Context name: {}'.format(context.name))
        print('Lifespan count: {}'.format(context.lifespan_count))
        print('Fields:')
        for field, value in context.parameters.fields.items():
            if value.string_value:
                print('\t{}: {}'.format(field, value))
示例#26
0
    def test_get_context(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        lifespan_count = 1178775510
        expected_response = {'name': name_2, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup Request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        response = client.get_context(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.GetContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#27
0
    def test_update_context(self):
        # Setup Expected Response
        name = 'name3373707'
        lifespan_count = 1178775510
        expected_response = {'name': name, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2beta1.ContextsClient(channel=channel)

        # Setup Request
        context = {}

        response = client.update_context(context)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.UpdateContextRequest(context=context)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_context(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        lifespan_count = 1178775510
        expected_response = {"name": name_2, "lifespan_count": lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        name = "name3373707"

        response = client.get_context(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.GetContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_context(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        lifespan_count = 1178775510
        expected_response = {'name': name_2, 'lifespan_count': lifespan_count}
        expected_response = context_pb2.Context(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2beta1.ContextsClient()

        # Setup Request
        name = client.context_path('[PROJECT]', '[SESSION]', '[CONTEXT]')

        response = client.get_context(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = context_pb2.GetContextRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request