39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
|
|
def load_env_file(path: Path) -> None:
|
|
if not path.exists():
|
|
return
|
|
for raw_line in path.read_text(encoding="utf-8").splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, value = line.split("=", 1)
|
|
os.environ.setdefault(key.strip(), value.strip())
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class BotConfig:
|
|
token: str
|
|
server_url: str
|
|
poll_timeout: int
|
|
proxy_url: str | None
|
|
|
|
@classmethod
|
|
def load(cls) -> "BotConfig":
|
|
base_dir = Path(__file__).resolve().parent
|
|
load_env_file(base_dir / ".env")
|
|
token = os.environ.get("NEW_QWEN_BOT_TOKEN", "").strip()
|
|
if not token:
|
|
raise RuntimeError("NEW_QWEN_BOT_TOKEN is required")
|
|
return cls(
|
|
token=token,
|
|
server_url=os.environ.get("NEW_QWEN_SERVER_URL", "http://127.0.0.1:8080").rstrip("/"),
|
|
poll_timeout=int(os.environ.get("NEW_QWEN_POLL_TIMEOUT", "30")),
|
|
proxy_url=os.environ.get("NEW_QWEN_TELEGRAM_PROXY", "").strip() or None,
|
|
)
|