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
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
54 changes: 54 additions & 0 deletions samples/snippets/webhook_configure_session_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2022, 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.


""" DialogFlow CX: webhook to to configure new session parameters."""

# [START dialogflow_v3beta1_webhook_configure_session_parameters]

# TODO (developer): change entry point to configure_session_params in Cloud Function


def configure_session_params(request):
"""Webhook to validate or configure new session parameters."""

request_dict = request.get_json()
tag = request_dict["fulfillmentInfo"]["tag"]

new_session_parameter = "Hi, I am new!"
text = f"{new_session_parameter}. I'm a session parameter configured by the webhook. The webhook's tag is {tag}."

json_response = {
"fulfillment_response": {
"messages": [
{
"text": {
"text": [
# fulfillment text response to be sent to the agent
text
],
},
},
],
},
"sessionInfo": {
"parameters": {
"newSessionParameter": new_session_parameter,
},
},
}

return json_response


# [END dialogflow_v3beta1_webhook_configure_session_parameters]
41 changes: 41 additions & 0 deletions samples/snippets/webhook_configure_session_parameters_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2022, 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 configure new session parameters"""

import flask
import pytest

from webhook_configure_session_parameters import configure_session_params


@pytest.fixture(name="app", scope="module")
def fixture_app():
"""Flask fixture to pass a flask.Request to the test function."""
return flask.Flask(__name__)


def test_validate_parameter(app):
"""Parameterized test for configure new session parameters."""

request = {"fulfillmentInfo": {"tag": "MOCK_TAG"}}

with app.test_request_context(json=request):
res = configure_session_params(flask.request)
assert (
res["fulfillment_response"]["messages"][0]["text"]["text"][0]
== (
"Hi, I am new!. I'm a session parameter configured by the webhook. "
"The webhook's tag is MOCK_TAG."
)
)
4 changes: 2 additions & 2 deletions samples/snippets/webhook_validate_form_parameter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021, Google LLC
# Copyright 2022, 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
Expand All @@ -20,7 +20,7 @@


def validate_parameter(request):
"""# Webhook to validate or invalidate parameter based on conditions configured by the user."""
"""Webhook to validate or invalidate parameter based on conditions configured by the user."""

request_dict = request.get_json()
param_to_validate = request_dict["pageInfo"]["formInfo"]["parameterInfo"][0][
Expand Down
4 changes: 2 additions & 2 deletions samples/snippets/webhook_validate_form_parameter_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021, Google LLC
# Copyright 2022, 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
Expand All @@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test webhook"""
"""Test validate form parameter webhook snippet."""

import flask
import pytest
Expand Down