sno-quiz/bot/config.py
2025-09-17 22:22:14 +03:00

82 lines
2.6 KiB
Python

"""
Bot configuration
"""
import os
from typing import Optional
from dotenv import load_dotenv
load_dotenv()
class BotConfig:
"""Bot configuration class"""
def __init__(self):
self.token: str = os.getenv("BOT_TOKEN", "")
self.backend_api_url: str = os.getenv("BACKEND_API_URL", "http://localhost:8080")
self.frontend_url: str = os.getenv("FRONTEND_URL", "http://localhost:5173")
self.webhook_secret: Optional[str] = os.getenv("WEBHOOK_SECRET")
self.webhook_url: Optional[str] = os.getenv("WEBHOOK_URL")
self.bot_username: str = os.getenv("BOT_USERNAME", "QuizBot")
self.admin_user_ids: list[int] = self._parse_admin_ids()
self.operator_user_ids: list[int] = self._parse_operator_ids()
def _parse_admin_ids(self) -> list[int]:
"""Parse admin user IDs from environment variable"""
admin_ids = os.getenv("ADMIN_USER_IDS", "")
if not admin_ids:
return []
try:
return [int(id_str.strip()) for id_str in admin_ids.split(",")]
except ValueError:
return []
def _parse_operator_ids(self) -> list[int]:
"""Parse operator user IDs from environment variable"""
operator_ids = os.getenv("OPERATOR_USER_IDS", "")
if not operator_ids:
return []
try:
return [int(id_str.strip()) for id_str in operator_ids.split(",")]
except ValueError:
return []
def validate(self) -> bool:
"""Validate configuration"""
if not self.token:
raise ValueError("BOT_TOKEN is required")
if not self.backend_api_url:
raise ValueError("BACKEND_API_URL is required")
if not self.frontend_url:
raise ValueError("FRONTEND_URL is required")
return True
def is_admin(self, user_id: int) -> bool:
"""Check if user is admin"""
return user_id in self.admin_user_ids
def is_operator(self, user_id: int) -> bool:
"""Check if user is operator"""
return user_id in self.operator_user_ids or self.is_admin(user_id)
def has_admin_privileges(self, user_id: int) -> bool:
"""Check if user has admin or operator privileges"""
return self.is_admin(user_id) or self.is_operator(user_id)
class AppConfig:
"""Application configuration"""
DEBUG = os.getenv("DEBUG", "false").lower() == "true"
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
DATABASE_URL = os.getenv("DATABASE_URL", "")
REDIS_URL = os.getenv("REDIS_URL", "")
# Global configuration instance
config = BotConfig()
app_config = AppConfig()