Note: Telegram - Anti Forward Bot (Ban Spam Forwards)

Published on

Original language: Chinese . AI translations: English , Japanese .


No more flooding~


Flooding Is Annoying

In group chats, there are always people forwarding messages from other channels. One forwarded message can take up a whole screen, and they forward frequently. Too much flooding.
Looking at those forwarded messages, it’s mostly the same two or three accounts forwarding, and they forward from the same one or two channels. Very likely bots.

Solution: ban either the accounts or the channels.

Since I’m not afraid of trouble, I decided to tinker with a Telegram bot that deletes messages forwarded from specific channels.

Find A Wheel

I searched on Google for a long time and didn’t find much.
Then I remembered: just search GitHub directly. Found this: Tooruchan / Anti-Forward-Bot

The author provided usage instructions.
But the README is very short, and for a newbie like me it was still difficult. After some tinkering, I finally deployed it.

Below is the process.

Steps

Create A Bot

Chat with @BotFather in tg and create a bot.
Follow the prompts and you’ll get a token (as important as a password, don’t leak it).
To use bots in groups, you need to disable privacy mode. The GitHub page explains how.

Apply For An App

(I’m not sure what this is for, but I followed the steps.)

Apply for an application at Telegram - API Settings.
There’s a form; I didn’t know what URL and platform meant so I just filled them randomly.
After submitting, you can see API Hash and API ID.

Deploy The Service

Follow Tooruchan / Anti-Forward-Bot

  • pip3 install -U pyrogram tgcrypto
    Install the framework by Telegram (not sure what it is exactly).

  • git clone https://github.com/Tooruchan/Anti-Forward-Bot.git
    Clone the repo and cd into it.

  • vim config.json
    Edit the config and fill in your bot Token, API Hash, API ID.

  • vim channels.json
    I tried a few ways online to get channel id, none worked well.
    In the debugging stage I found a method:

    • Run the service with python3 bot.py (Pyrogram quickstart: Get Pyrogram Real Fast)
    • Create a small group chat and add the bot
    • Forward messages from the channel you want to block into this group
    • The server CLI prints parsed message fields, including forward_from_chat
    Terminal window
    "forward_from_chat": {
    "_": "pyrogram.Chat",
    "id": -1001232395606,
    "type": "channel",
    "is_verified": false,
    "is_restricted": false,
    "is_scam": false,
    ...
    }
    • "id": -1001232395606 is the channel id. Put it into the config.

Run In Background

screen -dmS anti_forward_bot python3 bot.py
After testing, run it in the background.


Skill issue meant I stepped on a few rakes, but I got it working in the end.
Nice~


Common screen Commands

I’ve used screen a few times to run processes in the background.
Quick command notes:

Terminal window
screen -ls
screen -r process_id
screen -dmS session_name command

Ref: How To Use Linux Screen