11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
3+ import base64
34import json
45from abc import ABC , abstractmethod
6+ from _sha256 import sha256
57
68
79class TelemetryProcessor (ABC ):
@@ -11,8 +13,9 @@ class TelemetryProcessor(ABC):
1113 def activity_json (self ) -> json :
1214 """Retrieve the request body as json (Activity)."""
1315 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
1619
1720 @abstractmethod
1821 def can_process (self ) -> bool :
@@ -67,15 +70,34 @@ def __call__(self, data, context) -> bool:
6770 conversation = (
6871 post_data ["conversation" ] if "conversation" in post_data else None
6972 )
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.
7182 context .user .id = channel_id + user_id
72- context .session .id = conversation_id
7383
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
7590 if "id" in post_data :
7691 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
7795 if "channelId" in post_data :
7896 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
79100 if "type" in post_data :
80101 data .properties ["activityType" ] = post_data ["type" ]
102+
81103 return True
0 commit comments