feat(02-01): create bot configuration module

- BotConfig dataclass with from_env() classmethod
- Loads BOT_TOKEN (required), ALLOWED_USERS, DATABASE_URL, LOG_LEVEL
- Raises ValueError if BOT_TOKEN is missing
This commit is contained in:
Mikkel Georgsen 2026-01-16 15:35:44 +00:00
parent 4d6768e55c
commit 4381e12609

64
src/moai/bot/config.py Normal file
View file

@ -0,0 +1,64 @@
"""Bot configuration loaded from environment variables.
Provides BotConfig dataclass with configuration loaded from environment.
Required variables will raise ValueError if missing.
"""
import os
from dataclasses import dataclass
@dataclass
class BotConfig:
"""Configuration for the MoAI Telegram bot.
Attributes:
bot_token: Telegram Bot API token (required).
allowed_users: Set of allowed Telegram user IDs. Empty means all users allowed.
database_url: SQLAlchemy database URL.
log_level: Logging level string (DEBUG, INFO, WARNING, ERROR).
"""
bot_token: str
allowed_users: set[int]
database_url: str
log_level: str
@classmethod
def from_env(cls) -> "BotConfig":
"""Load configuration from environment variables.
Environment variables:
BOT_TOKEN (required): Telegram bot token from @BotFather.
ALLOWED_USERS (optional): Comma-separated Telegram user IDs.
DATABASE_URL (optional): Database URL, defaults to SQLite.
LOG_LEVEL (optional): Logging level, defaults to INFO.
Returns:
BotConfig instance populated from environment.
Raises:
ValueError: If BOT_TOKEN is not set.
"""
bot_token = os.environ.get("BOT_TOKEN")
if not bot_token:
raise ValueError("BOT_TOKEN environment variable is required")
# Parse allowed users from comma-separated string
allowed_users_str = os.environ.get("ALLOWED_USERS", "")
allowed_users: set[int] = set()
if allowed_users_str.strip():
for user_id in allowed_users_str.split(","):
user_id = user_id.strip()
if user_id:
allowed_users.add(int(user_id))
database_url = os.environ.get("DATABASE_URL", "sqlite+aiosqlite:///./moai.db")
log_level = os.environ.get("LOG_LEVEL", "INFO")
return cls(
bot_token=bot_token,
allowed_users=allowed_users,
database_url=database_url,
log_level=log_level,
)