Spaces:
Runtime error
Runtime error
Update bot/config.py
Browse files- bot/config.py +45 -14
bot/config.py
CHANGED
|
@@ -1,46 +1,77 @@
|
|
| 1 |
# PATH: bot/config.py
|
| 2 |
import os
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
|
| 5 |
-
load_dotenv()
|
| 6 |
|
| 7 |
def _get_int(name: str, default: int) -> int:
|
| 8 |
try:
|
| 9 |
return int(os.environ.get(name, str(default)))
|
| 10 |
-
except:
|
| 11 |
return default
|
| 12 |
|
| 13 |
def _get_str(name: str, default: str = "") -> str:
|
| 14 |
return str(os.environ.get(name, default)).strip()
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
class Telegram:
|
| 17 |
API_ID: int = _get_int("API_ID", 0)
|
| 18 |
API_HASH: str = _get_str("API_HASH", "")
|
| 19 |
BOT_TOKEN: str = _get_str("BOT_TOKEN", "")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
OWNER_ID: int = _get_int("OWNER_ID", 0)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
class Workers:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
WORKER2_URL: str = _get_str("WORKER2_URL", "")
|
| 30 |
BOT_BACKEND_KEY: str = _get_str("BOT_BACKEND_KEY", "")
|
| 31 |
HF_API_KEY: str = _get_str("HF_API_KEY", "")
|
| 32 |
|
| 33 |
class Server:
|
| 34 |
BIND_ADDRESS: str = _get_str("BIND_ADDRESS", "0.0.0.0")
|
| 35 |
PORT: int = _get_int("PORT", 7860)
|
|
|
|
| 36 |
|
| 37 |
LOGGER_CONFIG_JSON = {
|
| 38 |
"version": 1,
|
| 39 |
"disable_existing_loggers": False,
|
| 40 |
-
"formatters": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"handlers": {"stream": {"class": "logging.StreamHandler", "formatter": "default"}},
|
| 42 |
"loggers": {
|
|
|
|
|
|
|
| 43 |
"bot": {"level": "INFO", "handlers": ["stream"]},
|
| 44 |
-
"hydrogram": {"level": "
|
| 45 |
-
|
|
|
|
| 46 |
}
|
|
|
|
| 1 |
# PATH: bot/config.py
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def _get_int(name: str, default: int) -> int:
|
| 5 |
try:
|
| 6 |
return int(os.environ.get(name, str(default)))
|
| 7 |
+
except Exception:
|
| 8 |
return default
|
| 9 |
|
| 10 |
def _get_str(name: str, default: str = "") -> str:
|
| 11 |
return str(os.environ.get(name, default)).strip()
|
| 12 |
|
| 13 |
+
def _get_int_list(name: str) -> list[int]:
|
| 14 |
+
raw = _get_str(name, "")
|
| 15 |
+
if not raw:
|
| 16 |
+
return []
|
| 17 |
+
parts = [p.strip() for p in raw.replace(";", ",").replace("|", ",").split(",")]
|
| 18 |
+
out: list[int] = []
|
| 19 |
+
for p in parts:
|
| 20 |
+
if not p:
|
| 21 |
+
continue
|
| 22 |
+
try:
|
| 23 |
+
out.append(int(p))
|
| 24 |
+
except Exception:
|
| 25 |
+
continue
|
| 26 |
+
return out
|
| 27 |
+
|
| 28 |
+
def _as_https_base(v: str) -> str:
|
| 29 |
+
v = (v or "").strip().rstrip("/")
|
| 30 |
+
if not v:
|
| 31 |
+
return ""
|
| 32 |
+
if v.startswith("http://") or v.startswith("https://"):
|
| 33 |
+
return v.rstrip("/")
|
| 34 |
+
return "https://" + v
|
| 35 |
+
|
| 36 |
class Telegram:
|
| 37 |
API_ID: int = _get_int("API_ID", 0)
|
| 38 |
API_HASH: str = _get_str("API_HASH", "")
|
| 39 |
BOT_TOKEN: str = _get_str("BOT_TOKEN", "")
|
| 40 |
+
|
| 41 |
+
USER_SESSION_STRING: str = _get_str("USER_SESSION_STRING", "") or _get_str("SESSION_STRING", "")
|
| 42 |
+
SESSION_STRING: str = _get_str("SESSION_STRING", "") # legacy
|
| 43 |
+
|
| 44 |
OWNER_ID: int = _get_int("OWNER_ID", 0)
|
| 45 |
+
BOT_USERNAME: str = _get_str("TELEGRAM_BOT_USERNAME", "BotFather")
|
| 46 |
+
|
| 47 |
+
ADMIN_IDS: list[int] = _get_int_list("ADMIN_IDS")
|
| 48 |
|
| 49 |
class Workers:
|
| 50 |
+
WORKER1_URL: str = _as_https_base(_get_str("WORKER1_URL", ""))
|
| 51 |
+
WORKER2_URL: str = _as_https_base(_get_str("WORKER2_URL", ""))
|
|
|
|
| 52 |
BOT_BACKEND_KEY: str = _get_str("BOT_BACKEND_KEY", "")
|
| 53 |
HF_API_KEY: str = _get_str("HF_API_KEY", "")
|
| 54 |
|
| 55 |
class Server:
|
| 56 |
BIND_ADDRESS: str = _get_str("BIND_ADDRESS", "0.0.0.0")
|
| 57 |
PORT: int = _get_int("PORT", 7860)
|
| 58 |
+
BASE_URL: str = _get_str("BASE_URL", "http://127.0.0.1:7860")
|
| 59 |
|
| 60 |
LOGGER_CONFIG_JSON = {
|
| 61 |
"version": 1,
|
| 62 |
"disable_existing_loggers": False,
|
| 63 |
+
"formatters": {
|
| 64 |
+
"default": {
|
| 65 |
+
"format": "[%(asctime)s][%(name)s][%(levelname)s] -> %(message)s",
|
| 66 |
+
"datefmt": "%d/%m/%Y %H:%M:%S",
|
| 67 |
+
}
|
| 68 |
+
},
|
| 69 |
"handlers": {"stream": {"class": "logging.StreamHandler", "formatter": "default"}},
|
| 70 |
"loggers": {
|
| 71 |
+
"uvicorn": {"level": "INFO", "handlers": ["stream"]},
|
| 72 |
+
"uvicorn.error": {"level": "INFO", "handlers": ["stream"]},
|
| 73 |
"bot": {"level": "INFO", "handlers": ["stream"]},
|
| 74 |
+
"hydrogram": {"level": "INFO", "handlers": ["stream"]},
|
| 75 |
+
"httpx": {"level": "WARNING", "handlers": ["stream"]},
|
| 76 |
+
},
|
| 77 |
}
|