A simple Telegram Python bot running on Python3 to automatically forward messages from one chat to another.
v2 uses a different configuration file format. Please refer to the Configuration section for more information. The bot will not start if the configuration file is not in the correct format.
Once you've setup your your configuration (see below) is complete, simply run:
python -m forwarder
or with poetry (recommended)
poetry run forwarder
Telegram Forwarder only supports Python 3.9 and higher.
There are two files mandatory for the bot to work .env
and chat_list.json
.
Template env may be found in sample.env
. Rename it to .env
and fill in the values:
-
BOT_TOKEN
- Telegram bot token. You can get it from @BotFather -
OWNER_ID
- An integer of consisting of your owner ID. -
REMOVE_TAG
- set toTrue
if you want to remove the tag ("Forwarded from xxxxx") from the forwarded message.
Template chat_list may be found in chat_list.sample.json
. Rename it to chat_list.json
.
This file contains the list of chats to forward messages from and to. The bot expect it to be an Array of objects with the following structure:
[
{
"source": -10012345678,
"destination": [-10011111111, "-10022222222#123456"]
},
{
"source": "-10087654321#000000", // Topic/Forum group
"destination": ["-10033333333#654321"],
"filters": ["word1", "word2"] // message that contain this word will be forwarded
},
{
"source": -10087654321,
"destination": [-10033333333],
"blacklist": ["word3", "word4"] // message that contain this word will not be forwarded
},
{
"source": -10087654321,
"destination": [-10033333333],
"filters": ["word5"],
"blacklist": ["word6"]
// message must contain word5 and must not contain word6 to be forwarded
}
]
-
source
- The chat ID of the chat to forward messages from. It can be a group or a channel.If the source chat is a Topic groups, you MUST explicitly specify the topic ID. The bot will ignore incoming message from topic group if the topic ID is not specified.
-
destination
- An array of chat IDs to forward messages to. It can be a group or a channel.Destenation supports Topics chat. You can use
#topicID
string to forward to specific topic. Example:[-10011111111, "-10022222222#123456"]
. With this config it will forward to chat-10022222222
with topic123456
and to chat-10011111111
. -
filters
(Optional) - An array of strings to filter words. If the message containes any of the strings in the array, it WILL BE forwarded. -
blacklist
(Optional) - An array of strings to blacklist words. If the message containes any of the string in the array, it will NOT BE forwarded.
You may add as many objects as you want. The bot will forward messages from all the chats in the source
field to all the chats in the destination
field. Duplicates are allowed as it already handled by the bot.
Install the necessary python dependencies by moving to the project directory and running:
poetry install --only main
or with pip
pip3 install -r requirements.txt
This will install all necessary python packages.
- Docker
- docker compose
Before launch make sure all configuration are completed (.env
and chat_list.json
)!
Then, simply run the command:
docker compose up -d
You can view the logs by the command:
docker compose logs -f