This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
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 validating a form parameter. (#302)
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2021, 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 validate or invalidate form parameters snippet.""" | ||
|
||
# [START dialogflow_v3beta1_webhook_validate_form_parameter] | ||
|
||
# TODO (developer): change entry point to validate_parameter in Cloud Function | ||
|
||
|
||
def validate_parameter(request): | ||
"""# Webhook to validate or invalidate parameter based on conditions configured by the user.""" | ||
|
||
request_dict = request.get_json() | ||
param_to_validate = request_dict["pageInfo"]["formInfo"]["parameterInfo"][0][ | ||
"value" | ||
] | ||
|
||
if param_to_validate > 15: | ||
text = "That is too many! Please pick another number." | ||
param_state = "INVALID" | ||
else: | ||
text = "That is a number I can work with!" | ||
param_state = "VALID" | ||
|
||
json_response = { | ||
"fulfillment_response": { | ||
"messages": [ | ||
{ | ||
"text": { | ||
"text": [ | ||
text | ||
], # fulfillment text response to be sent to the agent | ||
}, | ||
}, | ||
], | ||
}, | ||
"page_info": { | ||
"form_info": { | ||
"parameter_info": [ | ||
{ | ||
"displayName": "paramToValidate", | ||
"required": True, | ||
"state": param_state, | ||
}, | ||
], | ||
}, | ||
}, | ||
"sessionInfo": { | ||
"parameters": { | ||
# Set session parameter to null if your agent needs to reprompt the user | ||
"paramToValidate": None | ||
}, | ||
}, | ||
} | ||
|
||
return json_response | ||
|
||
|
||
# [END dialogflow_v3beta1_webhook_validate_form_parameter] |
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,45 @@ | ||
# Copyright 2021, 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 webhook""" | ||
|
||
import flask | ||
import pytest | ||
|
||
from webhook_validate_form_parameter import validate_parameter | ||
|
||
|
||
@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, "That is a number I can work with!"), | ||
(16, "That is too many! Please pick another number."), | ||
], | ||
) | ||
def test_validate_parameter(value, expected_response, app): | ||
"""Parameterized test for validate form parameter webhook snippet.""" | ||
|
||
request = {"pageInfo": {"formInfo": {"parameterInfo": [{"value": value}]}}} | ||
|
||
with app.test_request_context(json=request): | ||
res = validate_parameter(flask.request) | ||
assert ( | ||
res["fulfillment_response"]["messages"][0]["text"]["text"][0] | ||
== expected_response | ||
) |