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

docs(samples): added webhook sample #169

Merged
merged 38 commits into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8cc7f05
docs(samples): added webhook sample
galz10 Sep 14, 2021
017a2c3
fixed webhoot test
galz10 Sep 14, 2021
2cc5e5e
failling test fix
galz10 Sep 14, 2021
e4fc495
used json
galz10 Sep 14, 2021
34c7d23
added flask to requirment.txt
galz10 Sep 14, 2021
950a01d
Changed request handling
galz10 Sep 14, 2021
5bef866
update test
galz10 Sep 14, 2021
fe3f78b
fixed failing test
galz10 Sep 14, 2021
b8dc552
convert response to string
galz10 Sep 14, 2021
2687b93
lint
galz10 Sep 14, 2021
4dc65b1
made test clearer
galz10 Sep 14, 2021
a6ac279
Merge branch 'main' into webhook
galz10 Sep 22, 2021
62fcc38
Added Flask and changed get_json
galz10 Sep 24, 2021
8d30a60
Removed flask import
galz10 Sep 24, 2021
eb61063
added request
galz10 Sep 24, 2021
938c3a7
Changed code
galz10 Sep 24, 2021
78c3616
changed test
galz10 Sep 24, 2021
28aaa6f
changed test
galz10 Sep 24, 2021
b712814
changed test
galz10 Sep 24, 2021
a704cf8
Changed test
galz10 Sep 24, 2021
36d074d
test fix
galz10 Sep 27, 2021
cc0e638
test fix
galz10 Sep 27, 2021
f0ebed9
lint fix
galz10 Sep 27, 2021
2c30483
Merge branch 'main' into webhook
galz10 Sep 29, 2021
f18be50
Revised Code
galz10 Sep 29, 2021
795ea2c
Lint fix
galz10 Sep 29, 2021
7799552
Merge branch 'main' into webhook
galz10 Sep 30, 2021
dedd160
Merge branch 'main' into webhook
galz10 Oct 8, 2021
32fde4a
Merge branch 'main' into webhook
galz10 Oct 12, 2021
6d2ebe1
fixed requirments.txt
galz10 Oct 12, 2021
a31d6fe
Revised Code
galz10 Oct 13, 2021
a3276fe
Merge branch 'main' into webhook
galz10 Oct 13, 2021
858bdff
lint fix
galz10 Oct 13, 2021
9d2a9a2
lint fix
galz10 Oct 14, 2021
4b193fb
lint fix
galz10 Oct 14, 2021
6a3bd67
Merge branch 'main' into webhook
galz10 Oct 14, 2021
88f9af0
revised code
galz10 Oct 14, 2021
37f6e1a
lint fix
galz10 Oct 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

galz10 marked this conversation as resolved.
Show resolved Hide resolved
''' 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 = {
galz10 marked this conversation as resolved.
Show resolved Hide resolved
"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 = {
galz10 marked this conversation as resolved.
Show resolved Hide resolved
"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)
galz10 marked this conversation as resolved.
Show resolved Hide resolved