Skip to content

Commit bbef545

Browse files
authored
docs: Dialogflow cx v3 detect intent event input snippet (#421)
* docs: add detect event with event input snippet * Update detect_intent_event_test.py
1 parent bc45185 commit bbef545

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

Dialogflow-CX/detect_intent_event.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""DialogFlow Detects intent using EventInput."""
18+
19+
20+
# [START dialogflow_cx_v3_detect_intent_event_input_async]
21+
import uuid
22+
23+
from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient
24+
from google.cloud.dialogflowcx_v3.types import session
25+
26+
27+
def run_sample():
28+
# TODO(developer): Update these values when running the function
29+
# project_id = "YOUR-PROJECT-ID"
30+
# location = "YOUR-LOCATION-ID"
31+
# agent_id = "YOUR-AGENT-ID"
32+
# event = "YOUR-EVENT"
33+
# language_code = "YOUR-LANGUAGE-CODE"
34+
35+
project_id = 'dialogflow-cx-demo-1-348717'
36+
location = 'global'
37+
agent_id = '8caa6b47-5dd7-4380-b86e-ea4301d565b0'
38+
event = 'sys.no-match-default'
39+
language_code = 'en-us'
40+
41+
detect_intent_with_event_input(
42+
project_id,
43+
location,
44+
agent_id,
45+
event,
46+
language_code,
47+
)
48+
49+
50+
def detect_intent_with_event_input(
51+
project_id,
52+
location,
53+
agent_id,
54+
event,
55+
language_code,
56+
):
57+
"""Detects intent using EventInput"""
58+
client_options = None
59+
if location != "global":
60+
api_endpoint = f"{location}-dialogflow.googleapis.com:443"
61+
print(f"API Endpoint: {api_endpoint}\n")
62+
client_options = {"api_endpoint": api_endpoint}
63+
session_client = SessionsClient(client_options=client_options)
64+
session_id = str(uuid.uuid4())
65+
session_path = session_client.session_path(
66+
project=project_id,
67+
location=location,
68+
agent=agent_id,
69+
session=session_id,
70+
)
71+
72+
# Construct detect intent request:
73+
event = session.EventInput(event=event)
74+
query_input = session.QueryInput(
75+
event=event,
76+
language_code=language_code
77+
)
78+
request = session.DetectIntentRequest(
79+
session=session_path,
80+
query_input=query_input,
81+
)
82+
83+
response = session_client.detect_intent(request=request)
84+
response_text = response.query_result.response_messages[0].text.text[0]
85+
print(f'Response: {response_text}')
86+
return response_text
87+
# [END dialogflow_cx_v3_detect_intent_event_input_async]
88+
89+
90+
if __name__ == "__main__":
91+
run_sample()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2022, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
"""Tests for detect_intent_with_sentiment_analysis.py"""
15+
16+
from __future__ import absolute_import
17+
18+
import os
19+
20+
from detect_intent_event import detect_intent_with_event_input
21+
22+
23+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
24+
AGENT_ID = os.getenv("AGENT_ID")
25+
26+
27+
def test_detect_intent_positive():
28+
response_text = detect_intent_with_event_input(
29+
PROJECT_ID,
30+
'global',
31+
AGENT_ID,
32+
'sys.no-match-default',
33+
'en-us',
34+
)
35+
assert response_text in [
36+
'Can you say that again?',
37+
'I didn\'t get that. Can you repeat?',
38+
'I didn\'t get that. Can you say it again?',
39+
'I missed that, say that again?',
40+
'I missed what you said. What was that?',
41+
'One more time?',
42+
'Say that one more time?',
43+
'Sorry, can you say that again?',
44+
'Sorry, could you say that again?',
45+
'Sorry, I didn\'t get that. Can you rephrase?',
46+
'Sorry, what was that?',
47+
'What was that?',
48+
]

0 commit comments

Comments
 (0)