A simple script to forward all the messages of one chat (indivisual/group/channel) to another. Made using Telethon. Can be used to back up the contents of a chat to another place.
To use this Telegram forwarder, you need your Telegram account’s API credentials:
API_ID– your numeric Telegram API IDAPI_HASH– your unique Telegram API hash
You can get them by following Telegram API Documentation.
# .env
API_ID=1234567
API_HASH=0123456789abcdef0123456789abcdef
STRING_SESSION="" # optional: can leave empty if you want to login manually- Clone this repo and move into it to get started.
git clone https://github.com/its-anya/telegram-all-forward.git && cd telegram-all-forward- Create a virtual environment and install dependencies.
python -m venv venvFor Windows, you may need to bypass execution policy to activate the virtual environment:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\venv\Scripts\Activate.ps1Install dependencies (if you encounter compilation errors on Windows, use the --only-binary flag):
pip install -r requirements.txt --only-binary=cffiNote: For Windows, the process for activating a virtual environment is different, search Google.
You must have the api_id and api_hash as environment variables.
You may simply create a file named .env in the project directory and paste the following into it.
api_id=12345
api_hash=kjfjfk9r9JOIJOIjoijf9wr0wReplace the above values with the actual values for your telegram account.
After this you need to create and fill up the config.ini file with your forwarding configurations.
- The
fromandtoin theconfig.inihas to be a username/phone/link/chat_id of the chat. - The chat id is the best way for configurations. It will always be accurate. To get the chat id of a chat, forward any message of the chat to @userinfobot
- You may have as many as forwarding pairs as you wish. Make sure to give a unique header to each pair. Follow the syntax shown below.
[name of forward1]
; in the above line give any name as you wish
; the square brackets around the name should remain
from = https://t.me/someone
to = -1001235055711
offset = 0
; the offset will auto-update, keep it zero initially
[another name]
; the name of section must be unique
from = @username
to = @anothername
offset = 0Note:Any line starting with
;in a.inifile, is treated as a comment.
- When you run the script for the first time, keep
offset=0. - When the script runs, the value of offset in
config.inigets updated automatically. - Offset is basically the id of the last message forwarded from
fromtoto. - When you run the script next time, the messages in
fromhaving an id greater than offset (newer messages) will be forwarded toto. That is why it is important not to loose the value ofoffset.
Telegram enforces rate limits to prevent abuse. If you encounter a FloodWaitError, the script will automatically wait for the required time before continuing. However, for the final summary messages, you may need to manually wait.
If you see an error like:
FloodWaitError: A wait of 2891 seconds is required
This means you need to wait approximately 48 minutes before running the script again. You can use the check_flood_wait.py script to calculate when you can run the forwarder again:
python check_flood_wait.pyTo reduce the likelihood of hitting rate limits:
- Avoid running the script too frequently
- Forward smaller batches of messages at a time
- The script now includes small delays between messages to help prevent rate limiting
After setting up the config.ini, run the forwarder.py script.
python forwarder.pyIn my case is slow_forward.py is a best without flood wait. You have to login for the first time using your phone number (inter-national format) and login code.
A session file called forwarder.session will be generated.
Please don't delete this and make sure to keep this file secret.
A huge thanks to the amazing open-source projects that inspired and made this work possible:
-
telegram-chat-forward — by @aahnik
A simple script using Telethon to forward messages from one chat to another.
-
A powerful, advanced Telegram message forwarder with real-time and static forwarding modes.
Your contributions to the open-source community are truly appreciated! 🙏
