Overview
The Telegram integration provides an autonomous, context-aware bot that can participate in groups and direct messages. It features intelligent action generation, message history analysis, sticker support, and photo interpretation.Features
- Autonomous Operation: Periodically checks for unread messages and responds proactively
- Multi-Chat Support: Handle multiple groups and DMs simultaneously with isolated contexts
- Sticker Understanding: Interpret and send context-appropriate stickers
- Photo Analysis: Vision model integration for image interpretation
- Action System: LLM-driven action selection (send, read, wait, sleep)
- Message History: Embedding-based semantic search with pgvector
- Interruption Handling: Adaptive processing with message prioritization
Prerequisites
- Node.js 18 or higher
- PostgreSQL with pgvector extension
- Telegram bot token from BotFather
- LLM API access (OpenAI, OpenRouter, etc.)
- Embedding model (Ollama with nomic-embed-text recommended)
- Docker and Docker Compose (for database)
Setup
1. Create Telegram Bot
- Open Telegram and search for @BotFather
- Send
/newbotcommand - Follow prompts to choose a name and username
- Copy the bot token (format:
1234567890:ABCdefGHIjklMNOpqrsTUVwxyz) - Configure bot settings:
/setprivacy- Disable (to receive all group messages)/setjoingroups- Enable
2. Start Ollama (for Embeddings)
Install and start Ollama:You can use other embedding providers, but Ollama with nomic-embed-text is recommended for cost-efficiency.
3. Configure Environment
Navigate to the Telegram bot service:.env.local:
4. Start Database
From the service directory:- PostgreSQL with pgvector
- Grafana (observability)
- Prometheus (metrics)
- Tempo (tracing)
- OpenTelemetry Collector
5. Install Dependencies
From project root:6. Initialize Database
Run migrations:7. Start the Bot
Usage
Adding to Groups
- Go to your Telegram group
- Click Group Info → Add Members
- Search for your bot username
- Add the bot
- Grant admin rights (recommended for full functionality)
Direct Messages
Simply send a message to your bot - it will respond automatically.Adding Sticker Packs
Only admins can add sticker packs:- Send a sticker from the pack you want to add
- Reply to that sticker with
/add_sticker_pack - The bot will process and remember all stickers in the pack
Code Examples
Bot Initialization
Fromservices/telegram-bot/src/bots/telegram/index.ts:462:
Message Handling
Fromservices/telegram-bot/src/bots/telegram/index.ts:525:
Action System
AIRI uses an LLM-driven action system. Available actions: read_unread_messages:Photo Interpretation
Fromservices/telegram-bot/src/bots/telegram/index.ts:511:
Periodic Tick System
Fromservices/telegram-bot/src/bots/telegram/index.ts:339:
Architecture
Message Queue
Messages are queued and processed sequentially per chat:- Message arrives → Added to queue
- If sticker/photo → Interpret first (status:
pending) - Mark as
ready→ Process with LLM - Generate action → Dispatch action
- Continue until action returns
breakorcontinue
Context Management
Each chat maintains isolated context:Embedding Storage
All messages are stored with embeddings for semantic search:Configuration
Observability
The Telegram bot includes full OpenTelemetry instrumentation:- Metrics: http://localhost:9090 (Prometheus)
- Traces: http://localhost:3000 (Grafana)
- Dashboards: Pre-configured in Grafana
Admin Commands
SetADMIN_USER_IDS in .env.local to enable admin-only commands:
/add_sticker_pack- Import a sticker set
Message Limits
- Context window: 20 messages (auto-trimmed)
- Action history: 50 actions (auto-trimmed)
- Unread queue: 100 messages per chat
Troubleshooting
Bot not responding
- Check logs for connection errors
- Verify
TELEGRAM_BOT_TOKENis correct - Ensure bot has message access in groups (privacy mode disabled)
Database connection failed
- Check PostgreSQL is running:
docker compose ps - Verify
DATABASE_URLconnection string - Run migrations:
pnpm run -F @proj-airi/telegram-bot db:push
Embedding errors
- Check Ollama is running:
ollama list - Verify model is pulled:
ollama pull nomic-embed-text - Test endpoint:
curl http://localhost:11434/api/embeddings
Vision not working
- Verify
LLM_VISION_MODELsupports image input - Check API key has vision model access
- Monitor rate limits
Next Steps
Discord Integration
Add AIRI to Discord
Minecraft Integration
Run AIRI in Minecraft
