67 lines
1.8 KiB
Python
67 lines
1.8 KiB
Python
import asyncio
|
|
import logging
|
|
import sys
|
|
|
|
from aiogram import Bot, Dispatcher
|
|
from telethon import TelegramClient
|
|
|
|
from bot.config import settings
|
|
from bot.handlers import analyze, start
|
|
|
|
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:
|
|
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)
|
|
# Attach telethon client to bot instance for handler access
|
|
bot._telethon_client = telethon_client # type: ignore[attr-defined]
|
|
|
|
dp = Dispatcher()
|
|
dp.include_router(start.router)
|
|
dp.include_router(analyze.router)
|
|
|
|
log.info("Starting bot polling...")
|
|
try:
|
|
await dp.start_polling(bot)
|
|
finally:
|
|
await telethon_client.disconnect()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if "--login" in sys.argv:
|
|
asyncio.run(login())
|
|
else:
|
|
asyncio.run(main())
|