Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

feat: add sample code for using regional Dialogflow endpoint #254

Merged
merged 4 commits into from Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions samples/snippets/detect_intent_texts_with_location.py
@@ -0,0 +1,93 @@
#!/usr/bin/env python

# Copyright 2017 Google LLC
martini9393 marked this conversation as resolved.
Show resolved Hide resolved
#
# 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.

"""DialogFlow API Detect Intent Python sample to use regional endpoint.

Examples:
python detect_intent_texts_with_location.py -h
python detect_intent_texts_with_location.py --project-id PROJECT_ID \
--location-id LOCATION_ID --session-id SESSION_ID \
"hello" "book a meeting room" "Mountain View"
"""

import argparse
import uuid


# [START dialogflow_detect_intent_text_with_location]
martini9393 marked this conversation as resolved.
Show resolved Hide resolved
def detect_intent_texts_with_location(project_id, location_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.

Using the same `session_id` between requests allows continuation
of the conversation."""
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient(client_options={
"api_endpoint": "{location}-dialogflow.googleapis.com".format(location=location_id)})

session = "projects/{project}/locations/{location}/agent/sessions/{session}".format(
martini9393 marked this conversation as resolved.
Show resolved Hide resolved
project=project_id, location=location_id, session=session_id)
print('Session path: {}\n'.format(session))

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

query_input = dialogflow.QueryInput(text=text_input)

response = session_client.detect_intent(
request={'session': session, 'query_input': query_input})

print('=' * 20)
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))
# [END dialogflow_detect_intent_text_with_location]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'--project-id',
help='Project/agent id. Required.',
required=True)
parser.add_argument(
'--location-id',
help='Location id. Required.',
required=True)
parser.add_argument(
'--session-id',
help='Identifier of the DetectIntent session. '
'Defaults to a random UUID.',
default=str(uuid.uuid4()))
parser.add_argument(
'--language-code',
help='Language code of the query. Defaults to "en-US".',
default='en-US')
parser.add_argument(
'texts',
nargs='+',
type=str,
help='Text inputs.')

args = parser.parse_args()

detect_intent_texts_with_location(
args.project_id, args.location_id, args.session_id, args.texts, args.language_code)
32 changes: 32 additions & 0 deletions samples/snippets/detect_intent_texts_with_location_test.py
@@ -0,0 +1,32 @@
# Copyright 2017, 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.

from __future__ import absolute_import

import os
import uuid

from detect_intent_texts_with_location import detect_intent_texts_with_location

PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
LOCATION_ID = 'europe-west2'
martini9393 marked this conversation as resolved.
Show resolved Hide resolved
SESSION_ID = 'test_{}'.format(uuid.uuid4())
TEXTS = ["hello", "book a meeting room", "Mountain View",
"tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"]


def test_detect_intent_texts_with_location(capsys):
detect_intent_texts_with_location(PROJECT_ID, LOCATION_ID, SESSION_ID, TEXTS, 'en-US')
out, _ = capsys.readouterr()

assert 'Fulfillment text: All set!' in out