-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Configure session parameters trigger transition (#304)
* chore: Typo fixes * docs(samples): Adds snippet for configuring new session parameters trigger transition.
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
Dialogflow-CX/webhook_configure_session_parameters_trigger_transition.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# 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 CX: Configure new session parameters with trigger transition.""" | ||
|
||
# [START dialogflow_v3beta1_webhook_configure_session_parameters_trigger_transition] | ||
|
||
# TODO (developer): change entry point to trigger_transition in Cloud Function | ||
|
||
|
||
def trigger_transition(request): | ||
"""Webhook to validate or configure new session parameters.""" | ||
|
||
request_dict = request.get_json() | ||
session_parameter = request_dict["sessionInfo"]["parameters"].get("value", 25) | ||
|
||
if session_parameter > 15: | ||
text = f"You said {session_parameter}. Let me redirect you to our higher number department" | ||
target_page = ( | ||
"projects/<Project ID>/locations/<Location ID>/" | ||
"agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>" | ||
) | ||
else: | ||
text = f"{session_parameter} is a number I can help you with!" | ||
target_page = ( | ||
"projects/<Project ID>/locations/<Location ID>/" | ||
"agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>" | ||
) | ||
|
||
json_response = { | ||
"target_page": target_page, | ||
"fulfillment_response": { | ||
"messages": [ | ||
{ | ||
"text": { | ||
"text": [ | ||
# fulfillment text response to be sent to the agent | ||
text | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
|
||
return json_response | ||
|
||
|
||
# [END dialogflow_v3beta1_webhook_configure_session_parameters_trigger_transition] |
53 changes: 53 additions & 0 deletions
53
Dialogflow-CX/webhook_configure_session_parameters_trigger_transition_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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. | ||
|
||
"""Test configure new session parameters trigger transition.""" | ||
|
||
import flask | ||
import pytest | ||
|
||
from webhook_configure_session_parameters_trigger_transition import trigger_transition | ||
|
||
|
||
@pytest.fixture(name="app", scope="module") | ||
def fixture_app(): | ||
"""Flask fixture to pass a flask.Request to the test function.""" | ||
return flask.Flask(__name__) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value,expected_response", | ||
[ | ||
(15, "15 is a number I can help you with!"), | ||
(None, "You said 25. Let me redirect you to our higher number department"), | ||
(30, "You said 30. Let me redirect you to our higher number department"), | ||
], | ||
) | ||
def test_trigger_transition(value, expected_response, app): | ||
"""Parameterized test for configure new session parameters with trigger transition.""" | ||
|
||
if not value: | ||
request = {"sessionInfo": {"parameters": {}}} | ||
else: | ||
request = {"sessionInfo": {"parameters": {"value": value}}} | ||
|
||
with app.test_request_context(json=request): | ||
res = trigger_transition(flask.request) | ||
assert res["target_page"] == ( | ||
"projects/<Project ID>/locations/<Location ID>/" | ||
"agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>" | ||
) | ||
assert ( | ||
res["fulfillment_response"]["messages"][0]["text"]["text"][0] | ||
== expected_response | ||
) |