File size: 1,686 Bytes
820c4b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""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"))

    @classmethod
    def webhook_url(cls) -> str:
        if not cls.SPACE_HOST:
            return ""
        return f"https://{cls.SPACE_HOST}/webhook/{cls.WEBHOOK_SECRET}"

    @classmethod
    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