58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Abilitazione/sospensione notifiche Telegram per script cron meteo e report."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
from pathlib import Path
|
|
|
|
LOGGER = logging.getLogger("telegram_gate")
|
|
_CONFIG = Path(__file__).with_name("cron-alerts.env")
|
|
|
|
|
|
def _load_config() -> None:
|
|
if not _CONFIG.is_file():
|
|
return
|
|
try:
|
|
for line in _CONFIG.read_text(encoding="utf-8").splitlines():
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, value = line.split("=", 1)
|
|
key = key.strip()
|
|
value = value.strip().strip('"').strip("'")
|
|
if key and key not in os.environ:
|
|
os.environ[key] = value
|
|
except OSError as exc:
|
|
LOGGER.debug("Config non caricata: %s", exc)
|
|
|
|
|
|
_load_config()
|
|
|
|
|
|
def telegram_alerts_enabled() -> bool:
|
|
value = os.environ.get("LOOGLE_TELEGRAM_ALERTS", "1").strip().lower()
|
|
return value not in ("0", "off", "false", "no", "disabled")
|
|
|
|
|
|
def mirror_alert_to_web(
|
|
message: str,
|
|
category: str,
|
|
severity: str = "warning",
|
|
*,
|
|
is_html: bool = False,
|
|
) -> bool:
|
|
if not message or not category:
|
|
return False
|
|
try:
|
|
import sys
|
|
|
|
sys.path.insert(0, "/home/daniely/docker/shared")
|
|
from loogle_core.alert_dispatcher import mirror_to_web
|
|
|
|
return mirror_to_web(message, category, severity, is_html=is_html)
|
|
except Exception as exc:
|
|
LOGGER.debug("Mirror WebApp fallito (%s): %s", category, exc)
|
|
return False
|