Skip to content
tzagimPublic

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

tzbot

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

please note:

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!!

Features

Deleting join and leave messages and commands

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.

Auto Forwarder

A simple Telegram Python bot running on Python3 to automatically forward messages from one chat to another.

Auto Delete messages after X time

Automatic deletion of messages after a specified period of time, please note that you cannot delete messages of another bot, Because of Telegram limitations.

Translation

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.

Setting Up The Bot (Read Before Trying To Use!):

Please make sure to use the latest Python version. (Recommended)

Configuration

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.

config.env

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

chat_list.json

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 - 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 #topicID string to forward to specific topic. Example: [-10011111111, "-10022222222#123456"]. With this config it will forward to chat -10022222222 with topic 123456 and 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_after applies 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 of filters / blacklist and 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 a source entry).

    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)
  }
]

Installing

Python dependencies

Install the necessary python dependencies by moving to the project directory and running:

$ git clone https://github.com/tzagim/tzbot
$ cd tzbot

If you are using pip:

$ pip install -r requirements.txt

If 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-apscheduler

Note: 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.

Launch in Docker container

Requirements

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

Running as a non-root user (optional)

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:
      - ALL

The data folder must be writable by that user: mkdir -p data && chown 1000:1000 data.

Then, simply run the command:

docker compose up -d

You can view the logs by the command:

docker compose logs -f

Starting The Bot

Once 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

Credits

Based on: MrMissx - Telegram_Forwarder

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages