This repository was archived by the owner on Jan 6, 2024. It is now read-only.
Repository navigation
docs(samples): added webhook sample #169
Merged
Merged
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 017a2c3
fixed webhoot test
galdawave 2cc5e5e
failling test fix
galdawave e4fc495
used json
galdawave 34c7d23
added flask to requirment.txt
galdawave 950a01d
Changed request handling
galdawave 5bef866
update test
galdawave fe3f78b
fixed failing test
galdawave b8dc552
convert response to string
galdawave 2687b93
lint
galdawave 4dc65b1
made test clearer
galdawave a6ac279
Merge branch 'main' into webhook
galdawave 62fcc38
Added Flask and changed get_json
galdawave 8d30a60
Removed flask import
galdawave eb61063
added request
galdawave 938c3a7
Changed code
galdawave 78c3616
changed test
galdawave 28aaa6f
changed test
galdawave b712814
changed test
galdawave a704cf8
Changed test
galdawave 36d074d
test fix
galdawave cc0e638
test fix
galdawave f0ebed9
lint fix
galdawave 2c30483
Merge branch 'main' into webhook
galdawave f18be50
Revised Code
galdawave 795ea2c
Lint fix
galdawave 7799552
Merge branch 'main' into webhook
galdawave dedd160
Merge branch 'main' into webhook
galdawave 32fde4a
Merge branch 'main' into webhook
galdawave 6d2ebe1
fixed requirments.txt
galdawave a31d6fe
Revised Code
galdawave a3276fe
Merge branch 'main' into webhook
galdawave 858bdff
lint fix
galdawave 9d2a9a2
lint fix
galdawave 4b193fb
lint fix
galdawave 6a3bd67
Merge branch 'main' into webhook
galdawave 88f9af0
revised code
galdawave 37f6e1a
lint fix
galdawave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 = { | ||
|
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 = { | ||
|
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] | ||
This file contains hidden or 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) | ||
|
galdawave marked this conversation as resolved.
Resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.