|
| 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] |
0 commit comments