def test_get_session_entity_type(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        expected_response = {'name': name_2}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **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_v2.SessionEntityTypesClient()

        # Setup Request
        name = client.session_entity_type_path('[PROJECT]', '[SESSION]',
                                               '[ENTITY_TYPE]')

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

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.GetSessionEntityTypeRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_session_entity_types(self):
        # Setup Expected Response
        next_page_token = ''
        session_entity_types_element = {}
        session_entity_types = [session_entity_types_element]
        expected_response = {
            'next_page_token': next_page_token,
            'session_entity_types': session_entity_types
        }
        expected_response = session_entity_type_pb2.ListSessionEntityTypesResponse(
            **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_v2.SessionEntityTypesClient()

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

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

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

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.ListSessionEntityTypesRequest(
            parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_session_entity_type(self):
        # Setup Expected Response
        name = 'name3373707'
        expected_response = {'name': name}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **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_v2.SessionEntityTypesClient()

        # Setup Request
        session_entity_type = {}

        response = client.update_session_entity_type(session_entity_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.UpdateSessionEntityTypeRequest(
            session_entity_type=session_entity_type)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 4
0
    def create_session_entity_type(project_id, session_id, entity_values,
                                   entity_type_display_name,
                                   entity_override_mode):
        """Create a session entity type with the given display name."""
        import dialogflow_v2 as dialogflow
        session_entity_types_client = dialogflow.SessionEntityTypesClient()

        session_path = session_entity_types_client.session_path(
            project_id, session_id)
        session_entity_type_name = (
            session_entity_types_client.session_entity_type_path(
                project_id, session_id, entity_type_display_name))

        # Here we use the entity value as the only synonym.
        entities = [
            dialogflow.types.EntityType.Entity(value=value, synonyms=[value])
            for value in entity_values
        ]
        session_entity_type = dialogflow.types.SessionEntityType(
            name=session_entity_type_name,
            entity_override_mode=entity_override_mode,
            entities=entities)

        response = session_entity_types_client.create_session_entity_type(
            session_path, session_entity_type)

        print('SessionEntityType created: \n\n{}'.format(response))
Exemplo n.º 5
0
    def test_create_session_entity_type(self):
        # Setup Expected Response
        name = "name3373707"
        expected_response = {"name": name}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **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_v2.SessionEntityTypesClient()

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

        response = client.create_session_entity_type(parent, session_entity_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.CreateSessionEntityTypeRequest(
            parent=parent, session_entity_type=session_entity_type
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 6
0
    def test_get_session_entity_type(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        expected_response = {"name": name_2}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **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_v2.SessionEntityTypesClient()

        # Setup Request
        name = "name3373707"

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

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.GetSessionEntityTypeRequest(
            name=name
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 7
0
    def test_list_session_entity_types_exception(self):
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.SessionEntityTypesClient(channel=channel)

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

        paged_list_response = client.list_session_entity_types(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
Exemplo n.º 8
0
    def test_update_session_entity_type_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.SessionEntityTypesClient(channel=channel)

        # Setup request
        session_entity_type = {}

        with pytest.raises(CustomException):
            client.update_session_entity_type(session_entity_type)
Exemplo n.º 9
0
    def test_delete_session_entity_type_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.SessionEntityTypesClient(channel=channel)

        # Setup request
        name = client.session_entity_type_path('[PROJECT]', '[SESSION]',
                                               '[ENTITY_TYPE]')

        with pytest.raises(CustomException):
            client.delete_session_entity_type(name)
Exemplo n.º 10
0
    def test_create_session_entity_type_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        client = dialogflow_v2.SessionEntityTypesClient(channel=channel)

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

        with pytest.raises(CustomException):
            client.create_session_entity_type(parent, session_entity_type)
Exemplo n.º 11
0
    def delete_session_entity_type(project_id, session_id,
                                   entity_type_display_name):
        """Delete session entity type with the given entity type display name."""
        import dialogflow_v2 as dialogflow
        session_entity_types_client = dialogflow.SessionEntityTypesClient()

        session_entity_type_name = (
            session_entity_types_client.session_entity_type_path(
                project_id, session_id, entity_type_display_name))

        session_entity_types_client.delete_session_entity_type(
            session_entity_type_name)
    def test_list_session_entity_types_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_v2.SessionEntityTypesClient()

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

        paged_list_response = client.list_session_entity_types(parent)
        with pytest.raises(CustomException):
            list(paged_list_response)
    def test_update_session_entity_type_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_v2.SessionEntityTypesClient()

        # Setup request
        session_entity_type = {}

        with pytest.raises(CustomException):
            client.update_session_entity_type(session_entity_type)
    def test_delete_session_entity_type_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_v2.SessionEntityTypesClient()

        # Setup request
        name = client.session_entity_type_path('[PROJECT]', '[SESSION]',
                                               '[ENTITY_TYPE]')

        with pytest.raises(CustomException):
            client.delete_session_entity_type(name)
Exemplo n.º 15
0
    def test_create_session_entity_type_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_v2.SessionEntityTypesClient()

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

        with pytest.raises(CustomException):
            client.create_session_entity_type(parent, session_entity_type)
Exemplo n.º 16
0
    def test_delete_session_entity_type(self):
        channel = ChannelStub()
        client = dialogflow_v2.SessionEntityTypesClient(channel=channel)

        # Setup Request
        name = client.session_entity_type_path('[PROJECT]', '[SESSION]',
                                               '[ENTITY_TYPE]')

        client.delete_session_entity_type(name)

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.DeleteSessionEntityTypeRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 17
0
def list_session_entity_types(project_id, session_id):
    import dialogflow_v2 as dialogflow
    session_entity_types_client = dialogflow.SessionEntityTypesClient()

    session_path = session_entity_types_client.session_path(
        project_id, session_id)

    session_entity_types = (
        session_entity_types_client.list_session_entity_types(session_path))

    print('SessionEntityTypes for session {}:\n'.format(session_path))
    for session_entity_type in session_entity_types:
        print('\tSessionEntityType name: {}'.format(session_entity_type.name))
        print('\tNumber of entities: {}\n'.format(
            len(session_entity_type.entities)))
    def test_delete_session_entity_type(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_v2.SessionEntityTypesClient()

        # Setup Request
        name = client.session_entity_type_path('[PROJECT]', '[SESSION]',
                                               '[ENTITY_TYPE]')

        client.delete_session_entity_type(name)

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.DeleteSessionEntityTypeRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 19
0
    def test_delete_session_entity_type(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_v2.SessionEntityTypesClient()

        # Setup Request
        name = "name3373707"

        client.delete_session_entity_type(name)

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.DeleteSessionEntityTypeRequest(
            name=name
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 20
0
    def test_update_session_entity_type(self):
        # Setup Expected Response
        name = 'name3373707'
        expected_response = {'name': name}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **expected_response)

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

        # Setup Request
        session_entity_type = {}

        response = client.update_session_entity_type(session_entity_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.UpdateSessionEntityTypeRequest(
            session_entity_type=session_entity_type)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 21
0
    def test_get_session_entity_type(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        expected_response = {'name': name_2}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **expected_response)

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

        # Setup Request
        name = client.session_entity_type_path('[PROJECT]', '[SESSION]',
                                               '[ENTITY_TYPE]')

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

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.GetSessionEntityTypeRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 22
0
    def test_create_session_entity_type(self):
        # Setup Expected Response
        name = 'name3373707'
        expected_response = {'name': name}
        expected_response = session_entity_type_pb2.SessionEntityType(
            **expected_response)

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

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

        response = client.create_session_entity_type(parent,
                                                     session_entity_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_entity_type_pb2.CreateSessionEntityTypeRequest(
            parent=parent, session_entity_type=session_entity_type)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 23
0
from app.log import Logger

import dialogflow_v2 as dialogflow

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    'mealworm5-630b8064ec70.json')
session_client = dialogflow.SessionsClient(credentials=credentials)
entity_client = dialogflow.SessionEntityTypesClient(credentials=credentials)


class DialogFlowController:
    def __init__(self, g_config):
        self.project_id = g_config['DIALOGFLOW']['PROJECT_ID']
        return

    def get_results(self, query, user_id, session):
        session = session_client.session_path(self.project_id,
                                              user_id + session)
        text_input = dialogflow.types.TextInput(text=query,
                                                language_code='ko_KR')
        query_input = dialogflow.types.QueryInput(text=text_input)
        response = session_client.detect_intent(session=session,
                                                query_input=query_input)

        Logger.log('[DF > get_intent] 인텐트 분석 완료.', 'INFO', '쿼리: %s' % query)

        entities = {}
        for e in response.query_result.parameters.fields:
            entities[e] = response.query_result.parameters.fields.get(