Overview
The@elizaos/plugin-telegram
package provides comprehensive Telegram Bot API integration for elizaOS agents. It enables agents to operate as Telegram bots with support for private chats, groups, channels, media processing, interactive buttons, and forum topics.
This plugin handles all Telegram-specific functionality including:
- Initializing and managing the Telegram bot connection via Telegraf
- Processing messages across different chat types
- Handling media attachments and documents
- Managing interactive UI elements (buttons, keyboards)
- Supporting forum topics as separate conversation contexts
- Implementing access control and chat restrictions
Architecture Overview
Core Components
Telegram Service
TheTelegramService
class is the main entry point for Telegram functionality:
Key Responsibilities:
-
Bot Initialization
- Creates Telegraf instance with bot token
- Configures API root if custom endpoint provided
- Handles connection lifecycle
-
Middleware Setup
- Preprocesses incoming updates
- Manages chat synchronization
- Handles user entity creation
-
Event Registration
- Message handlers
- Callback query handlers
- Edited message handlers
-
Chat Management
- Tracks known chats
- Syncs chat metadata
- Manages access control
Message Manager
TheMessageManager
class handles all message-related operations:
Message Processing Flow:
-
Message Reception
-
Format Conversion
-
Media Processing
-
Response Handling
Utilities
Various utility functions support the core functionality:Event Processing Flow
Message Flow
Callback Query Flow
Configuration
Environment Variables
Character Configuration
Bot Creation
-
Create Bot with BotFather
-
Configure Bot Settings
Message Handling
Message Types
The plugin handles various Telegram message types:Message Context
Each message maintains context about its origin:Message History
The plugin tracks conversation history:Media Processing
Image Processing
Voice Transcription
Document Handling
Interactive Elements
Inline Keyboards
Create interactive button layouts:Callback Handling
Process button clicks:Reply Keyboards
Create custom keyboard layouts:Group Management
Access Control
Restrict bot to specific groups:Group Features
Handle group-specific functionality:Privacy Mode
Handle bot privacy settings:Forum Topics
Topic Detection
Identify and handle forum topics:Topic Context
Maintain separate context per topic:Error Handling
API Errors
Handle Telegram API errors:Multi-Agent Environment
Handle bot token conflicts:Connection Management
Handle connection issues:Integration Guide
Basic Setup
Custom Message Handler
Override default message handling:Webhook Setup
Configure webhooks for production:Testing
Best Practices
-
Token Security
- Never commit tokens to version control
- Use environment variables
- Rotate tokens periodically
-
Rate Limiting
- Implement exponential backoff
- Cache frequently requested data
- Use bulk operations when possible
-
Group Management
- Always check permissions before actions
- Handle bot removal gracefully
- Implement admin controls
-
Error Handling
- Log all API errors
- Provide user-friendly error messages
- Implement retry logic for transient errors
-
Performance
- Use webhooks in production
- Implement message queuing
- Optimize media processing
Support
For issues and questions:- 📚 Check the examples
- 💬 Join our Discord community
- 🐛 Report issues on GitHub