-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pytest==6.2.5 | ||
pytest==6.2.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
google-cloud-dialogflow-cx==1.5.0 | ||
Flask==2.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
''' 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |