@@ -64,15 +64,21 @@ def get_event_batching_query_params(account_id: str) -> Dict[str, Any]:
64
64
65
65
# Function to build generic properties for tracking events
66
66
def get_events_base_properties (
67
- setting : SettingsModel ,
68
67
event_name : str ,
69
68
visitor_user_agent : str = '' ,
70
69
ip_address : str = ''
71
70
) -> Dict [str , Any ]:
72
- sdk_key = setting .get_sdk_key ()
71
+ from ..services .settings_manager import SettingsManager
72
+ # Get the instance of SettingsManager
73
+ settings = SettingsManager .get_instance ()
74
+
75
+ # Fetch SDK key and account ID from the SettingsManager instance
76
+ sdk_key = settings .get_sdk_key ()
77
+ account_id = settings .get_account_id ()
78
+
73
79
return {
74
80
'en' : event_name ,
75
- 'a' : setting . get_account_id () ,
81
+ 'a' : account_id ,
76
82
'env' : sdk_key ,
77
83
'eTime' : get_current_unix_timestamp_in_millis (),
78
84
'random' : get_random_number (),
@@ -90,8 +96,9 @@ def _get_event_base_payload(
90
96
visitor_user_agent : str = '' ,
91
97
ip_address : str = ''
92
98
) -> Dict [str , Any ]:
93
- uuid_value = get_uuid (user_id , settings .get_account_id ())
94
- sdk_key = settings .get_sdk_key ()
99
+ from ..services .settings_manager import SettingsManager
100
+ uuid_value = get_uuid (user_id , SettingsManager .get_instance ().get_account_id ())
101
+ sdk_key = SettingsManager .get_instance ().get_sdk_key ()
95
102
properties = {
96
103
'd' : {
97
104
'msgId' : f"{ uuid_value } -{ get_current_unix_timestamp_in_millis ()} " ,
@@ -268,6 +275,76 @@ def send_post_api_request(properties: Dict[str, Any], payload: Dict[str, Any]):
268
275
),
269
276
)
270
277
278
+ # Function to construct the messaging event payload
279
+ def get_messaging_event_payload (message_type : str , message : str , event_name : str ) -> Dict [str , Any ]:
280
+ from ..services .settings_manager import SettingsManager
281
+ # Get user ID and properties
282
+ settings = SettingsManager .get_instance ()
283
+ user_id = f"{ settings .get_account_id ()} _{ settings .get_sdk_key ()} "
284
+ properties = _get_event_base_payload (None , user_id , event_name , None , None )
285
+
286
+ # Set the environment key and product
287
+ properties ['d' ]['event' ]['props' ]['vwo_envKey' ] = settings .get_sdk_key ()
288
+ properties ['d' ]['event' ]['props' ]['product' ] = "fme" # Assuming 'product' is a required field
289
+
290
+ # Set the message data
291
+ data = {
292
+ 'type' : message_type ,
293
+ 'content' : {
294
+ 'title' : message ,
295
+ 'dateTime' : get_current_unix_timestamp_in_millis ()
296
+ }
297
+ }
298
+
299
+ # Add data to the properties
300
+ properties ['d' ]['event' ]['props' ]['data' ] = data
301
+
302
+ return properties
303
+
304
+ def send_messaging_event (properties : Dict [str , Any ], payload : Dict [str , Any ]) -> Dict [str , Any ]:
305
+ network_instance = NetworkManager .get_instance ()
306
+
307
+ try :
308
+ # Prepare the request model
309
+ request = RequestModel (
310
+ Constants .HOST_NAME ,
311
+ 'POST' ,
312
+ UrlEnum .EVENTS .value ,
313
+ properties ,
314
+ payload ,
315
+ None ,
316
+ Constants .HTTPS_PROTOCOL ,
317
+ 443
318
+ )
319
+
320
+ # Flag to check if the event loop is initialized
321
+ event_loop_initialized = False
322
+ main_event_loop = None
323
+
324
+ # Start a new event loop in a separate thread if it hasn't been initialized yet
325
+ if not event_loop_initialized :
326
+ main_event_loop = asyncio .new_event_loop ()
327
+ threading .Thread (target = start_event_loop , args = (main_event_loop ,), daemon = True ).start ()
328
+ event_loop_initialized = True
329
+
330
+ # Submit the asynchronous POST request to the newly started event loop
331
+ asyncio .run_coroutine_threadsafe (network_instance .post_async (request ), main_event_loop )
332
+
333
+ # Return a success message
334
+ return {"success" : True , "message" : "Event sent successfully" }
335
+
336
+ except Exception as err :
337
+ # Log the error
338
+ LogManager .get_instance ().error (
339
+ error_messages .get ('NETWORK_CALL_FAILED' ).format (
340
+ method = 'POST' ,
341
+ err = err ,
342
+ )
343
+ )
344
+
345
+ # Return a failure message
346
+ return {"success" : False , "message" : "Failed to send event" }
347
+
271
348
# Function to start the event loop in a new thread
272
349
def start_event_loop (loop ):
273
350
# Set the provided loop as the current event loop for the new thread
0 commit comments