From b4bc32732d806afd9b02aef67f420ea4dbc8faf0 Mon Sep 17 00:00:00 2001 From: hjosiah <104866418+hjosiah@users.noreply.github.com> Date: Mon, 3 Oct 2022 15:44:19 -0500 Subject: [PATCH] feat: Adding snippet to extract SessionInfo (#457) This snippet provides an example of how to extract SessionInfo from WebhookRequests. Co-authored-by: Anthonios Partheniou --- Dialogflow-CX/webhook_log_session_info.py | 49 ++++++++++++++ .../webhook_log_session_info_test.py | 64 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 Dialogflow-CX/webhook_log_session_info.py create mode 100644 Dialogflow-CX/webhook_log_session_info_test.py diff --git a/Dialogflow-CX/webhook_log_session_info.py b/Dialogflow-CX/webhook_log_session_info.py new file mode 100644 index 000000000000..644927000264 --- /dev/null +++ b/Dialogflow-CX/webhook_log_session_info.py @@ -0,0 +1,49 @@ +# 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: webhook to log session ID for each request.""" + +# [START dialogflow_cx_v3_webhook_log_session_id] + +import re + + +def log_session_id_for_troubleshooting(request): + """Webhook will log session id corresponding to request.""" + + req = request.get_json() + # You can read more about SessionInfo at https://cloud.google.com/dialogflow/cx/docs/reference/rest/v3/SessionInfo + # Use a regex pattern to get the session ID + session_id_regex = r".+\/sessions\/(.+)" + session = req["sessionInfo"]["session"] + regex_match = re.search(session_id_regex, session) + session_id = regex_match.group(1) + + # Instead of printing, use the logging tools available to you + print(f"Debug Node: session ID = {session_id}") + + # Return a generic response + res = { + "fulfillment_response": { + "messages": [{ + "text": { + "text": [f"Request Session ID: {session_id}"] + } + }] + } + } + + # Returns json + return res + + +# [END dialogflow_cx_v3_webhook_log_session_id] diff --git a/Dialogflow-CX/webhook_log_session_info_test.py b/Dialogflow-CX/webhook_log_session_info_test.py new file mode 100644 index 000000000000..3c30056df40d --- /dev/null +++ b/Dialogflow-CX/webhook_log_session_info_test.py @@ -0,0 +1,64 @@ +# 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 validate form webhook request session ID snippet.""" + +import flask +import pytest + +from webhook_log_session_info import log_session_id_for_troubleshooting + + +@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.fixture +def session_id(): + return "d0bdaa0c-0d00-0000-b0eb-b00b0db000b0" + + +@pytest.fixture +def session_prefix(): + agent_id = "000000f0-f000-00b0-0000-af00d0e00000" + return f"projects/test_project/locations/us-central1/agents/{agent_id}" + + +@pytest.fixture +def session(session_prefix, session_id): + """Session string without environment path""" + return f"{session_prefix}/sessions/{session_id}" + + +@pytest.fixture +def env_session(session_prefix, session_id): + """Session string with environment path""" + environment = "0d0000f0-0aac-0d0c-0a00-b00b0000a000" + return f"{session_prefix}/environments/{environment}/sessions/{session_id}" + + +def test_logging_session_id(app, session, session_id): + """Parameterized test for regular session string.""" + request = {"sessionInfo": {"session": session}} + with app.test_request_context(json=request): + res = log_session_id_for_troubleshooting(flask.request) + assert session_id in str(res) + + +def test_logging_session_id_with_env_path(app, env_session, session_id): + """Parameterized test for session string with environment path.""" + request = {"sessionInfo": {"session": env_session}} + with app.test_request_context(json=request): + res = log_session_id_for_troubleshooting(flask.request) + assert session_id in str(res)