Skip to content

Commit ac0b67f

Browse files
authored
docs(samples): Configure session parameters snippet (#303)
* chore: update copyright year * chore: Cleans up stray # in docstring. * docs(samples): Adds snippet for configuring new session parameters. * Update module docstring for the validate form parameter snippet.
1 parent 938c167 commit ac0b67f

4 files changed

+99
-4
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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: webhook to to configure new session parameters."""
16+
17+
# [START dialogflow_v3beta1_webhook_configure_session_parameters]
18+
19+
# TODO (developer): change entry point to configure_session_params in Cloud Function
20+
21+
22+
def configure_session_params(request):
23+
"""Webhook to validate or configure new session parameters."""
24+
25+
request_dict = request.get_json()
26+
tag = request_dict["fulfillmentInfo"]["tag"]
27+
28+
new_session_parameter = "Hi, I am new!"
29+
text = f"{new_session_parameter}. I'm a session parameter configured by the webhook. The webhook's tag is {tag}."
30+
31+
json_response = {
32+
"fulfillment_response": {
33+
"messages": [
34+
{
35+
"text": {
36+
"text": [
37+
# fulfillment text response to be sent to the agent
38+
text
39+
],
40+
},
41+
},
42+
],
43+
},
44+
"sessionInfo": {
45+
"parameters": {
46+
"newSessionParameter": new_session_parameter,
47+
},
48+
},
49+
}
50+
51+
return json_response
52+
53+
54+
# [END dialogflow_v3beta1_webhook_configure_session_parameters]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 configure new session parameters"""
15+
16+
import flask
17+
import pytest
18+
19+
from webhook_configure_session_parameters import configure_session_params
20+
21+
22+
@pytest.fixture(name="app", scope="module")
23+
def fixture_app():
24+
"""Flask fixture to pass a flask.Request to the test function."""
25+
return flask.Flask(__name__)
26+
27+
28+
def test_validate_parameter(app):
29+
"""Parameterized test for configure new session parameters."""
30+
31+
request = {"fulfillmentInfo": {"tag": "MOCK_TAG"}}
32+
33+
with app.test_request_context(json=request):
34+
res = configure_session_params(flask.request)
35+
assert (
36+
res["fulfillment_response"]["messages"][0]["text"]["text"][0]
37+
== (
38+
"Hi, I am new!. I'm a session parameter configured by the webhook. "
39+
"The webhook's tag is MOCK_TAG."
40+
)
41+
)

Dialogflow-CX/webhook_validate_form_parameter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021, Google LLC
1+
# Copyright 2022, Google LLC
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -20,7 +20,7 @@
2020

2121

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

2525
request_dict = request.get_json()
2626
param_to_validate = request_dict["pageInfo"]["formInfo"]["parameterInfo"][0][

Dialogflow-CX/webhook_validate_form_parameter_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021, Google LLC
1+
# Copyright 2022, Google LLC
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -11,7 +11,7 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
"""Test webhook"""
14+
"""Test validate form parameter webhook snippet."""
1515

1616
import flask
1717
import pytest

0 commit comments

Comments
 (0)