Files
Otkritiebot/session_bot/config.py
Verum a13f4e378c
Some checks failed
CI / Lint (ruff + mypy) (push) Failing after 33s
CI / Run tests (push) Has been skipped
CI / Docker build test (push) Successful in 12s
Security / Dependency security scan (push) Failing after 48s
ир
2026-04-03 01:03:52 +07:00

64 lines
1.9 KiB
Python

from __future__ import annotations
import json
import os
from dataclasses import dataclass
from pathlib import Path
from dotenv import load_dotenv
@dataclass(slots=True)
class BotSettings:
bot_token: str
channel_id: int
channel_message_id: int
config_path: Path
state_path: Path
admin_ids: set[int]
hidden_link_url: str
hidden_link_char: str
telethon_api_id: int | None
telethon_api_hash: str
telethon_session_string: str
telethon_channel: str
@property
def telethon_enabled(self) -> bool:
return bool(self.telethon_api_id and self.telethon_api_hash and self.telethon_session_string)
def _parse_admin_ids(value: str) -> set[int]:
ids = set()
for chunk in value.split(","):
chunk = chunk.strip()
if chunk:
ids.add(int(chunk))
return ids
def load_settings() -> BotSettings:
load_dotenv()
telethon_api_id = os.environ.get("TELETHON_API_ID", "").strip()
return BotSettings(
bot_token=os.environ["BOT_TOKEN"],
channel_id=int(os.environ["CHANNEL_ID"]),
channel_message_id=int(os.environ["CHANNEL_MESSAGE_ID"]),
config_path=Path(os.environ.get("CONFIG_PATH", "config/actors.json")),
state_path=Path(os.environ.get("STATE_PATH", "data/state.json")),
admin_ids=_parse_admin_ids(os.environ.get("ADMIN_IDS", "")),
hidden_link_url=os.environ.get("HIDDEN_LINK_URL", ""),
hidden_link_char=os.environ.get("HIDDEN_LINK_CHAR", "​"),
telethon_api_id=int(telethon_api_id) if telethon_api_id else None,
telethon_api_hash=os.environ.get("TELETHON_API_HASH", "").strip(),
telethon_session_string=os.environ.get("TELETHON_SESSION_STRING", "").strip(),
telethon_channel=os.environ.get("TELETHON_CHANNEL", "").strip(),
)
def load_actor_config(path: Path) -> dict:
with path.open("r", encoding="utf-8") as file:
return json.load(file)