Skip to content

Commit 58439d1

Browse files
authored
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
1 parent c10a605 commit 58439d1

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pytest==6.2.5
1+
pytest==6.2.5

Dialogflow-CX/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
google-cloud-dialogflow-cx==1.5.0
2+
Flask==2.0.1

Dialogflow-CX/webhook.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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]

Dialogflow-CX/webhook_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
"""Test webhook"""
15+
16+
import flask
17+
import pytest
18+
19+
from webhook import handle_webhook
20+
21+
# Create a fake 'app' for generating test request contexts.
22+
23+
request = {
24+
"fulfillmentInfo": {"tag": "Default Welcome Intent"}
25+
}
26+
27+
28+
@pytest.fixture(scope='module')
29+
def app():
30+
return flask.Flask(__name__)
31+
32+
33+
def test_handle_webhook(app):
34+
with app.test_request_context(json=request):
35+
res = handle_webhook(flask.request)
36+
assert 'Hi from a GCF Webhook' in str(res)

0 commit comments

Comments
 (0)