This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
- Start:
npm start(runsnode index.js) - Development:
node index.js(direct execution) - PM2 Production:
pm2 start ecosystem.config.js(cluster mode with health checker) - Docker:
docker compose --profile dev up -d(requires quote-api setup)
- Lint:
npx eslint .(ESLint with Standard config) - Test: No test suite configured (displays error message)
- Health Check: Bot runs health endpoint on port configured in environment
- Monitoring: Uses PM2 ecosystem config with auto-restart and memory limits
The bot uses Node.js cluster mode with a master-worker pattern:
- Master Process (
master.js): Handles load balancing, queue management, TDLib operations, and health monitoring - Worker Processes (
worker.js): Process individual Telegram updates - Queue Manager (
queueManager.js): Manages update queues with priority handling and backpressure
- Telegraf: Main bot framework with middleware-based architecture
- TDLib Integration: Uses
helpers/tdlib/for advanced Telegram features - Internationalization: Multi-language support via
locales/directory
- Quote API: External service for generating quote images (quote-api repo)
- AI Integration: OpenAI/Anthropic APIs for smart message selection and content generation
- Sticker Management: Dynamic sticker pack creation and cleanup
- MongoDB: Uses Mongoose for data modeling
- Models: User, Group, Quote, Stats, Advertisement, Invoice models
- Caching: In-memory caching for privacy settings and forward lookups
- Handler System: Modular handlers in
handlers/directory - Middleware: Rate limiting, stats collection, session management
- Update Distribution: Consistent hashing for worker assignment
- Multi-format: Supports WebP stickers, PNG images, and documents
- AI-powered: Smart message selection using natural language queries
- Customization: Background colors, emoji brands, scaling options
- Privacy: Configurable privacy settings for user anonymization
- Health Monitoring: Circuit breaker pattern for TDLib operations
- Adaptive Scaling: Dynamic worker count based on CPU and queue load
- Rate Limiting: Per-user and per-chat rate limiting
- Error Handling: Comprehensive error handling with user-friendly messages
- Business API: Support for Telegram Business connections
- Inline Queries: Inline bot functionality
- Advertisement System: Built-in ad management
- Statistics: Usage tracking and analytics
BOT_TOKEN: Telegram bot tokenOPENAI_API_KEY: OpenRouter API key for AI featuresANTHROPIC_API_KEY: Anthropic API key for AI featuresQUOTE_API_URI: Quote generation API endpointMAX_WORKERS: Number of worker processesWORKER_HANDLER_TIMEOUT: Timeout for worker operations
- MongoDB connection configured in
database/connection.js - Models defined in
database/models/
- Requires TDLib binary at
helpers/tdlib/data/libtdjson.so - Can use prebuilt-tdlib npm package or manual compilation
- Handlers go in
handlers/directory - Database models in
database/models/ - Middleware in
middlewares/ - Utilities in
utils/
- Update received by master process
- Distributed to worker based on user/chat ID hash
- Worker processes update through handler middleware chain
- Database operations handled asynchronously
- Uses cluster mode for horizontal scaling
- Queue management prevents memory overflow
- Caching reduces database queries
- Circuit breaker prevents TDLib cascade failures
- Smart message selection with GPT-4 models
- Image processing for AI analysis
- Context-aware response generation
- Fallback handling for API failures