From 74cfc9a7ab9b85fa05565c406534a3cd3391ab4f Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Thu, 14 Oct 2021 14:59:25 -0700 Subject: [PATCH] docs(samples): added webhook sample (#169) * docs(samples): added webhook sample * fixed webhoot test * failling test fix * used json * added flask to requirment.txt * Changed request handling * update test * fixed failing test * convert response to string * lint * made test clearer * Added Flask and changed get_json * Removed flask import * added request * Changed code * changed test * changed test * changed test * Changed test * test fix * test fix * lint fix * Revised Code * Lint fix * fixed requirments.txt * Revised Code * lint fix * lint fix * lint fix * revised code * lint fix --- samples/snippets/requirements-test.txt | 2 +- samples/snippets/requirements.txt | 1 + samples/snippets/webhook.py | 57 ++++++++++++++++++++++++++ samples/snippets/webhook_test.py | 36 ++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 samples/snippets/webhook.py create mode 100644 samples/snippets/webhook_test.py diff --git a/samples/snippets/requirements-test.txt b/samples/snippets/requirements-test.txt index 9299a7a8..92709451 100644 --- a/samples/snippets/requirements-test.txt +++ b/samples/snippets/requirements-test.txt @@ -1 +1 @@ -pytest==6.2.5 \ No newline at end of file +pytest==6.2.5 diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 722c4969..4d53d98c 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1 +1,2 @@ google-cloud-dialogflow-cx==1.5.0 +Flask==2.0.1 diff --git a/samples/snippets/webhook.py b/samples/snippets/webhook.py new file mode 100644 index 00000000..0d85280e --- /dev/null +++ b/samples/snippets/webhook.py @@ -0,0 +1,57 @@ +# 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. + +''' handle_webhook will return the correct fullfilment response dependong the tag that is sent in the request''' + +# [START dialogflow_cx_webhook] + + +def handle_webhook(request): + + req = request.get_json() + + tag = req["fulfillmentInfo"]["tag"] + + if tag == "Default Welcome Intent": + # You can also use the google.cloud.dialogflowcx_v3.types.WebhookRequest protos instead of manually writing the json object + # Please see https://googleapis.dev/python/dialogflow/latest/dialogflow_v2/types.html?highlight=webhookresponse#google.cloud.dialogflow_v2.types.WebhookResponse for an overview + res = { + "fulfillment_response": { + "messages": [{"text": {"text": ["Hi from a GCF Webhook"]}}] + } + } + elif tag == "get-name": + res = { + "fulfillment_response": { + "messages": [{"text": {"text": ["My name is Phlowhook"]}}] + } + } + else: + res = { + "fulfillment_response": { + "messages": [ + { + "text": { + "text": [ + f"There are no fulfillment responses defined for {tag} tag" + ] + } + } + ] + } + } + + # Returns json + return res + +# [END dialogflow_cx_webhook] diff --git a/samples/snippets/webhook_test.py b/samples/snippets/webhook_test.py new file mode 100644 index 00000000..dee58ddf --- /dev/null +++ b/samples/snippets/webhook_test.py @@ -0,0 +1,36 @@ +# 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 import handle_webhook + +# Create a fake 'app' for generating test request contexts. + +request = { + "fulfillmentInfo": {"tag": "Default Welcome Intent"} +} + + +@pytest.fixture(scope='module') +def app(): + return flask.Flask(__name__) + + +def test_handle_webhook(app): + with app.test_request_context(json=request): + res = handle_webhook(flask.request) + assert 'Hi from a GCF Webhook' in str(res)