Skip to content

Commit b6a7b61

Browse files
authored
docs(samples): Adds snippet for configuring a webhook to enable an agent response. (#306)
1 parent d65329c commit b6a7b61

2 files changed

+119
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2022, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
15+
""" DialogFlow CX: Configures a webhook to enable an agent response."""
16+
17+
# [START dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response]
18+
19+
# TODO (developer): change entry point to enable_agent_response in Cloud Function
20+
21+
22+
def enable_agent_response(request):
23+
"""A webhook to enable an agent response."""
24+
25+
request_dict = request.get_json()
26+
tag = request_dict["fulfillmentInfo"]["tag"]
27+
28+
# The value of the parameter used to enable agent response:
29+
session_parameter = request_dict["sessionInfo"]["parameters"]["number"]
30+
31+
if tag == "increase number":
32+
session_parameter += 100
33+
text = f"The new increased value of the number parameter is {session_parameter}"
34+
elif tag == "decrease number":
35+
session_parameter -= 50
36+
text = f"The new decreased value of the number parameter is {session_parameter}"
37+
38+
return {
39+
"fulfillment_response": {
40+
"messages": [
41+
{
42+
"text": {
43+
"text": [
44+
# fulfillment text response to be sent to the agent
45+
text
46+
],
47+
},
48+
},
49+
],
50+
},
51+
"sessionInfo": {
52+
"parameters": {
53+
"number": session_parameter,
54+
},
55+
},
56+
}
57+
58+
59+
# [END dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2022, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
"""Test webhook to enable an agent response."""
15+
16+
import flask
17+
import pytest
18+
19+
from webhook_configure_session_parameters_enable_agent_response import (
20+
enable_agent_response,
21+
)
22+
23+
24+
@pytest.fixture(name="app", scope="module")
25+
def fixture_app():
26+
"""Flask fixture to pass a flask.Request to the test function."""
27+
return flask.Flask(__name__)
28+
29+
30+
@pytest.mark.parametrize(
31+
"tag,value,expected_value",
32+
[
33+
("increase number", 100, 200),
34+
("decrease number", 100, 50),
35+
],
36+
)
37+
def test_enable_agent_response(tag, value, expected_value, app):
38+
"""Test for webhook to enable an agent response."""
39+
40+
request = {
41+
"fulfillmentInfo": {"tag": tag},
42+
"sessionInfo": {"parameters": {"number": value}},
43+
}
44+
45+
if tag == "increase number":
46+
expected_text = (
47+
f"The new increased value of the number parameter is {expected_value}"
48+
)
49+
else:
50+
expected_text = (
51+
f"The new decreased value of the number parameter is {expected_value}"
52+
)
53+
54+
with app.test_request_context(json=request):
55+
res = enable_agent_response(flask.request)
56+
assert (
57+
res["fulfillment_response"]["messages"][0]["text"]["text"][0]
58+
== expected_text
59+
)
60+
assert res["sessionInfo"]["parameters"]["number"] == expected_value

0 commit comments

Comments
 (0)