def test_list_intents(self): # Setup Expected Response next_page_token = "" intents_element = {} intents = [intents_element] expected_response = { "next_page_token": next_page_token, "intents": intents } expected_response = intent_pb2.ListIntentsResponse(**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.IntentsClient() # Setup Request parent = client.project_agent_path("[PROJECT]") paged_list_response = client.list_intents(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.intents[0] == resources[0] assert len(channel.requests) == 1 expected_request = intent_pb2.ListIntentsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_intents(self): # Setup Expected Response next_page_token = '' intents_element = {} intents = [intents_element] expected_response = { 'next_page_token': next_page_token, 'intents': intents } expected_response = intent_pb2.ListIntentsResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_intents(parent) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.intents[0] == resources[0] assert len(channel.requests) == 1 expected_request = intent_pb2.ListIntentsRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_delete_intents(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name='operations/test_batch_delete_intents', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') intents = [] response = client.batch_delete_intents(parent, intents) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchDeleteIntentsRequest( parent=parent, intents=intents) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_update_intents(self): # Setup Expected Response expected_response = {} expected_response = intent_pb2.BatchUpdateIntentsResponse( **expected_response) operation = operations_pb2.Operation( name='operations/test_batch_update_intents', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') language_code = 'languageCode-412800396' response = client.batch_update_intents(parent, language_code) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchUpdateIntentsRequest( parent=parent, language_code=language_code) actual_request = channel.requests[0][1] assert expected_request == actual_request
def batch_create_intents(intents): """ This is used to insert multiple intents at the same time, much more efficient than running the create_intent multiple times. :param intents: A list of intents you want to create. :return: a simple int of """ intents_out = [] # A simple counter for how many intents we have inserted. counter = 0 for intent in intents: current_intent = create_intent_object(intent["intent_name"], intent["training_phrases"]) intents_out.append(current_intent) counter += 1 client = dialogflow_v2beta1.IntentsClient() parent = client.project_agent_path(PROJECT_ID) client.batch_update_intents(parent, "no", intent_batch_inline={"intents": intents_out}) return counter
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, )
def test_batch_delete_intents(self): # Setup Expected Response expected_response = {} expected_response = empty_pb2.Empty(**expected_response) operation = operations_pb2.Operation( name="operations/test_batch_delete_intents", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.IntentsClient() # Setup Request parent = client.project_agent_path("[PROJECT]") intents = [] response = client.batch_delete_intents(parent, intents) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = intent_pb2.BatchDeleteIntentsRequest( parent=parent, intents=intents) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_delete_intent_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup request name = client.intent_path('[PROJECT]', '[INTENT]') with pytest.raises(CustomException): client.delete_intent(name)
def test_list_intents_exception(self): channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup request parent = client.project_agent_path('[PROJECT]') paged_list_response = client.list_intents(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_update_intent_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup request intent = {} language_code = 'languageCode-412800396' with pytest.raises(CustomException): client.update_intent(intent, language_code)
def create_intent(intent_name, training_phrases): """ Create a single intent. :param intent_name: A simple string. :param training_phrases: A list containing multiple training_phrases. :return: the newly created intent. """ client = dialogflow_v2beta1.IntentsClient() parent = client.project_agent_path(PROJECT_ID) intent = create_intent_object(intent_name, training_phrases) return client.create_intent(parent, intent)
def test_create_intent_exception(self): # Mock the API response channel = ChannelStub(responses=[CustomException()]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup request parent = client.project_agent_path('[PROJECT]') intent = {} with pytest.raises(CustomException): client.create_intent(parent, intent)
def test_delete_intent_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.IntentsClient() # Setup request name = client.intent_path("[PROJECT]", "[INTENT]") with pytest.raises(CustomException): client.delete_intent(name)
def test_update_intent_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.IntentsClient() # Setup request intent = {} with pytest.raises(CustomException): client.update_intent(intent)
def test_list_intents_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.IntentsClient() # Setup request parent = client.project_agent_path("[PROJECT]") paged_list_response = client.list_intents(parent) with pytest.raises(CustomException): list(paged_list_response)
def test_delete_intent(self): channel = ChannelStub() client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request name = client.intent_path('[PROJECT]', '[INTENT]') client.delete_intent(name) assert len(channel.requests) == 1 expected_request = intent_pb2.DeleteIntentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_intent_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.IntentsClient() # Setup request parent = client.project_agent_path('[PROJECT]') intent = {} with pytest.raises(CustomException): client.create_intent(parent, intent)
def test_delete_intent(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.IntentsClient() # Setup Request name = client.intent_path("[PROJECT]", "[INTENT]") client.delete_intent(name) assert len(channel.requests) == 1 expected_request = intent_pb2.DeleteIntentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_intent(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' priority = 1165461084 is_fallback = False ml_enabled = False ml_disabled = True end_interaction = True action = 'action-1422950858' reset_contexts = True root_followup_intent_name = 'rootFollowupIntentName402253784' parent_followup_intent_name = 'parentFollowupIntentName-1131901680' expected_response = { 'name': name, 'display_name': display_name, 'priority': priority, 'is_fallback': is_fallback, 'ml_enabled': ml_enabled, 'ml_disabled': ml_disabled, 'end_interaction': end_interaction, 'action': action, 'reset_contexts': reset_contexts, 'root_followup_intent_name': root_followup_intent_name, 'parent_followup_intent_name': parent_followup_intent_name } expected_response = intent_pb2.Intent(**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.IntentsClient() # Setup Request intent = {} language_code = 'languageCode-412800396' response = client.update_intent(intent, language_code) assert expected_response == response assert len(channel.requests) == 1 expected_request = intent_pb2.UpdateIntentRequest( intent=intent, language_code=language_code) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_intent(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" priority = 1165461084 is_fallback = False ml_enabled = False ml_disabled = True end_interaction = True action = "action-1422950858" reset_contexts = True root_followup_intent_name = "rootFollowupIntentName402253784" parent_followup_intent_name = "parentFollowupIntentName-1131901680" expected_response = { "name": name, "display_name": display_name, "priority": priority, "is_fallback": is_fallback, "ml_enabled": ml_enabled, "ml_disabled": ml_disabled, "end_interaction": end_interaction, "action": action, "reset_contexts": reset_contexts, "root_followup_intent_name": root_followup_intent_name, "parent_followup_intent_name": parent_followup_intent_name, } expected_response = intent_pb2.Intent(**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.IntentsClient() # Setup Request intent = {} language_code = "languageCode-412800396" response = client.update_intent(intent, language_code) assert expected_response == response assert len(channel.requests) == 1 expected_request = intent_pb2.UpdateIntentRequest( intent=intent, language_code=language_code) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_intent(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' priority = 1165461084 is_fallback = False ml_enabled = False ml_disabled = True end_interaction = True action = 'action-1422950858' reset_contexts = True root_followup_intent_name = 'rootFollowupIntentName402253784' parent_followup_intent_name = 'parentFollowupIntentName-1131901680' expected_response = { 'name': name_2, 'display_name': display_name, 'priority': priority, 'is_fallback': is_fallback, 'ml_enabled': ml_enabled, 'ml_disabled': ml_disabled, 'end_interaction': end_interaction, 'action': action, 'reset_contexts': reset_contexts, 'root_followup_intent_name': root_followup_intent_name, 'parent_followup_intent_name': parent_followup_intent_name } expected_response = intent_pb2.Intent(**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.IntentsClient() # Setup Request name = client.intent_path('[PROJECT]', '[INTENT]') response = client.get_intent(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = intent_pb2.GetIntentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_intent(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" priority = 1165461084 is_fallback = False ml_enabled = False ml_disabled = True end_interaction = True action = "action-1422950858" reset_contexts = True root_followup_intent_name = "rootFollowupIntentName402253784" parent_followup_intent_name = "parentFollowupIntentName-1131901680" expected_response = { "name": name_2, "display_name": display_name, "priority": priority, "is_fallback": is_fallback, "ml_enabled": ml_enabled, "ml_disabled": ml_disabled, "end_interaction": end_interaction, "action": action, "reset_contexts": reset_contexts, "root_followup_intent_name": root_followup_intent_name, "parent_followup_intent_name": parent_followup_intent_name, } expected_response = intent_pb2.Intent(**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.IntentsClient() # Setup Request name = client.intent_path("[PROJECT]", "[INTENT]") response = client.get_intent(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = intent_pb2.GetIntentRequest(name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_update_intents_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_batch_update_intents_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request parent = client.agent_path('[PROJECT]', '[AGENT]') language_code = 'languageCode-412800396' response = client.batch_update_intents(parent, language_code) exception = response.exception() assert exception.errors[0] == error
def test_batch_delete_intents_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_batch_delete_intents_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') intents = [] response = client.batch_delete_intents(parent, intents) exception = response.exception() assert exception.errors[0] == error
def test_create_intent(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' priority = 1165461084 is_fallback = False ml_enabled = False ml_disabled = True end_interaction = True action = 'action-1422950858' reset_contexts = True root_followup_intent_name = 'rootFollowupIntentName402253784' parent_followup_intent_name = 'parentFollowupIntentName-1131901680' expected_response = { 'name': name, 'display_name': display_name, 'priority': priority, 'is_fallback': is_fallback, 'ml_enabled': ml_enabled, 'ml_disabled': ml_disabled, 'end_interaction': end_interaction, 'action': action, 'reset_contexts': reset_contexts, 'root_followup_intent_name': root_followup_intent_name, 'parent_followup_intent_name': parent_followup_intent_name } expected_response = intent_pb2.Intent(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = dialogflow_v2beta1.IntentsClient(channel=channel) # Setup Request parent = client.project_agent_path('[PROJECT]') intent = {} response = client.create_intent(parent, intent) assert expected_response == response assert len(channel.requests) == 1 expected_request = intent_pb2.CreateIntentRequest( parent=parent, intent=intent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_batch_update_intents_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name="operations/test_batch_update_intents_exception", done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.IntentsClient() # Setup Request parent = "parent-995424086" response = client.batch_update_intents(parent) exception = response.exception() assert exception.errors[0] == error
def test_batch_delete_intents_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_batch_delete_intents_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = dialogflow_v2beta1.IntentsClient() # Setup Request parent = client.project_agent_path('[PROJECT]') intents = [] response = client.batch_delete_intents(parent, intents) exception = response.exception() assert exception.errors[0] == error
def create_intent(project_id, display_name, training_phrases_parts, message_texts): """Create an intent of the given intent type.""" intents_client = dialogflow.IntentsClient() parent = intents_client.project_agent_path(project_id) training_phrases = [] for training_phrases_part in training_phrases_parts: part = dialogflow.types.Intent.TrainingPhrase.Part( text=training_phrases_part) # Here we create a new training phrase for each provided part. training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part]) training_phrases.append(training_phrase) text = dialogflow.types.Intent.Message.Text(text=message_texts) message = dialogflow.types.Intent.Message(text=text) intent = dialogflow.types.Intent( display_name=display_name, training_phrases=training_phrases, messages=[message]) response = intents_client.create_intent(parent, intent) return response
import dialogflow_v2beta1 client = dialogflow_v2beta1.IntentsClient() parent = client.project_agent_path('shoppingmalldemo') # Iterate over all results for element in client.list_intents(parent): print element
def get_all_intents(): client = dialogflow_v2beta1.IntentsClient() parent = client.project_agent_path(PROJECT_ID) return client.list_intents(parent)