-
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.
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>
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
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,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] |
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,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) |