Skip to content

Commit e2b77bf

Browse files
authored
docs(samples): Configure session parameters trigger transition (#304)
* chore: Typo fixes * docs(samples): Adds snippet for configuring new session parameters trigger transition.
1 parent ac0b67f commit e2b77bf

4 files changed

+115
-2
lines changed

Dialogflow-CX/webhook_configure_session_parameters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# limitations under the License.
1313

1414

15-
""" DialogFlow CX: webhook to to configure new session parameters."""
15+
""" DialogFlow CX: webhook to configure new session parameters."""
1616

1717
# [START dialogflow_v3beta1_webhook_configure_session_parameters]
1818

Dialogflow-CX/webhook_configure_session_parameters_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def fixture_app():
2626

2727

2828
def test_validate_parameter(app):
29-
"""Parameterized test for configure new session parameters."""
29+
"""Test for configure new session parameters."""
3030

3131
request = {"fulfillmentInfo": {"tag": "MOCK_TAG"}}
3232

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
15+
""" DialogFlow CX: Configure new session parameters with trigger transition."""
16+
17+
# [START dialogflow_v3beta1_webhook_configure_session_parameters_trigger_transition]
18+
19+
# TODO (developer): change entry point to trigger_transition in Cloud Function
20+
21+
22+
def trigger_transition(request):
23+
"""Webhook to validate or configure new session parameters."""
24+
25+
request_dict = request.get_json()
26+
session_parameter = request_dict["sessionInfo"]["parameters"].get("value", 25)
27+
28+
if session_parameter > 15:
29+
text = f"You said {session_parameter}. Let me redirect you to our higher number department"
30+
target_page = (
31+
"projects/<Project ID>/locations/<Location ID>/"
32+
"agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>"
33+
)
34+
else:
35+
text = f"{session_parameter} is a number I can help you with!"
36+
target_page = (
37+
"projects/<Project ID>/locations/<Location ID>/"
38+
"agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>"
39+
)
40+
41+
json_response = {
42+
"target_page": target_page,
43+
"fulfillment_response": {
44+
"messages": [
45+
{
46+
"text": {
47+
"text": [
48+
# fulfillment text response to be sent to the agent
49+
text
50+
],
51+
},
52+
},
53+
],
54+
},
55+
}
56+
57+
return json_response
58+
59+
60+
# [END dialogflow_v3beta1_webhook_configure_session_parameters_trigger_transition]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
"""Test configure new session parameters trigger transition."""
15+
16+
import flask
17+
import pytest
18+
19+
from webhook_configure_session_parameters_trigger_transition import trigger_transition
20+
21+
22+
@pytest.fixture(name="app", scope="module")
23+
def fixture_app():
24+
"""Flask fixture to pass a flask.Request to the test function."""
25+
return flask.Flask(__name__)
26+
27+
28+
@pytest.mark.parametrize(
29+
"value,expected_response",
30+
[
31+
(15, "15 is a number I can help you with!"),
32+
(None, "You said 25. Let me redirect you to our higher number department"),
33+
(30, "You said 30. Let me redirect you to our higher number department"),
34+
],
35+
)
36+
def test_trigger_transition(value, expected_response, app):
37+
"""Parameterized test for configure new session parameters with trigger transition."""
38+
39+
if not value:
40+
request = {"sessionInfo": {"parameters": {}}}
41+
else:
42+
request = {"sessionInfo": {"parameters": {"value": value}}}
43+
44+
with app.test_request_context(json=request):
45+
res = trigger_transition(flask.request)
46+
assert res["target_page"] == (
47+
"projects/<Project ID>/locations/<Location ID>/"
48+
"agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>"
49+
)
50+
assert (
51+
res["fulfillment_response"]["messages"][0]["text"]["text"][0]
52+
== expected_response
53+
)

0 commit comments

Comments
 (0)