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

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
galz10 committed Oct 14, 2021
1 parent 778b86f commit 74cfc9a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion samples/snippets/requirements-test.txt
@@ -1 +1 @@
pytest==6.2.5
pytest==6.2.5
1 change: 1 addition & 0 deletions samples/snippets/requirements.txt
@@ -1 +1,2 @@
google-cloud-dialogflow-cx==1.5.0
Flask==2.0.1
57 changes: 57 additions & 0 deletions 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]
36 changes: 36 additions & 0 deletions 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)

0 comments on commit 74cfc9a

Please sign in to comment.