3.0 KiB
3.0 KiB
UMB Bot - Agent Quickstart
Environment & Setup
source .venv/bin/activatebefore running or testing anything.envis gitignored; API keys and tokens live there (verified read-only)- Database: SQLite at
bot/data/umb.db; initialized automatically on boot viainit_db() - Proxy required: bot is designed for regions with Telegram blocks (Russia/CIS). All outbound traffic (Telegram API, OpenRouter model fetch, AI requests, Whisper model download) goes through SOCKS5 proxy configured in
.envviaPROXY_ENABLED=trueandPROXY_URL.
Exact Command to Run
python main.py
No separate test/lint/formatter/config commands defined in repo.
Architecture Snapshot
- Entry:
main.py→asyncio.run(main()) - Boot sequence:
init_db()→ proxy setup (if enabled) →dp.start_polling(bot) - Routers (included in order in
main.py):moderation.py— sticker/GIF flood detection (25 in 60s → 5min ban)layout.py—/start,/res(en→ru layout converter inlayout_converter.py)weather.py—/weather <city>via OpenWeatherMapai.py—/ai,/aino,/aiyes(AI assistant "Astra")voice.py— auto-transcribes all voice messages via local faster-whisper (no proxy needed)
Voice Recognition
- Local: faster-whisper model
base(~2.5 GB) auto-downloaded on first voice message tomodels/faster-whisper/(portable, next to the bot) - Model download: uses
ALL_PROXYenv var + PySocks to route through SOCKS5 proxy - Voice files: temporarily stored in
bot/data/voice/, deleted after transcription - Flow: download OGG from Telegram → ffmpeg to WAV → transcribe locally → post raw text to chat
- Normalization: capitalize first letter, trailing period, collapse whitespace
- No proxy used for actual transcription (fully local on CPU)
AI Assistant "Astra" Gotchas
- Free models: fetched dynamically from OpenRouter API (
:freesuffix), cached 1 hour. Fallback to hardcoded list if API unavailable. - Paid fallback:
routerai.ruif all free fail (adds⚡ Обработано через платный APIsuffix) - Context: Last 10 messages per user/chat stored in SQLite (
user_contexttable) - System prompt: brief, helpful, reply with "я потеряла мысль"/"смотри, какая птичка!" when context lost
- Block system:
/aino @userblocks 24h (creator only);/aiyes @userunblocks - Rate-limit behavior: automatically rotates models on 429 errors
DB Tables (source of truth)
user_context— messages for AI context (user_id, chat_id, text, role, timestamp)ai_blocked_users— AI bans (user_id, chat_id, blocked_by, blocked_at, expires_at)sticker_bans— sticker flood bans (user_id, chat_id, count, start_time, ban_until, ban_trigger)
Run on change
- Edit source
- Restart bot (
Ctrl+Cthenpython main.py) - DB schema updates via
await init_db()→Base.metadata.create_all()(no migrations)