-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Adds snippet for configuring a webhook to enable an ag…
…ent response. (#306)
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
Dialogflow-CX/webhook_configure_session_parameters_enable_agent_response.py
This file contains 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,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] |
60 changes: 60 additions & 0 deletions
60
Dialogflow-CX/webhook_configure_session_parameters_enable_agent_response_test.py
This file contains 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,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 |