Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
docs(samples): Adds snippet for configuring a webhook to enable an ag…
Browse files Browse the repository at this point in the history
…ent response. (#306)
  • Loading branch information
nicain committed May 3, 2022
1 parent 720c0bd commit c0cc924
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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: Configures a webhook to enable an agent response."""

# [START dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response]

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


def enable_agent_response(request):
"""A webhook to enable an agent response."""

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

# The value of the parameter used to enable agent response:
session_parameter = request_dict["sessionInfo"]["parameters"]["number"]

if tag == "increase number":
session_parameter += 100
text = f"The new increased value of the number parameter is {session_parameter}"
elif tag == "decrease number":
session_parameter -= 50
text = f"The new decreased value of the number parameter is {session_parameter}"

return {
"fulfillment_response": {
"messages": [
{
"text": {
"text": [
# fulfillment text response to be sent to the agent
text
],
},
},
],
},
"sessionInfo": {
"parameters": {
"number": session_parameter,
},
},
}


# [END dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response]
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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 webhook to enable an agent response."""

import flask
import pytest

from webhook_configure_session_parameters_enable_agent_response import (
enable_agent_response,
)


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


@pytest.mark.parametrize(
"tag,value,expected_value",
[
("increase number", 100, 200),
("decrease number", 100, 50),
],
)
def test_enable_agent_response(tag, value, expected_value, app):
"""Test for webhook to enable an agent response."""

request = {
"fulfillmentInfo": {"tag": tag},
"sessionInfo": {"parameters": {"number": value}},
}

if tag == "increase number":
expected_text = (
f"The new increased value of the number parameter is {expected_value}"
)
else:
expected_text = (
f"The new decreased value of the number parameter is {expected_value}"
)

with app.test_request_context(json=request):
res = enable_agent_response(flask.request)
assert (
res["fulfillment_response"]["messages"][0]["text"]["text"][0]
== expected_text
)
assert res["sessionInfo"]["parameters"]["number"] == expected_value

0 comments on commit c0cc924

Please sign in to comment.