Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
docs(samples): Adds snippet for validating a form parameter. (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicain authored Apr 28, 2022
1 parent ad4c5d6 commit 8cfe6a1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
71 changes: 71 additions & 0 deletions samples/snippets/webhook_validate_form_parameter.py
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]
45 changes: 45 additions & 0 deletions samples/snippets/webhook_validate_form_parameter_test.py
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
)

0 comments on commit 8cfe6a1

Please sign in to comment.