.
This commit is contained in:
56
bot/config.py
Normal file
56
bot/config.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from dataclasses import dataclass
|
||||
import os
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Config:
|
||||
bot_token: str
|
||||
admin_telegram_ids: set[int]
|
||||
admin_departments: dict[int, set[str]]
|
||||
database_path: str
|
||||
sla_minutes: int
|
||||
|
||||
|
||||
def parse_admin_departments(raw: str) -> dict[int, set[str]]:
|
||||
result: dict[int, set[str]] = {}
|
||||
for rule in raw.split(";"):
|
||||
if ":" not in rule:
|
||||
continue
|
||||
admin_id_raw, departments_raw = rule.split(":", 1)
|
||||
admin_id_raw = admin_id_raw.strip()
|
||||
if not admin_id_raw.isdigit():
|
||||
continue
|
||||
|
||||
departments = {
|
||||
department.strip()
|
||||
for department in departments_raw.split(",")
|
||||
if department.strip()
|
||||
}
|
||||
if departments:
|
||||
result[int(admin_id_raw)] = departments
|
||||
return result
|
||||
|
||||
|
||||
def load_config() -> Config:
|
||||
token = os.getenv("BOT_TOKEN", "").strip()
|
||||
if not token:
|
||||
raise RuntimeError("BOT_TOKEN is not set")
|
||||
|
||||
admin_ids_raw = os.getenv("ADMIN_TELEGRAM_IDS", "")
|
||||
admin_ids = {
|
||||
int(item.strip())
|
||||
for item in admin_ids_raw.split(",")
|
||||
if item.strip().isdigit()
|
||||
}
|
||||
admin_departments = parse_admin_departments(os.getenv("ADMIN_DEPARTMENTS", ""))
|
||||
# Если для админа не задан явный список разделов — он видит ВСЕ разделы.
|
||||
for admin_id in admin_ids:
|
||||
admin_departments.setdefault(admin_id, set())
|
||||
|
||||
return Config(
|
||||
bot_token=token,
|
||||
admin_telegram_ids=admin_ids,
|
||||
admin_departments=admin_departments,
|
||||
database_path=os.getenv("DATABASE_PATH", "/app/data/tickets.db"),
|
||||
sla_minutes=int(os.getenv("SLA_MINUTES", "60")),
|
||||
)
|
||||
Reference in New Issue
Block a user