Skip to content

MrMissx/Telegram_Forwarder

Repository files navigation

Telegram Forwarder

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

Migration from V1

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.

Starting The Bot

Once you've setup your your configuration (see below) is complete, simply run:

python -m forwarder

or with poetry (recommended)

poetry run forwarder

Setting Up The Bot (Read the instruction bellow before starting the bot!):

Telegram Forwarder only supports Python 3.9 and higher.

Configuration

There are two files mandatory for the bot to work .env and chat_list.json.

.env

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 to True if you want to remove the tag ("Forwarded from xxxxx") from the forwarded message.

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": -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 topic 123456 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.

Python dependencies

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.

Launch in Docker container

Requrements

  • 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

Credits