tg-channel-analysis/bot/__main__.py
Sergei Poljanski c30b7e4675 Add multi-user features: i18n, payments, multi-model AI, focus areas
Replace single report type with composable analysis: depth levels
(basic/standard/full) + focus area multi-select (psychology, business,
marketing, content, audience, sentiment). Multi-step inline keyboard
flow guides users through selection.

- i18n: English + Russian, auto-detect from Telegram, /lang override
- AI providers: Anthropic + OpenRouter via AIClient abstraction
- Telegram Stars payments with per-depth pricing and free trial
- SQLite (aiosqlite) for users, analyses, payments tracking
- User middleware for auto-registration and language detection
- Report persistence: save .md locally, offer file download
- New commands: /features, /prices, /lang
- Composable prompt system: depth modifiers + focus area fragments
2026-02-23 01:18:12 +02:00

82 lines
2.2 KiB
Python

import asyncio
import logging
import sys
from aiogram import Bot, Dispatcher
from telethon import TelegramClient
from bot.config import settings
from bot.db.engine import get_db, close_db
from bot.handlers import analyze, features, lang, payment, prices, start
from bot.middleware.user_middleware import UserMiddleware
SESSION_PATH = "/app/data/analyzer_session"
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)
log = logging.getLogger(__name__)
async def login() -> None:
"""Interactive one-time login to create the Telethon session file."""
client = TelegramClient(
SESSION_PATH,
settings.telegram_api_id,
settings.telegram_api_hash,
)
await client.start(phone=settings.telegram_phone)
log.info("Session created at %s.session", SESSION_PATH)
await client.disconnect()
async def main() -> None:
# Init database
await get_db()
log.info("Database initialized")
telethon_client = TelegramClient(
SESSION_PATH,
settings.telegram_api_id,
settings.telegram_api_hash,
)
await telethon_client.connect()
if not await telethon_client.is_user_authorized():
log.error(
"No valid session. Run with --login first: "
"podman run -it --env-file .env -v ./data:/app/data tg-analyzer python -m bot --login"
)
await telethon_client.disconnect()
sys.exit(1)
log.info("Telethon client started")
bot = Bot(token=settings.bot_token)
bot._telethon_client = telethon_client # type: ignore[attr-defined]
dp = Dispatcher()
# Register middleware
dp.update.middleware(UserMiddleware())
# Register routers — payment.pre_checkout must come before analyze
dp.include_router(start.router)
dp.include_router(lang.router)
dp.include_router(features.router)
dp.include_router(prices.router)
dp.include_router(payment.router)
dp.include_router(analyze.router)
log.info("Starting bot polling...")
try:
await dp.start_polling(bot)
finally:
await telethon_client.disconnect()
await close_db()
if __name__ == "__main__":
if "--login" in sys.argv:
asyncio.run(login())
else:
asyncio.run(main())