1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
+ import base64
3
4
import json
4
5
from abc import ABC , abstractmethod
6
+ from _sha256 import sha256
5
7
6
8
7
9
class TelemetryProcessor (ABC ):
@@ -11,8 +13,9 @@ class TelemetryProcessor(ABC):
11
13
def activity_json (self ) -> json :
12
14
"""Retrieve the request body as json (Activity)."""
13
15
body_text = self .get_request_body ()
14
- body = json .loads (body_text ) if body_text is not None else None
15
- return body
16
+ if body_text :
17
+ return body_text if isinstance (body_text , dict ) else json .loads (body_text )
18
+ return None
16
19
17
20
@abstractmethod
18
21
def can_process (self ) -> bool :
@@ -67,15 +70,34 @@ def __call__(self, data, context) -> bool:
67
70
conversation = (
68
71
post_data ["conversation" ] if "conversation" in post_data else None
69
72
)
70
- conversation_id = conversation ["id" ] if "id" in conversation else None
73
+
74
+ session_id = ""
75
+ if "id" in conversation :
76
+ conversation_id = conversation ["id" ]
77
+ session_id = base64 .b64encode (
78
+ sha256 (conversation_id .encode ("utf-8" )).digest ()
79
+ ).decode ()
80
+
81
+ # Set the user id on the Application Insights telemetry item.
71
82
context .user .id = channel_id + user_id
72
- context .session .id = conversation_id
73
83
74
- # Additional bot-specific properties
84
+ # Set the session id on the Application Insights telemetry item.
85
+ # Hashed ID is used due to max session ID length for App Insights session Id
86
+ context .session .id = session_id
87
+
88
+ # Set the activity id:
89
+ # https://github.com/Microsoft/botframework-obi/blob/master/botframework-activity/botframework-activity.md#id
75
90
if "id" in post_data :
76
91
data .properties ["activityId" ] = post_data ["id" ]
92
+
93
+ # Set the channel id:
94
+ # https://github.com/Microsoft/botframework-obi/blob/master/botframework-activity/botframework-activity.md#channel-id
77
95
if "channelId" in post_data :
78
96
data .properties ["channelId" ] = post_data ["channelId" ]
97
+
98
+ # Set the activity type:
99
+ # https://github.com/Microsoft/botframework-obi/blob/master/botframework-activity/botframework-activity.md#type
79
100
if "type" in post_data :
80
101
data .properties ["activityType" ] = post_data ["type" ]
102
+
81
103
return True
0 commit comments