Files
oaip-project-app/utils/windows_toast.py
2025-05-21 07:55:46 +03:00

45 lines
1.6 KiB
Python

from win10toast import ToastNotifier
import asyncio
from typing import Dict, Any
async def show_notification(args: Dict[str, Any]) -> Dict[str, Any]:
"""
Displays a system notification in the Windows system tray (bottom-right corner).
Args:
args: Dictionary containing 'title' (str), 'message' (str), and optional 'duration' (int, in seconds).
Returns:
Dictionary with status message and input arguments.
Raises:
ValueError: If required 'title' or 'message' arguments are missing.
OSError: If the notification fails to display.
"""
try:
# Check for required arguments
title = args.get('title')
message = args.get('message')
if not title or not message:
raise ValueError("Both 'title' and 'message' are required in args")
# Optional duration (default to 5 seconds)
duration = args.get('duration', 5)
if not isinstance(duration, int) or duration < 1:
raise ValueError("'duration' must be a positive integer")
# Initialize the notifier
toaster = ToastNotifier()
# Show notification (run in a separate thread to avoid blocking)
await asyncio.get_event_loop().run_in_executor(
None,
lambda: toaster.show_toast(
title=title,
msg=message,
duration=duration,
threaded=True
)
)
return {"message": "Notification displayed successfully", "args": args}
except Exception as e:
raise OSError(f"Error displaying notification: {str(e)}")