73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
"""
|
|
Example subscription function for System Errors.
|
|
This is a template that can be used as a reference for creating other subscription functions.
|
|
"""
|
|
|
|
from typing import List
|
|
from modules.datamodels.datamodelMessaging import (
|
|
MessagingEventParameters,
|
|
MessagingSubscriptionExecutionResult,
|
|
MessagingSubscriptionRegistration,
|
|
MessagingChannel
|
|
)
|
|
|
|
|
|
def execute(
|
|
eventParameters: MessagingEventParameters,
|
|
registrations: List[MessagingSubscriptionRegistration],
|
|
messagingService
|
|
) -> MessagingSubscriptionExecutionResult:
|
|
"""
|
|
Subscription-Funktion für System-Errors.
|
|
Erhält eventParameters vom Trigger und registrations bereits geholt.
|
|
|
|
Args:
|
|
eventParameters: Event-Parameter vom Trigger
|
|
registrations: Liste der aktiven Registrierungen für diese Subscription
|
|
messagingService: MessagingService-Instanz
|
|
|
|
Returns:
|
|
MessagingSubscriptionExecutionResult mit Status und Anzahl gesendeter Nachrichten
|
|
"""
|
|
# Gruppiere nach Channel
|
|
emailRegistrations = [r for r in registrations if r.channel == MessagingChannel.EMAIL]
|
|
smsRegistrations = [r for r in registrations if r.channel == MessagingChannel.SMS]
|
|
|
|
# Bereite Nachrichten vor (können pro Channel unterschiedlich sein)
|
|
triggerData = eventParameters.triggerData
|
|
errors = triggerData.get('errors', [])
|
|
timestamp = triggerData.get('timestamp', 'Unknown')
|
|
|
|
emailSubject = "System Error Report"
|
|
emailMessage = f"System errors detected at {timestamp}:\n\n{errors}"
|
|
|
|
smsMessage = f"System Error: {len(errors)} errors detected at {timestamp}"
|
|
|
|
messagesSent = 0
|
|
|
|
# Versende über sendMessage
|
|
for reg in emailRegistrations:
|
|
sendResult = messagingService.sendMessage(
|
|
subject=emailSubject,
|
|
message=emailMessage,
|
|
registration=reg
|
|
)
|
|
if sendResult.success:
|
|
messagesSent += 1
|
|
|
|
for reg in smsRegistrations:
|
|
sendResult = messagingService.sendMessage(
|
|
subject="", # SMS hat kein Subject
|
|
message=smsMessage,
|
|
registration=reg
|
|
)
|
|
if sendResult.success:
|
|
messagesSent += 1
|
|
|
|
return MessagingSubscriptionExecutionResult(
|
|
success=True,
|
|
messagesSent=messagesSent
|
|
)
|
|
|