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

Commit

Permalink
Merge branch 'main' into dialogflow_cx_detect_intent_with_disabled_we…
Browse files Browse the repository at this point in the history
…bhook
  • Loading branch information
nicain committed Jul 6, 2022
2 parents a186b5a + 0228912 commit 37134d0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
4 changes: 2 additions & 2 deletions samples/snippets/detect_intent_disabled_webhook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def test_detect_intent_positive():
for response_text in response_text_list:
assert response_text[0] in [
'You are welcome!',
'It\'s my pleasure',
'Anytime',
'It\'s my pleasure.',
'Anytime.',
'Of course.',
'It\'s my pleasure to serve you.',
]
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?',
]
2 changes: 1 addition & 1 deletion samples/snippets/detect_intent_synthesize_tts_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC
# Copyright 2020-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.
Expand Down

0 comments on commit 37134d0

Please sign in to comment.