Skip to content

Commit d65329c

Browse files
authored
docs(samples): Adds snippet for configuring optional or required form parameters (#305)
1 parent e2b77bf commit d65329c

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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: webhook to configure optional or required form parameters."""
16+
17+
# [START dialogflow_v3beta1_webhook_configure_optional_or_required_form_params]
18+
19+
# TODO (developer): change entry point to configure_optional_form_param in Cloud Function
20+
21+
22+
def configure_optional_form_param(request):
23+
"""Webhook to configure optional or required form parameters."""
24+
25+
request_dict = request.get_json()
26+
form_parameter = request_dict["pageInfo"]["formInfo"]["parameterInfo"][0]["value"]
27+
is_param_required = True
28+
param_state = "VALID"
29+
30+
if form_parameter <= 15:
31+
text = f"{form_parameter} is a number I can work with!"
32+
33+
if form_parameter > 15 and form_parameter < 20:
34+
text = f"{form_parameter} is too many, but it's okay. Let's move on."
35+
is_param_required = False
36+
else:
37+
text = f"{form_parameter} isn't going to work for me. Please try again!"
38+
param_state = "INVALID"
39+
form_parameter = None
40+
41+
json_response = {
42+
"fulfillment_response": {
43+
"messages": [
44+
{
45+
"text": {
46+
"text": [
47+
# fulfillment text response to be sent to the agent
48+
text
49+
],
50+
},
51+
},
52+
],
53+
},
54+
"pageInfo": {
55+
"formInfo": {
56+
"parameterInfo": [
57+
{
58+
"displayName": form_parameter,
59+
# if required: false, the agent will not reprompt for
60+
# this parameter, even if the state is 'INVALID'
61+
"required": is_param_required,
62+
"state": param_state,
63+
},
64+
],
65+
},
66+
},
67+
"sessionInfo": {
68+
"parameterInfo": {
69+
# Set session parameter to null if you want to reprompt
70+
# the user to enter a required parameter
71+
"formParameter": form_parameter,
72+
},
73+
},
74+
}
75+
return json_response
76+
77+
78+
# [END dialogflow_v3beta1_webhook_configure_optional_or_required_form_params]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 optional or required form param."""
15+
16+
import flask
17+
import pytest
18+
19+
from webhook_configure_optional_or_required_form_parameters import (
20+
configure_optional_form_param,
21+
)
22+
23+
24+
@pytest.fixture(name="app", scope="module")
25+
def fixture_app():
26+
"""Flask fixture to pass a flask.Request to the test function."""
27+
return flask.Flask(__name__)
28+
29+
30+
@pytest.mark.parametrize(
31+
"value,form_parameter,required,state,text",
32+
[
33+
(10, None, True, "INVALID", "10 isn't going to work for me. Please try again!"),
34+
(17, 17, False, "VALID", "17 is too many, but it's okay. Let's move on."),
35+
(25, None, True, "INVALID", "25 isn't going to work for me. Please try again!"),
36+
],
37+
)
38+
def test_validate_parameter(value, form_parameter, required, state, text, app):
39+
"""Test for configure optional or required form param."""
40+
41+
request = {"pageInfo": {"formInfo": {"parameterInfo": [{"value": value}]}}}
42+
43+
with app.test_request_context(json=request):
44+
res = configure_optional_form_param(flask.request)
45+
assert res["sessionInfo"]["parameterInfo"]["formParameter"] == form_parameter
46+
assert (
47+
res["pageInfo"]["formInfo"]["parameterInfo"][0]["displayName"]
48+
== form_parameter
49+
)
50+
assert res["pageInfo"]["formInfo"]["parameterInfo"][0]["required"] == required
51+
assert res["pageInfo"]["formInfo"]["parameterInfo"][0]["state"] == state
52+
assert res["fulfillment_response"]["messages"][0]["text"]["text"][0] == text

0 commit comments

Comments
 (0)