Skip to main content

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

The TelegramService class is the main entry point for Telegram functionality:

Key Responsibilities:

  1. Bot Initialization
    • Creates Telegraf instance with bot token
    • Configures API root if custom endpoint provided
    • Handles connection lifecycle
  2. Middleware Setup
    • Preprocesses incoming updates
    • Manages chat synchronization
    • Handles user entity creation
  3. Event Registration
    • Message handlers
    • Callback query handlers
    • Edited message handlers
  4. Chat Management
    • Tracks known chats
    • Syncs chat metadata
    • Manages access control

Message Manager

The MessageManager class handles all message-related operations:

Message Processing Flow:

  1. Message Reception
  2. Format Conversion
  3. Media Processing
  4. 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

  1. Create Bot with BotFather
  2. 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

  1. Token Security
    • Never commit tokens to version control
    • Use environment variables
    • Rotate tokens periodically
  2. Rate Limiting
    • Implement exponential backoff
    • Cache frequently requested data
    • Use bulk operations when possible
  3. Group Management
    • Always check permissions before actions
    • Handle bot removal gracefully
    • Implement admin controls
  4. Error Handling
    • Log all API errors
    • Provide user-friendly error messages
    • Implement retry logic for transient errors
  5. Performance
    • Use webhooks in production
    • Implement message queuing
    • Optimize media processing

Support

For issues and questions: