Sending messages from group to group in Telegram using a bot, the main difference between a user and a bot is the ability to edit messages.
If you want to send messages as a USER, use: fuserbot
version 4.x works with Telegram Python bot ver 22+
version 3.x works with Telegram Python bot ver 21+
version 2.x works with Telegram Python bot ver 20-21
version 1.X works with Telegram Python bot ver 13.15 and below!!
The bot must be granted deletion privileges in order to delete messages user leave or join the group, and also commands by users other than the bot owner.
A simple Telegram Python bot running on Python3 to automatically forward messages from one chat to another.
Automatic deletion of messages after a specified period of time, please note that you cannot delete messages of another bot, Because of Telegram limitations.
Languages can be added in strings.py, the default language is the user's language, if the user's language cannot be found or in channels the default is English.
Please make sure to use the latest Python version. (Recommended)
Configuring your bot: a config.env and chat_list.json files.
Both files should be placed in the project root folder (the folder you run python3 -m tzbot from), not inside the tzbot folder.
config.env is where your bot token will be loaded from, and most of your other settings.
Template env may be found in sample.config.env. Rename it to config.env and fill in the values:
| Value | Type | Default | Description | Options |
|---|---|---|---|---|
BOT_TOKEN |
str |
none |
Telegram bot token. You can get it from @BotFather | string |
OWNER_ID |
int |
none |
An integer or a comma-separated list of consisting of your owner ID. | integer |
REMOVE_TAG |
str |
False |
set to True if you want to remove the tag ("Forwarded from xxxxx") from the forwarded message. |
False True |
DEFAULT_LANG |
str |
en |
Default language when the user language cannot be retrieved and in channels, if blank defaults to English. | en / he |
APSCHEDULER_LOG_LEVEL |
str |
INFO |
Syslog log levels are defined in RFC 5424 | string |
APSCHEDULER_SCHEDULER_INFO |
int |
0 |
An integer that determines whether schedule additions or removals will be displayed: 0 = hidden, 1 = visible |
0 / 1 |
LOG_FILE |
str |
tzbot/tzbot.log |
Path to the log file. Relative paths are relative to the project root. The folder is created automatically if it does not exist. | string |
CHAT_LIST_FILE |
str |
chat_list.json |
Path to the chat list file. Relative paths are relative to the project root. | string |
TZ |
str |
UTC (in Docker) |
Time zone for the log timestamps, e.g. Asia/Jerusalem. Outside Docker the system time zone is used. |
string |
An example config.env file could be:
BOT_TOKEN = 1234567890:Abcdef1234567890GHIJ
OWNER_ID = 1234567890, 0987654321
REMOVE_TAG = True
DEFAULT_LANG = en
APSCHEDULER_LOG_LEVEL = INFO
APSCHEDULER_SCHEDULER_INFO = 0
LOG_FILE = logs/tzbot.logTemplate 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- 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(Optional) - An array of chat IDs to forward messages to. It can be a group or a channel.Destination supports Topics chat. You can use
#topicIDstring to forward to specific topic. Example:[-10011111111, "-10022222222#123456"]. With this config it will forward to chat-10022222222with topic123456and to chat-10011111111. -
filters(Optional) - An array of strings to filter words. If the message contains any of the strings in the array, it WILL BE forwarded. If omitted, all messages are forwarded. -
blacklist(Optional) - An array of strings to blacklist words. If the message contains any of the strings in the array, it will NOT BE forwarded. -
delete_after(Optional) - An integer. Time in seconds after which messages will be deleted.delete_afterapplies to the whole source chat, not just to the rule it is written in: every message in that chat is deleted after the given time, regardless offilters/blacklistand whether it was forwarded. Bot replies to commands in that chat are deleted as well.Forwarded copies are deleted only if the destination chat has its own
delete_after(as asourceentry).If the same source chat has more than one
delete_after, the last one in the file wins.
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.
It is also possible to enter several different filters on the same source.
[
{
"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"],
"delete_after": 60
// message must contain word5 and must not contain word6 to be forwarded
// ALL messages in chat -10087654321 will be deleted after 60 seconds
},
{
"source": -10099999999,
"delete_after": 2700
// all messages in this chat will be deleted after 2700 seconds (no forwarding)
}
]Install the necessary python dependencies by moving to the project directory and running:
$ git clone https://github.com/tzagim/tzbot
$ cd tzbotIf you are using pip:
$ pip install -r requirements.txtIf you are using advanced versions of linux that pip cannot be used:
$ sudo apt install python3-anyio python3-certifi python3-h11 python3-httpcore python3-httpx python3-idna python3-dotenv python3-python-telegram-bot python3-sniffio python3-apschedulerNote: distribution packages may be older than the versions required (python-telegram-bot 22+). If the bot fails to start, use pip in a virtual environment instead.
This will install all necessary python packages.
- Docker
- docker compose
Before launch make sure all configuration is completed (config.env and chat_list.json in the project root)!
chat_list.json is mounted into the container, so after editing it just run docker compose restart. Changes to config.env require docker compose up -d to take effect.
The log is written to data/tzbot.log in the project root, so it is kept across docker compose up -d and rebuilds.
By default the container runs as root. To run it as a regular user, create a docker-compose.override.yml next to docker-compose.yml (it is merged automatically and ignored by git), for example:
services:
tzbot:
user: "1000:1000" # the output of: id -u; id -g
read_only: true
tmpfs:
- /tmp
security_opt:
- no-new-privileges:true
cap_drop:
- ALLThe data folder must be writable by that user: mkdir -p data && chown 1000:1000 data.
Then, simply run the command:
docker compose up -dYou can view the logs by the command:
docker compose logs -fOnce your configuration (see above) is complete, run from the project root:
$ python3 -m tzbot
Can be run in a screen with the following command:
$ screen -dmS tzbot python3 -m tzbot
Based on: MrMissx - Telegram_Forwarder