nvd commited on
Commit
a40ca0e
·
verified ·
1 Parent(s): 4ba566e

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +40 -8
bot.py CHANGED
@@ -17,18 +17,50 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
17
  logger = logging.getLogger(__name__)
18
 
19
  # ---------- Import tokens and allowed users ----------
20
- try:
21
- from config import BALE_BOT_TOKEN as BALE_TOKEN, GITHUB_TOKEN, ALLOWED_USERS
22
- logger.info("Tokens and allowed users loaded from config.py")
23
- except ImportError:
24
- BALE_TOKEN = os.getenv("BALE_BOT_TOKEN")
25
- GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
26
- ALLOWED_USERS = []
27
- logger.info("Tokens loaded from environment variables; no user restriction")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  if not BALE_TOKEN:
30
  raise ValueError("❌ BALE_BOT_TOKEN not set!")
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  GITHUB_API = "https://api.github.com"
33
  BALE_API = f"https://tapi.bale.ai/bot{BALE_TOKEN}/"
34
  DB_FILE = "subscriptions.db"
 
17
  logger = logging.getLogger(__name__)
18
 
19
  # ---------- Import tokens and allowed users ----------
20
+ import os
21
+ import logging
22
+
23
+ logger = logging.getLogger(__name__)
 
 
 
 
24
 
25
+ # 1. Try environment variables (used on Hugging Face / production)
26
+ BALE_TOKEN = os.getenv("BALE_BOT_TOKEN")
27
+ GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") # can be None
28
+ allowed_raw = os.getenv("ALLOWED_USERS") # comma-separated chat IDs, e.g. "447422307,123456789"
29
+
30
+ if allowed_raw:
31
+ ALLOWED_USERS = [int(uid.strip()) for uid in allowed_raw.split(",") if uid.strip()]
32
+ else:
33
+ ALLOWED_USERS = [] # empty = no restrictions (or fallback to config.py)
34
+
35
+ # 2. If BALE_TOKEN is missing, fall back to config.py (local development)
36
+ if not BALE_TOKEN:
37
+ try:
38
+ from config import BALE_BOT_TOKEN as BALE_TOKEN, GITHUB_TOKEN, ALLOWED_USERS
39
+ logger.info("Tokens and allowed users loaded from config.py")
40
+ except ImportError:
41
+ logger.error("❌ BALE_BOT_TOKEN not set in environment or config.py!")
42
+ raise ValueError("BALE_BOT_TOKEN missing")
43
+
44
+ # 3. Final safety check
45
  if not BALE_TOKEN:
46
  raise ValueError("❌ BALE_BOT_TOKEN not set!")
47
 
48
+ # 4. For GitHub token and allowed users, if still empty after the env check, try config.py as fallback
49
+ if not GITHUB_TOKEN:
50
+ try:
51
+ from config import GITHUB_TOKEN
52
+ except ImportError:
53
+ pass
54
+
55
+ if not ALLOWED_USERS:
56
+ try:
57
+ from config import ALLOWED_USERS
58
+ except ImportError:
59
+ pass
60
+
61
+ # 5. Log the final state
62
+ logger.info(f"Allowed users: {ALLOWED_USERS if ALLOWED_USERS else 'All users permitted'}")
63
+
64
  GITHUB_API = "https://api.github.com"
65
  BALE_API = f"https://tapi.bale.ai/bot{BALE_TOKEN}/"
66
  DB_FILE = "subscriptions.db"