| """Telegram bot configuration. | |
| All values come from environment variables at import time — there is no .env | |
| file parsing. On HuggingFace Spaces set these as Space secrets/variables. | |
| """ | |
| import logging | |
| import os | |
| logger = logging.getLogger(__name__) | |
| class BotConfig: | |
| # Reuses the same env vars as the core alerting pipeline | |
| BOT_TOKEN: str = os.getenv("TELEGRAM_BOT_TOKEN", "") | |
| ADMIN_CHAT_ID: str = os.getenv("TELEGRAM_CHAT_ID", "") | |
| # Google Apps Script proxy; required on HuggingFace Spaces because direct | |
| # calls to api.telegram.org are blocked there. Empty = call Telegram directly. | |
| PROXY_URL: str = os.getenv("GOOGLE_APPS_SCRIPT_URL", "") | |
| # Secret path segment of the webhook URL: /webhook/{WEBHOOK_SECRET} | |
| WEBHOOK_SECRET: str = os.getenv("WEBHOOK_SECRET", "secret-key") | |
| # Set automatically by HuggingFace Spaces (e.g. "user-space.hf.space") | |
| SPACE_HOST: str = os.getenv("SPACE_HOST", "") | |
| PORT: int = int(os.getenv("PORT", "7860")) | |
| def webhook_url(cls) -> str: | |
| if not cls.SPACE_HOST: | |
| return "" | |
| return f"https://{cls.SPACE_HOST}/webhook/{cls.WEBHOOK_SECRET}" | |
| def validate(cls) -> bool: | |
| """Log missing settings; the platform still runs without them.""" | |
| ok = True | |
| if not cls.BOT_TOKEN: | |
| logger.warning("TELEGRAM_BOT_TOKEN is not set — bot cannot send messages") | |
| ok = False | |
| if not cls.PROXY_URL: | |
| logger.warning( | |
| "GOOGLE_APPS_SCRIPT_URL is not set — falling back to direct " | |
| "Telegram API calls (blocked on HuggingFace Spaces)" | |
| ) | |
| return ok | |