An AI-powered Telegram bot with streaming responses, tool support (web search and webpage extraction), conversation persistence, invite-based access control, and multi-language support.
- Streaming Responses: Real-time AI responses using Telegram's draft messages (Bot API 9.3)
- Tool Support: Web search and webpage extraction via Tavily
- Thinking Mode: Optional display of AI reasoning traces
- Conversation Persistence: SQLite or PostgreSQL database storage
- Multi-language UI: English, Russian, and Ukrainian with auto-detection
- Invite System: Access control with shareable invite codes
- Multimodal Input: Support for images and PDF documents
- Timezone Support: Personalized time display based on user timezone
| Command | Description |
|---|---|
/start |
Start the bot or use an invite code |
/help |
Show help message |
/lang |
Change interface language |
/thinking |
Toggle AI thinking display |
/redo |
Regenerate last response |
/edit <text> |
Edit last message and regenerate |
/code <code> |
Manually enter invite code |
/timezone |
Set timezone by entering current time |
| Command | Description |
|---|---|
/invite [code] [uses] |
Create invite code |
/invites |
List all active invite codes |
/deleteinvite <code> |
Delete an invite code |
/approve <user_id> |
Pre-approve user by Telegram ID |
Admins can also share invite codes via inline mode by typing @botname in any chat.
# Clone the repository
git clone https://github.com/yourname/ai-tg-bot.git
cd ai-tg-bot
# Create environment file
cp .env.example .env
# Edit .env with your API keys
# Install dependencies (requires uv)
uv sync
# Run the bot
python main.py# Create environment file
cp .env.example .env
# Edit .env with your API keys
# Run with Docker Compose (includes PostgreSQL)
docker compose up -d| Variable | Description |
|---|---|
TELEGRAM_BOT_TOKEN |
Bot token from @BotFather |
OPENROUTER_API_KEY |
API key from OpenRouter |
TAVILY_API_KEY |
API key from Tavily |
ADMIN_IDS |
Comma-separated Telegram user IDs (e.g., 123456789,987654321) |
| Variable | Default | Description |
|---|---|---|
OPENROUTER_MODEL |
moonshotai/kimi-k2.5 |
Model ID from OpenRouter |
DATABASE_URL |
sqlite+aiosqlite:///bot.db |
Database connection URL |
SQLite (default, local development):
DATABASE_URL=sqlite+aiosqlite:///bot.db
PostgreSQL (recommended for production):
DATABASE_URL=postgresql+asyncpg://user:password@localhost:5432/dbname
ai-tg-bot/
├── main.py # Entry point
├── bot/
│ ├── config.py # Configuration
│ ├── ai/ # OpenRouter + Tavily client
│ ├── database/ # SQLAlchemy models and repository
│ ├── i18n/ # Translations
│ ├── telegram/ # Bot, handlers, and filters
│ └── utils/ # Formatting and token utilities
├── docs/
│ └── CODEBASE_MAP.md # Detailed architecture documentation
└── SYS_PROMPT.md # AI system prompt template
See docs/CODEBASE_MAP.md for detailed architecture documentation.
The bot uses an invite-based access control system:
- Admins (listed in
ADMIN_IDS) have full access automatically - New users need an invite code to access the bot
- Admins can create invite codes via
/invitecommand or inline mode - Admins can pre-approve users via
/approve <user_id>
Via command:
/invite mycode 5 # Creates "mycode" with 5 uses
/invite # Creates random code with 1 use
Via inline mode: Type @yourbotname in any chat to share invite codes with a button.
Via deep link: Share https://t.me/yourbotname?start=CODE
- Python 3.12+
- uv package manager
aiogram- Telegram Bot API frameworkopenai- OpenAI SDK (for OpenRouter)sqlalchemy+aiosqlite/asyncpg- Async database ORMtavily-python- Web search and extractionpydantic-settings- Configuration managementtelegramify-markdown- Markdown conversion
- Add translation keys to
bot/i18n/translations.py - Add command key to
USER_COMMAND_KEYSorADMIN_COMMAND_KEYSinmain.py - Create handler in
bot/telegram/handlers/
- Add tool definition to
TOOLSinbot/ai/openrouter.py - Add execution logic to
_execute_tool() - Add status translation keys to
bot/i18n/translations.py
The project includes multi-platform Docker images (amd64/arm64) published to GitHub Container Registry.
# Pull and run
docker pull ghcr.io/yourname/ai-tg-bot:latest
docker run --env-file .env ghcr.io/yourname/ai-tg-bot:latest
# Or use docker-compose with PostgreSQL
docker compose up -dMIT