DEV Community

loading...
Cover image for Bulk Forward messages in Telegram using this Python Script 😎

Bulk Forward messages in Telegram using this Python Script 😎

aahnik profile image Aahnik Daw Originally published at github.com ・3 min read

telegram-chat-forward

A simple script to forward all the messages of one chat (private/group/channel) to another. Made using Telethon. Can be used to back up the contents of a chat to another place.

Installation

  • Clone this repo and move into it to get started.
git clone https://github.com/aahnik/telegram-chat-forward.git && cd telegram-chat-forward
Enter fullscreen mode Exit fullscreen mode
  • Create a virtual environment and install dependencies.
python3 -m venv venv && . venv/bin/activate
pip3 install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Note: For Windows, the process for activating a virtual environment is different, search Google.

Signing in

First of all you need to have your Telegram account's api_id and api_hash.
Learn how to get them.

Setup

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=kjfjfk9r9JOIJOIjoijf9wr0w
Enter fullscreen mode Exit fullscreen mode

Replace 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.

Configuration

  • The from and to in the config.ini has to be a username/phone/link/chat_id of the chat.
  • To confirm that you are using the correct from and to run the get_chat_info.py script.

  • 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 = whatAz
to = testWha
offset = 0
; the offset will auto-update, keep it zero initially
[another name]
; the name of section must be unique
from = someone
to = another
offset = 0
Enter fullscreen mode Exit fullscreen mode

Note:Any line starting with ; in a .ini file, is treated as a comment.

Offset

  • When you run the script for the first time, keep offset=0.
  • When the script runs, the value of offset in config.ini gets updated automatically.
  • Offset is basically the id of the last message forwarded from from to to.
  • When you run the script next time, the messages in from having an id greater than offset (newer messages) will be forwarded to to. That is why it is important not to loose the value of offset.

Execution

After setting up the config.ini, run the forwarder.py script.

python3 forwarder.py
Enter fullscreen mode Exit fullscreen mode

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.

Feel free to ask your questions via Telegram chat with me. For bugs and feature requests use the issues section of this repo.

Extra

To get all information about any particular chat:

  • Run the script run_chat_info.py.
  • Open Telegram and send .id to any chat to get the chat id.
  • Send .info to any chat to get details of the chat.
  • When you are done, come back to your terminal and press CTRL + C to stop.

telegram-chat
made-with-python
MIT license

Discussion (1)

pic
Editor guide
Collapse
aahnik profile image
Aahnik Daw Author

Do you want to achieve live syncing, or real time forwarding then contact me

Chat with me on telegram
telegram.me/aahnikdaw