Skip to content

Commit b4bc327

Browse files
hjosiahparthea
andauthored
feat: Adding snippet to extract SessionInfo (#457)
This snippet provides an example of how to extract SessionInfo from WebhookRequests. Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent fefd09d commit b4bc327

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
""" DialogFlow CX: webhook to log session ID for each request."""
14+
15+
# [START dialogflow_cx_v3_webhook_log_session_id]
16+
17+
import re
18+
19+
20+
def log_session_id_for_troubleshooting(request):
21+
"""Webhook will log session id corresponding to request."""
22+
23+
req = request.get_json()
24+
# You can read more about SessionInfo at https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3/SessionInfo
25+
# Use a regex pattern to get the session ID
26+
session_id_regex = r".+\/sessions\/(.+)"
27+
session = req["sessionInfo"]["session"]
28+
regex_match = re.search(session_id_regex, session)
29+
session_id = regex_match.group(1)
30+
31+
# Instead of printing, use the logging tools available to you
32+
print(f"Debug Node: session ID = {session_id}")
33+
34+
# Return a generic response
35+
res = {
36+
"fulfillment_response": {
37+
"messages": [{
38+
"text": {
39+
"text": [f"Request Session ID: {session_id}"]
40+
}
41+
}]
42+
}
43+
}
44+
45+
# Returns json
46+
return res
47+
48+
49+
# [END dialogflow_cx_v3_webhook_log_session_id]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
"""Test validate form webhook request session ID snippet."""
14+
15+
import flask
16+
import pytest
17+
18+
from webhook_log_session_info import log_session_id_for_troubleshooting
19+
20+
21+
@pytest.fixture(name="app", scope="module")
22+
def fixture_app():
23+
"""Flask fixture to pass a flask.Request to the test function"""
24+
return flask.Flask(__name__)
25+
26+
27+
@pytest.fixture
28+
def session_id():
29+
return "d0bdaa0c-0d00-0000-b0eb-b00b0db000b0"
30+
31+
32+
@pytest.fixture
33+
def session_prefix():
34+
agent_id = "000000f0-f000-00b0-0000-af00d0e00000"
35+
return f"projects/test_project/locations/us-central1/agents/{agent_id}"
36+
37+
38+
@pytest.fixture
39+
def session(session_prefix, session_id):
40+
"""Session string without environment path"""
41+
return f"{session_prefix}/sessions/{session_id}"
42+
43+
44+
@pytest.fixture
45+
def env_session(session_prefix, session_id):
46+
"""Session string with environment path"""
47+
environment = "0d0000f0-0aac-0d0c-0a00-b00b0000a000"
48+
return f"{session_prefix}/environments/{environment}/sessions/{session_id}"
49+
50+
51+
def test_logging_session_id(app, session, session_id):
52+
"""Parameterized test for regular session string."""
53+
request = {"sessionInfo": {"session": session}}
54+
with app.test_request_context(json=request):
55+
res = log_session_id_for_troubleshooting(flask.request)
56+
assert session_id in str(res)
57+
58+
59+
def test_logging_session_id_with_env_path(app, env_session, session_id):
60+
"""Parameterized test for session string with environment path."""
61+
request = {"sessionInfo": {"session": env_session}}
62+
with app.test_request_context(json=request):
63+
res = log_session_id_for_troubleshooting(flask.request)
64+
assert session_id in str(res)

0 commit comments

Comments
 (0)