agnes-the-ai-analyst/services/telegram_bot/dispatch.py
Petr f2d3d156e3 Move standalone services from server/ to services/
Extract 4 self-contained services into services/ module:
- server/telegram_bot/ -> services/telegram_bot/
- server/ws_gateway/ -> services/ws_gateway/
- server/corporate_memory/ -> services/corporate_memory/
- server/session_collector.py -> services/session_collector/

Each service now has its own systemd/ directory with .service and .timer files.
deploy.sh updated to auto-discover service units from services/*/systemd/*.

server/ now contains only deployment infrastructure (deploy.sh, setup scripts,
bin/ management tools, sudoers, nginx config).

All imports updated: webapp/app.py, server/bin/ scripts, systemd ExecStart paths.
2026-03-09 12:54:30 +01:00

45 lines
1.5 KiB
Python

"""
Shared notification dispatch to WebSocket gateway.
Used by both the Telegram bot and the webapp REST API to push
notifications to connected desktop app clients.
"""
import logging
import os
import time
import uuid
from . import config
logger = logging.getLogger(__name__)
WS_GATEWAY_SOCKET_PATH = "/run/ws-gateway/ws.sock"
def dispatch_to_ws_gateway(username: str, output: dict, script_name: str) -> None:
"""Dispatch notification to WebSocket gateway for desktop app clients."""
if not os.path.exists(WS_GATEWAY_SOCKET_PATH):
return
try:
import httpx
transport = httpx.HTTPTransport(uds=WS_GATEWAY_SOCKET_PATH)
with httpx.Client(transport=transport, timeout=10) as client:
notification = {
"id": str(uuid.uuid4()),
"title": output.get("title", ""),
"message": output.get("message", ""),
"script": script_name,
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
image_path = output.get("image_path", "")
if image_path and os.path.isfile(image_path):
filename = os.path.basename(image_path)
notification["image_url"] = f"/api/notifications/images/{filename}"
client.post(
"http://localhost/dispatch",
json={"user": username, "notification": notification},
)
except Exception:
logger.exception("WS gateway dispatch failed")