Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.
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
galdawave Sep 14, 2021
017a2c3
fixed webhoot test
galdawave Sep 14, 2021
2cc5e5e
failling test fix
galdawave Sep 14, 2021
e4fc495
used json
galdawave Sep 14, 2021
34c7d23
added flask to requirment.txt
galdawave Sep 14, 2021
950a01d
Changed request handling
galdawave Sep 14, 2021
5bef866
update test
galdawave Sep 14, 2021
fe3f78b
fixed failing test
galdawave Sep 14, 2021
b8dc552
convert response to string
galdawave Sep 14, 2021
2687b93
lint
galdawave Sep 14, 2021
4dc65b1
made test clearer
galdawave Sep 14, 2021
a6ac279
Merge branch 'main' into webhook
galdawave Sep 22, 2021
62fcc38
Added Flask and changed get_json
galdawave Sep 24, 2021
8d30a60
Removed flask import
galdawave Sep 24, 2021
eb61063
added request
galdawave Sep 24, 2021
938c3a7
Changed code
galdawave Sep 24, 2021
78c3616
changed test
galdawave Sep 24, 2021
28aaa6f
changed test
galdawave Sep 24, 2021
b712814
changed test
galdawave Sep 24, 2021
a704cf8
Changed test
galdawave Sep 24, 2021
36d074d
test fix
galdawave Sep 27, 2021
cc0e638
test fix
galdawave Sep 27, 2021
f0ebed9
lint fix
galdawave Sep 27, 2021
2c30483
Merge branch 'main' into webhook
galdawave Sep 29, 2021
f18be50
Revised Code
galdawave Sep 29, 2021
795ea2c
Lint fix
galdawave Sep 29, 2021
7799552
Merge branch 'main' into webhook
galdawave Sep 30, 2021
dedd160
Merge branch 'main' into webhook
galdawave Oct 8, 2021
32fde4a
Merge branch 'main' into webhook
galdawave Oct 12, 2021
6d2ebe1
fixed requirments.txt
galdawave Oct 12, 2021
a31d6fe
Revised Code
galdawave Oct 13, 2021
a3276fe
Merge branch 'main' into webhook
galdawave Oct 13, 2021
858bdff
lint fix
galdawave Oct 13, 2021
9d2a9a2
lint fix
galdawave Oct 14, 2021
4b193fb
lint fix
galdawave Oct 14, 2021
6a3bd67
Merge branch 'main' into webhook
galdawave Oct 14, 2021
88f9af0
revised code
galdawave Oct 14, 2021
37f6e1a
lint fix
galdawave Oct 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest==6.2.5
pytest==6.2.5
1 change: 1 addition & 0 deletions samples/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
google-cloud-dialogflow-cx==1.5.0
Flask==2.0.1
57 changes: 57 additions & 0 deletions samples/snippets/webhook.py
Original file line number Diff line number Diff line change
@@ -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.

Comment thread
galdawave marked this conversation as resolved.
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 = {
Comment thread
galdawave marked this conversation as resolved.
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 = {
Comment thread
galdawave marked this conversation as resolved.
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
Original file line number Diff line number Diff line change
@@ -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)
Comment thread
galdawave marked this conversation as resolved.
Resolved