Skip to content

Commit

Permalink
docs(samples): added webhook sample (#169)
Browse files Browse the repository at this point in the history
* 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 authored Oct 14, 2021
1 parent c10a605 commit 58439d1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dialogflow-CX/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 Dialogflow-CX/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 Dialogflow-CX/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.

''' 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 Dialogflow-CX/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)

0 comments on commit 58439d1

Please sign in to comment.