-
Notifications
You must be signed in to change notification settings - Fork 1.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): Adds snippet for configuring optional or required form…
… parameters (#305)
- Loading branch information
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...-dialogflow-cx/samples/snippets/webhook_configure_optional_or_required_form_parameters.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,78 @@ | ||
# 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: webhook to configure optional or required form parameters.""" | ||
|
||
# [START dialogflow_v3beta1_webhook_configure_optional_or_required_form_params] | ||
|
||
# TODO (developer): change entry point to configure_optional_form_param in Cloud Function | ||
|
||
|
||
def configure_optional_form_param(request): | ||
"""Webhook to configure optional or required form parameters.""" | ||
|
||
request_dict = request.get_json() | ||
form_parameter = request_dict["pageInfo"]["formInfo"]["parameterInfo"][0]["value"] | ||
is_param_required = True | ||
param_state = "VALID" | ||
|
||
if form_parameter <= 15: | ||
text = f"{form_parameter} is a number I can work with!" | ||
|
||
if form_parameter > 15 and form_parameter < 20: | ||
text = f"{form_parameter} is too many, but it's okay. Let's move on." | ||
is_param_required = False | ||
else: | ||
text = f"{form_parameter} isn't going to work for me. Please try again!" | ||
param_state = "INVALID" | ||
form_parameter = None | ||
|
||
json_response = { | ||
"fulfillment_response": { | ||
"messages": [ | ||
{ | ||
"text": { | ||
"text": [ | ||
# fulfillment text response to be sent to the agent | ||
text | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
"pageInfo": { | ||
"formInfo": { | ||
"parameterInfo": [ | ||
{ | ||
"displayName": form_parameter, | ||
# if required: false, the agent will not reprompt for | ||
# this parameter, even if the state is 'INVALID' | ||
"required": is_param_required, | ||
"state": param_state, | ||
}, | ||
], | ||
}, | ||
}, | ||
"sessionInfo": { | ||
"parameterInfo": { | ||
# Set session parameter to null if you want to reprompt | ||
# the user to enter a required parameter | ||
"formParameter": form_parameter, | ||
}, | ||
}, | ||
} | ||
return json_response | ||
|
||
|
||
# [END dialogflow_v3beta1_webhook_configure_optional_or_required_form_params] |
52 changes: 52 additions & 0 deletions
52
...ogflow-cx/samples/snippets/webhook_configure_optional_or_required_form_parameters_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,52 @@ | ||
# 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 optional or required form param.""" | ||
|
||
import flask | ||
import pytest | ||
|
||
from webhook_configure_optional_or_required_form_parameters import ( | ||
configure_optional_form_param, | ||
) | ||
|
||
|
||
@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,form_parameter,required,state,text", | ||
[ | ||
(10, None, True, "INVALID", "10 isn't going to work for me. Please try again!"), | ||
(17, 17, False, "VALID", "17 is too many, but it's okay. Let's move on."), | ||
(25, None, True, "INVALID", "25 isn't going to work for me. Please try again!"), | ||
], | ||
) | ||
def test_validate_parameter(value, form_parameter, required, state, text, app): | ||
"""Test for configure optional or required form param.""" | ||
|
||
request = {"pageInfo": {"formInfo": {"parameterInfo": [{"value": value}]}}} | ||
|
||
with app.test_request_context(json=request): | ||
res = configure_optional_form_param(flask.request) | ||
assert res["sessionInfo"]["parameterInfo"]["formParameter"] == form_parameter | ||
assert ( | ||
res["pageInfo"]["formInfo"]["parameterInfo"][0]["displayName"] | ||
== form_parameter | ||
) | ||
assert res["pageInfo"]["formInfo"]["parameterInfo"][0]["required"] == required | ||
assert res["pageInfo"]["formInfo"]["parameterInfo"][0]["state"] == state | ||
assert res["fulfillment_response"]["messages"][0]["text"]["text"][0] == text |