From df77ff90b05f87f0576f02eb7f9d5e50950554d4 Mon Sep 17 00:00:00 2001 From: Franklin Nunez <69214580+b-loved-dreamer@users.noreply.github.com> Date: Wed, 20 Oct 2021 10:55:58 -0700 Subject: [PATCH] docs(samples): adds list training phrases sample (#196) --- Dialogflow-CX/list_training_phrases.py | 41 +++++++++++++++++++++ Dialogflow-CX/list_training_phrases_test.py | 34 +++++++++++++++++ Dialogflow-CX/noxfile_config.py | 1 + 3 files changed, 76 insertions(+) create mode 100644 Dialogflow-CX/list_training_phrases.py create mode 100644 Dialogflow-CX/list_training_phrases_test.py diff --git a/Dialogflow-CX/list_training_phrases.py b/Dialogflow-CX/list_training_phrases.py new file mode 100644 index 000000000000..11f7417f489e --- /dev/null +++ b/Dialogflow-CX/list_training_phrases.py @@ -0,0 +1,41 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START dialogflow_list_training_phrases] + + +def list_training_phrases(project_id, agent_id, intent_id, location): + """Returns all training phrases for a specified intent.""" + + from google.cloud import dialogflowcx + + # Create the intents client + intent_client = dialogflowcx.IntentsClient() + + # Specify working intent + intent_name = intent_client.intent_path(project_id, location, agent_id, intent_id) + + # Compose the get-intent request + get_intent_request = dialogflowcx.GetIntentRequest(name=intent_name) + + intent = intent_client.get_intent(get_intent_request) + + # Iterate through the training phrases. + for phrase in intent.training_phrases: + print(phrase) + + return intent.training_phrases + + +# [END dialogflow_list_training_phrases] diff --git a/Dialogflow-CX/list_training_phrases_test.py b/Dialogflow-CX/list_training_phrases_test.py new file mode 100644 index 000000000000..94d4e74c2d84 --- /dev/null +++ b/Dialogflow-CX/list_training_phrases_test.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import google.auth + +import list_training_phrases + + +_, PROJECT_ID = google.auth.default() +INTENT_ID = os.getenv("INTENT_ID") +LOCATION = "global" +AGENT_ID = os.getenv("AGENT_ID") + + +def test_list_training_phrases(capsys): + training_phrases = list_training_phrases.list_training_phrases( + PROJECT_ID, AGENT_ID, INTENT_ID, LOCATION + ) + assert len(training_phrases) >= 15 # Number of training phrases at this point. diff --git a/Dialogflow-CX/noxfile_config.py b/Dialogflow-CX/noxfile_config.py index 6562b1479010..995dc281d748 100644 --- a/Dialogflow-CX/noxfile_config.py +++ b/Dialogflow-CX/noxfile_config.py @@ -35,5 +35,6 @@ "AGENT_ID": "53516802-3e2a-4016-80b6-a3df0d240240", "AGENT_ID_US_CENTRAL1": "edf8372c-c66a-4984-83ba-b85885e95e2a", "AUDIO_PATH": "resources/hello.wav", + "INTENT_ID": "164428bd-647a-4e30-ab0f-cc7f3e3b76f9", }, }