Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
docs: Dialogflow cx v3 detect intent event input snippet (#421)
Browse files Browse the repository at this point in the history
* docs: add detect event with event input snippet

* Update detect_intent_event_test.py
  • Loading branch information
nicain committed Jul 6, 2022
1 parent ac6aae4 commit 0524558
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
91 changes: 91 additions & 0 deletions samples/snippets/detect_intent_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python

# Copyright 2022 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.

"""DialogFlow Detects intent using EventInput."""


# [START dialogflow_cx_v3_detect_intent_event_input_async]
import uuid

from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient
from google.cloud.dialogflowcx_v3.types import session


def run_sample():
# TODO(developer): Update these values when running the function
# project_id = "YOUR-PROJECT-ID"
# location = "YOUR-LOCATION-ID"
# agent_id = "YOUR-AGENT-ID"
# event = "YOUR-EVENT"
# language_code = "YOUR-LANGUAGE-CODE"

project_id = 'dialogflow-cx-demo-1-348717'
location = 'global'
agent_id = '8caa6b47-5dd7-4380-b86e-ea4301d565b0'
event = 'sys.no-match-default'
language_code = 'en-us'

detect_intent_with_event_input(
project_id,
location,
agent_id,
event,
language_code,
)


def detect_intent_with_event_input(
project_id,
location,
agent_id,
event,
language_code,
):
"""Detects intent using EventInput"""
client_options = None
if location != "global":
api_endpoint = f"{location}-dialogflow.googleapis.com:443"
print(f"API Endpoint: {api_endpoint}\n")
client_options = {"api_endpoint": api_endpoint}
session_client = SessionsClient(client_options=client_options)
session_id = str(uuid.uuid4())
session_path = session_client.session_path(
project=project_id,
location=location,
agent=agent_id,
session=session_id,
)

# Construct detect intent request:
event = session.EventInput(event=event)
query_input = session.QueryInput(
event=event,
language_code=language_code
)
request = session.DetectIntentRequest(
session=session_path,
query_input=query_input,
)

response = session_client.detect_intent(request=request)
response_text = response.query_result.response_messages[0].text.text[0]
print(f'Response: {response_text}')
return response_text
# [END dialogflow_cx_v3_detect_intent_event_input_async]


if __name__ == "__main__":
run_sample()
48 changes: 48 additions & 0 deletions samples/snippets/detect_intent_event_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2022, 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.

"""Tests for detect_intent_with_sentiment_analysis.py"""

from __future__ import absolute_import

import os

from detect_intent_event import detect_intent_with_event_input


PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
AGENT_ID = os.getenv("AGENT_ID")


def test_detect_intent_positive():
response_text = detect_intent_with_event_input(
PROJECT_ID,
'global',
AGENT_ID,
'sys.no-match-default',
'en-us',
)
assert response_text in [
'Can you say that again?',
'I didn\'t get that. Can you repeat?',
'I didn\'t get that. Can you say it again?',
'I missed that, say that again?',
'I missed what you said. What was that?',
'One more time?',
'Say that one more time?',
'Sorry, can you say that again?',
'Sorry, could you say that again?',
'Sorry, I didn\'t get that. Can you rephrase?',
'Sorry, what was that?',
'What was that?',
]

0 comments on commit 0524558

Please sign in to comment.