Skip to content

Commit 938c167

Browse files
authored
docs(samples): Adds snippet for validating a form parameter. (#302)
1 parent a2e936a commit 938c167

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2021, 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: webhook to validate or invalidate form parameters snippet."""
16+
17+
# [START dialogflow_v3beta1_webhook_validate_form_parameter]
18+
19+
# TODO (developer): change entry point to validate_parameter in Cloud Function
20+
21+
22+
def validate_parameter(request):
23+
"""# Webhook to validate or invalidate parameter based on conditions configured by the user."""
24+
25+
request_dict = request.get_json()
26+
param_to_validate = request_dict["pageInfo"]["formInfo"]["parameterInfo"][0][
27+
"value"
28+
]
29+
30+
if param_to_validate > 15:
31+
text = "That is too many! Please pick another number."
32+
param_state = "INVALID"
33+
else:
34+
text = "That is a number I can work with!"
35+
param_state = "VALID"
36+
37+
json_response = {
38+
"fulfillment_response": {
39+
"messages": [
40+
{
41+
"text": {
42+
"text": [
43+
text
44+
], # fulfillment text response to be sent to the agent
45+
},
46+
},
47+
],
48+
},
49+
"page_info": {
50+
"form_info": {
51+
"parameter_info": [
52+
{
53+
"displayName": "paramToValidate",
54+
"required": True,
55+
"state": param_state,
56+
},
57+
],
58+
},
59+
},
60+
"sessionInfo": {
61+
"parameters": {
62+
# Set session parameter to null if your agent needs to reprompt the user
63+
"paramToValidate": None
64+
},
65+
},
66+
}
67+
68+
return json_response
69+
70+
71+
# [END dialogflow_v3beta1_webhook_validate_form_parameter]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2021, 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 webhook"""
15+
16+
import flask
17+
import pytest
18+
19+
from webhook_validate_form_parameter import validate_parameter
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, "That is a number I can work with!"),
32+
(16, "That is too many! Please pick another number."),
33+
],
34+
)
35+
def test_validate_parameter(value, expected_response, app):
36+
"""Parameterized test for validate form parameter webhook snippet."""
37+
38+
request = {"pageInfo": {"formInfo": {"parameterInfo": [{"value": value}]}}}
39+
40+
with app.test_request_context(json=request):
41+
res = validate_parameter(flask.request)
42+
assert (
43+
res["fulfillment_response"]["messages"][0]["text"]["text"][0]
44+
== expected_response
45+
)

0 commit comments

Comments
 (0)