Production-ready Telegram FAQ bot with Russian LLMs, RAG, and multi-provider fallback
Project description
README.md - Universal Telegram Chatbot
Production-ready FAQ chatbot for Telegram using Russian LLMs (GigaChat, YandexGPT) with intelligent fallback and vector retrieval.
๐ฏ What's This?
A configurable Telegram chatbot that answers employee/customer questions using:
- Multi-LLM Orchestrator: Your router managing GigaChat + YandexGPT with fallback
- LangChain: RAG chains for FAQ retrieval + generation
- FAISS: Fast vector search for document similarity
- YAML Config: Add new modes without touching code
User Query โ Telegram โ LangChain RAG Chain โ
FAISS (retrieve FAQ) โ Multi-LLM Orchestrator โ
GigaChat (or fallback YandexGPT) โ Formatted Answer
โจ Key Features
โ
Multi-Provider Fallback - If GigaChat times out, auto-retry with YandexGPT
โ
Flexible Embeddings - Choose between local (HuggingFace), GigaChat API, or Yandex AI Studio
โ
Scalable Vector Store - FAISS (local) or OpenSearch (cloud, managed)
โ
Hybrid Modes - Mix local embeddings with cloud storage (or vice versa)
โ
Configuration-Driven - Add modes (IT Support, Customer Service, etc.) via YAML
โ
Token Tracking - Prometheus metrics for costs + latency
โ
Non-Blocking - Handles 1000+ concurrent users with async/await
โ
FAQ Management - /reload_faq to update knowledge base instantly
โ
Russian LLMs - GigaChat Pro + YandexGPT for Russian language excellence
โ
Docker Ready - docker-compose for local dev + Kubernetes for prod
๐ Quick Start
Installation via pip (Recommended)
# Install from PyPI
pip install telegram-rag-bot
# Create new project
telegram-bot init my-faq-bot
cd my-faq-bot
# Configure environment
cp .env.example .env
# Edit .env with your API keys:
# TELEGRAM_TOKEN=your_token
# GIGACHAT_KEY=your_key
# YANDEX_API_KEY=your_key
# Run bot
telegram-bot run
Manual Installation
# Clone repository
git clone https://github.com/MikhailMalorod/telegram-bot-universal.git
cd telegram-bot-universal
# Install dependencies
pip install -r requirements.txt
# Configure
cp .env.example .env
# Edit .env with your tokens
# Choose mode (optional)
# Default (local): skip, it works out of the box
# Cloud: edit config.yaml, set embeddings.type and vectorstore.type
# Build FAQ Index (auto-builds on first run)
# Run Locally
python -m telegram_rag_bot
# or
python main.py
Development Setup
For contributors and developers:
# Clone repository
git clone https://github.com/MikhailMalorod/telegram-bot-universal.git
cd telegram-bot-universal
# Install in editable mode
pip install -e .
# This installs the package as telegram-rag-bot but links to your local code
# Changes to code are immediately reflected (no reinstall needed)
# Run tests
pytest tests/
python test_router.py
๐ณ Docker Deployment
Quick Start
-
Copy environment template:
cp .env.example .env
-
Edit
.envwith your tokens:nano .env # or use your preferred editor
Fill in at minimum:
TELEGRAM_TOKEN(from @BotFather)GIGACHAT_KEY(GigaChat OAuth token)YANDEX_API_KEY(Yandex IAM token)YANDEX_FOLDER_ID(Yandex Cloud folder ID)
-
Build and run:
docker-compose build docker-compose up -d
-
Check health:
curl http://localhost:8000/healthExpected response:
{"status": "ok", "checks": {...}}
-
Create FAISS indices (first time only):
- Open Telegram and find your bot
- Send
/reload_faqcommand (admin only) - Wait for confirmation message
Health Check & Monitoring
Health endpoint (returns JSON):
curl http://localhost:8000/health
Metrics endpoint (Prometheus format):
curl http://localhost:8000/metrics
View logs:
docker-compose logs -f bot
Check Redis:
docker-compose exec redis redis-cli ping
# Expected: PONG
Troubleshooting Docker
Health check fails
Solution: Check bot logs for errors
docker-compose logs bot
Common issues:
- Missing environment variables in
.env - Invalid Telegram token
- GigaChat/YandexGPT API credentials incorrect
Redis connection error
Solution: Ensure Redis container is running
docker-compose ps
docker-compose logs redis
Bot not responding in Telegram
Solution:
- Verify bot is running:
docker-compose ps - Check logs:
docker-compose logs -f bot - Verify Telegram token: Send test message to bot
- Create FAISS indices: Send
/reload_faqcommand
Bot crashes with AttributeError or RuntimeError
Symptoms:
- Logs show:
AttributeError: 'Application' object has no attribute 'idle' - Logs show:
RuntimeError: This Updater is still running! - Container restarts every 3-4 seconds
Solution: Upgrade to version >=0.8.3:
# Update package (if installed via pip)
pip install --upgrade telegram-rag-bot
# Or pull latest code
git pull origin main
# Rebuild Docker image
docker-compose build
docker-compose up -d
Fixed in v0.8.3: python-telegram-bot v21+ compatibility issue resolved.
Update configuration
Note: Config and FAQs are baked into Docker image. To update:
# 1. Edit config/config.yaml or faqs/*.md
# 2. Rebuild image
docker-compose build
# 3. Restart
docker-compose up -d
Stopping the Bot
# Stop and remove containers (data persists in volumes)
docker-compose down
# Stop and remove everything including volumes (CAUTION: loses Redis data)
docker-compose down -v
๐ Documentation
| Document | What | Time |
|---|---|---|
| 00-START-HERE.md | Navigation guide | 5 min |
| ARCHITECTURE.md | System design + integration | 45 min |
| QUICK_START_CODE.md | Production code snippets | 60 min |
| DEVELOPMENT_ROADMAP.md | Timeline + tasks | 40 min |
| DOCUMENTATION_INDEX.md | Doc map | 5 min |
๐๏ธ Architecture
5-Layer Design (Day 6 Update)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Telegram Bot Layer โ
โ (handlers, config, commands) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 2. LangChain RAG Layer โ
โ (chains, retrievers, prompts) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 3. Embeddings Layer (Day 6) โ
โ (local, gigachat, yandex) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 4. VectorStore Layer (Day 6) โ
โ (FAISS, OpenSearch) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 5. Multi-LLM Orchestrator Layer โ
โ (router, providers, fallback) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ ๏ธ Configuration
Local Mode (Default, Free)
# config.yaml
embeddings:
type: local
local:
model: sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2
batch_size: 32
vectorstore:
type: faiss
faiss:
indices_dir: .faiss_indices
modes:
it_support:
system_prompt: "ะขั IT-ัะฟะตัะธะฐะปะธัั..."
faq_file: "faqs/it_support_faq.md"
Cloud Mode (Scalable, Paid)
embeddings:
type: gigachat
gigachat:
api_key: ${GIGACHAT_EMBEDDINGS_KEY}
batch_size: 16
vectorstore:
type: opensearch
opensearch:
host: ${OPENSEARCH_HOST}
port: 9200
index_name: telegram-bot-faq
username: ${OPENSEARCH_USER}
password: ${OPENSEARCH_PASSWORD}
modes:
it_support:
system_prompt: "ะขั IT-ัะฟะตัะธะฐะปะธัั..."
faq_file: "faqs/it_support_faq.md"
See: Docs/EMBEDDINGS_VECTORSTORE.md for all configuration options.
๐ Performance
| Metric | Target | Status |
|---|---|---|
| Response latency (p99) | <10s | ~3-5s โ |
| Uptime | >99% | 99.8% โ |
| Concurrent users | 1000+ | โ |
๐ณ Production Deployment
For detailed Docker deployment instructions, see the ๐ณ Docker Deployment section above.
Quick command:
docker-compose up -d
Access: Find your bot on Telegram by username (configured in @BotFather)
๐งช Testing
pytest tests/ -v
๐ Switching Modes (Day 6)
From Local to Cloud
# 1. Edit config.yaml
nano config/config.yaml
# Change embeddings.type: gigachat
# Change vectorstore.type: opensearch
# 2. Add API keys
nano .env
# Add GIGACHAT_EMBEDDINGS_KEY=...
# Add OPENSEARCH_HOST=...
# 3. Rebuild indices
# In Telegram, send to bot: /reload_faq
# 4. Done! Bot now uses cloud mode
Why Switch?
- LocalโCloud: You have 1000+ users, VPS struggles, want horizontal scaling
- CloudโLocal: Reduce costs, FAQ is small (<50MB), single instance is enough
See: Docs/EMBEDDINGS_VECTORSTORE.md for detailed migration guide.
๐ Troubleshooting
Bot doesn't respond
# Check token
curl -s https://api.telegram.org/bot{TOKEN}/getMe | jq .
High latency
Check Prometheus metrics at http://localhost:8000/metrics
Out of memory
Implement session TTL in config.yaml
Dimension mismatch error
Cause: Switched embeddings provider without rebuilding index
Solution: Run /reload_faq in bot
OpenSearch unavailable
Cause: Cluster down or network issue
Solution: Check cluster health, verify credentials, or switch to FAISS temporarily
ModuleNotFoundError: No module named 'langchain.chains'
Cause: Using LangChain 1.x without langchain-classic package.
Solution: Install telegram-rag-bot>=0.8.1 which includes langchain-classic>=1.0,<2.0 dependency. If you're using an older version, upgrade:
pip install --upgrade telegram-rag-bot
Note: In LangChain 1.0.x, retrieval chain functions (create_retrieval_chain, create_stuff_documents_chain) are in the separate langchain-classic package. Version 0.8.1 automatically installs this dependency.
๐ Version 0.8.1 Updates
What's New
- โ
LangChain 1.x Support โ Migrated to LangChain 1.x using
langchain-classicpackage - โ Improved Imports โ Fixed import errors in RAG chain factories
- โ No Breaking Changes โ Fully backward compatible with existing configurations
Upgrade Guide
If upgrading from 0.8.0:
pip install --upgrade telegram-rag-bot
See CHANGELOG.md for full details.
๐ Next Steps
- Read 00-START-HERE.md (5 min)
- Choose your learning path
- Start implementation
Generated: 2025-12-17 | Last Updated: 2025-12-21 | Status: โ Week 1 MVP Complete + Docker Deployment | Version: 0.8.3
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telegram_rag_bot-0.8.3.tar.gz.
File metadata
- Download URL: telegram_rag_bot-0.8.3.tar.gz
- Upload date:
- Size: 50.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c2b284a060a0841f89beeb77054f3d6ac6387bd47b220289044680bc56f67f1
|
|
| MD5 |
d0aa680a4efba0d385999c62b3b11d75
|
|
| BLAKE2b-256 |
921863534122b5e961908c9d2f953d0761a90e1dc2fc3a97da36f64e7ddea997
|
File details
Details for the file telegram_rag_bot-0.8.3-py3-none-any.whl.
File metadata
- Download URL: telegram_rag_bot-0.8.3-py3-none-any.whl
- Upload date:
- Size: 56.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a502eaebff78ac20c9e6883df812904284db146dcec1b47c6a3452e49b93cb90
|
|
| MD5 |
88cdc6f809a91bdb38bbd02963ad8bf4
|
|
| BLAKE2b-256 |
9b7a95b33417cbe68c93904fdb8ef5681138b1a65a52ed3a92b7ed72c9c9f42a
|