TGForwarder
TGForwarder is a self-hosted message forwarding tool for Telegram. It uses your bot token to monitor a source chat (channel, group, or private) and forwards all messages to a specified destination. Perfect for automation, backups, or cross-posting.
Install / Use
/learn @Linuxmaster14/TGForwarderREADME
Telegram Forwarder
A Python script using Telethon to automatically forward messages from a source chat/group/channel to a target destination.
Features
- Forward messages from any Telegram chat, group, or channel
- Multiple source/target mapping support
- One-to-many and many-to-one forwarding
- Support for both user accounts and bot accounts
- Optional removal of "Forward from..." signature
- Optional quiet mode for console logging
- Automatic handling of rate limits
- Comprehensive logging
- Easy configuration via environment variables
Prerequisites
- Telegram API Credentials: Get your
api_idandapi_hashfrom https://my.telegram.org - Bot Token (optional): If you want to use a bot account, get a bot token from @BotFather
- Chat IDs: You need the IDs of the source and target chats/groups/channels
Installation
- Clone this repository:
git clone https://github.com/Linuxmaster14/TGForwarder.git
cd TGForwarder
- Install the required dependencies:
pip install -r requirements.txt
- Create a
.envfile based on the example:
cp .env.example .env
- Edit the
.envfile and fill in your credentials:
Option 1: Single Source/Target (Legacy)
API_ID=your_api_id_here
API_HASH=your_api_hash_here
BOT_TOKEN=your_bot_token_here # Optional, for bot mode
SOURCE_ID=your_source_chat_id
TARGET_ID=your_target_chat_id
Option 2: Multiple Sources/Targets (Recommended)
API_ID=your_api_id_here
API_HASH=your_api_hash_here
BOT_TOKEN=your_bot_token_here # Optional, for bot mode
FORWARDING_RULES=-1001111111111:-1002222222222,-1003333333333:-1004444444444
Getting Chat IDs
To find chat IDs, you can:
- For private chats: Use the user's ID (positive number)
- For groups/channels: Use the negative ID format
- For groups:
-100+ group ID (e.g.,-1001234567890) - For channels:
-100+ channel ID (e.g.,-1001234567890)
- For groups:
You can use tools like @userinfobot or @get_id_bot to get chat IDs.
Usage
Run the script with various options:
# Basic usage
python telegram_forwarder.py
# Remove "Forward from..." signature (sends as new messages)
python telegram_forwarder.py --remove-forward-signature
# Disable console logging (only log to file)
python telegram_forwarder.py --disable-console-log
# Use both options
python telegram_forwarder.py -r -q
Command Line Arguments
| Argument | Short | Description |
|----------|-------|-------------|
| --remove-forward-signature | -r | Remove "Forward from..." signature by sending as new messages instead of forwarding |
| --disable-console-log | -q | Disable console logging (only log to telegram_forwarder.log file) |
First Run
- User Mode: If you're not using a bot token, you'll be prompted to enter your phone number and verification code
- Bot Mode: If you provided a bot token, the script will start immediately
The script will run continuously and forward any new messages from the source to the target.
Configuration Options
Environment Variables
| Variable | Required | Description |
|----------|----------|-------------|
| API_ID | Yes | Your Telegram API ID |
| API_HASH | Yes | Your Telegram API Hash |
| BOT_TOKEN | No | Bot token for bot mode (leave empty for user mode) |
| SOURCE_ID | No* | ID of the source chat/group/channel (legacy single mode) |
| TARGET_ID | No* | ID of the target chat/group/channel (legacy single mode) |
| FORWARDING_RULES | No* | Multiple forwarding rules (see format below) |
*Either SOURCE_ID/TARGET_ID OR FORWARDING_RULES must be provided.
Forwarding Rules Format
The FORWARDING_RULES environment variable supports flexible mapping:
Format: source_id:target_id1:target_id2,source_id2:target_id3
Examples:
# One-to-one mapping
FORWARDING_RULES=-1001111111111:-1002222222222
# One-to-many (one source to multiple targets)
FORWARDING_RULES=-1001111111111:-1002222222222:-1003333333333
# Many-to-one (multiple sources to one target)
FORWARDING_RULES=-1001111111111:-1004444444444,-1002222222222:-1004444444444
# Complex mapping
FORWARDING_RULES=-1001111111111:-1002222222222,-1001111111111:-1003333333333,-1004444444444:-1005555555555
Forwarding Patterns
The script supports various forwarding patterns:
1. One-to-One Forwarding
Forward messages from one source to one target:
FORWARDING_RULES=-1001111111111:-1002222222222
2. One-to-Many Forwarding
Forward messages from one source to multiple targets:
FORWARDING_RULES=-1001111111111:-1002222222222:-1003333333333:-1004444444444
3. Many-to-One Forwarding
Forward messages from multiple sources to one target:
FORWARDING_RULES=-1001111111111:-1005555555555,-1002222222222:-1005555555555,-1003333333333:-1005555555555
4. Complex Mapping
Mix of different patterns:
FORWARDING_RULES=-1001111111111:-1002222222222,-1001111111111:-1003333333333,-1004444444444:-1005555555555:-1006666666666
User Mode vs Bot Mode
- User Mode: Uses your personal Telegram account. Can access any chat you're a member of.
- Bot Mode: Uses a bot account. The bot must be added to both source and target chats with appropriate permissions.
Important Notes
- Rate Limits: The script automatically handles Telegram's rate limits
- Permissions: Ensure the account/bot has necessary permissions in both source and target chats
- Privacy: Be mindful of privacy and legal considerations when forwarding messages
- Session Files: The script creates session files (
user_session.sessionorbot_session.session) to avoid re-authentication
Logging
The script provides detailed logging including:
- Connection status
- Message forwarding events
- Error handling
- Rate limit notifications
Log Output
- Default: Logs to both console and
telegram_forwarder.logfile - Quiet mode (
-qflag): Logs only totelegram_forwarder.logfile
Forward Signature Options
- Default: Messages are forwarded with "Forward from..." signature
- Remove signature (
-rflag): Messages are sent as new messages without the forward signature
Troubleshooting
Common Issues
- Authentication Failed: Check your API credentials
- Permission Denied: Ensure the account/bot has access to both chats
- Invalid Chat ID: Verify the chat IDs are correct
- Rate Limited: The script handles this automatically, but frequent rate limits may indicate too much activity
Error Messages
Missing required environment variables: Check your.envfileSOURCE_ID and TARGET_ID must be valid integers: Ensure IDs are numbersError getting entity info: The account/bot cannot access the specified chat
License
This project is licensed under the terms specified in the LICENSE file.
Author
Made with Linuxmaster14
Disclaimer
This tool is for educational and personal use. Please respect Telegram's Terms of Service and applicable laws regarding message forwarding and privacy.
Related Skills
claude-opus-4-5-migration
107.8kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
model-usage
347.0kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
TrendRadar
50.8k⭐AI-driven public opinion & trend monitor with multi-platform aggregation, RSS, and smart alerts.🎯 告别信息过载,你的 AI 舆情监控助手与热点筛选工具!聚合多平台热点 + RSS 订阅,支持关键词精准筛选。AI 智能筛选新闻 + AI 翻译 + AI 分析简报直推手机,也支持接入 MCP 架构,赋能 AI 自然语言对话分析、情感洞察与趋势预测等。支持 Docker ,数据本地/云端自持。集成微信/飞书/钉钉/Telegram/邮件/ntfy/bark/slack 等渠道智能推送。
mcp-for-beginners
15.8kThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.
