|
| 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 | +''' handle_webhook will return the correct fullfilment response dependong the tag that is sent in the request''' |
| 15 | + |
| 16 | +# [START dialogflow_cx_webhook] |
| 17 | + |
| 18 | + |
| 19 | +def handle_webhook(request): |
| 20 | + |
| 21 | + req = request.get_json() |
| 22 | + |
| 23 | + tag = req["fulfillmentInfo"]["tag"] |
| 24 | + |
| 25 | + if tag == "Default Welcome Intent": |
| 26 | + # You can also use the google.cloud.dialogflowcx_v3.types.WebhookRequest protos instead of manually writing the json object |
| 27 | + # Please see https://googleapis.dev/python/dialogflow/latest/dialogflow_v2/types.html?highlight=webhookresponse#google.cloud.dialogflow_v2.types.WebhookResponse for an overview |
| 28 | + res = { |
| 29 | + "fulfillment_response": { |
| 30 | + "messages": [{"text": {"text": ["Hi from a GCF Webhook"]}}] |
| 31 | + } |
| 32 | + } |
| 33 | + elif tag == "get-name": |
| 34 | + res = { |
| 35 | + "fulfillment_response": { |
| 36 | + "messages": [{"text": {"text": ["My name is Phlowhook"]}}] |
| 37 | + } |
| 38 | + } |
| 39 | + else: |
| 40 | + res = { |
| 41 | + "fulfillment_response": { |
| 42 | + "messages": [ |
| 43 | + { |
| 44 | + "text": { |
| 45 | + "text": [ |
| 46 | + f"There are no fulfillment responses defined for {tag} tag" |
| 47 | + ] |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + # Returns json |
| 55 | + return res |
| 56 | + |
| 57 | +# [END dialogflow_cx_webhook] |
0 commit comments