Skip to content

yangsheng6810/no_dup_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

No Duplication Bot

A Telegram bot that shouts out when some channel message is forwarded to a group multiple times. 中文 Readme

We love to forward all kinds of messages (usually some kind of news) to group chats, and the messages sometimes lead to nice discussions. However, a same message may be forwarded multiple times, usually by users who didn’t see the first forward and discussion. It soon becomes frustrating: people who saw its previously feel the forwarder wasn’t reading discussion history, while the new forwarder has no idea that it was forwarded before. This bot detects the later forwards, reply to those with a link to the first forwarded message in the current group.

Design

Currently, the bot reacts to either

  • a message that is a URL,
  • a message forwarded from a public channel.

It does not reacts to

  • a message forwarded from another group,
  • a message forwarded from the current group (This is by design, as it is a very common practice known as “复读机”),
  • a message from a private chat.

When a message is forwarded into the current group, we obtain a link to the original message from the public channel. Previously seen links are stored in a KV store with some other information, including the number of times we have seen it in the current group. If we have seen new link before, we increase its count, reply to the message with the link. Otherwise, we just store it in the KV store with a count of one. To have separate counting for different group, the id of the group is also part of the key. The handling of a URL is similar.

Deployment

The instructions below shows how to deploy your bot on a Ubuntu 20.04 LTS server.

Prepare the bot

First of all, you must have a Telegram bot. You can apply for one by talking with @BotFather. See official documentation for more information.

The bot is supposed to be used in a group, so add the bot to a group before using. Then, check the following settings of the bot in BotFather - /mybots - @your_bot - Bot Settings:

  • Set Allow Groups? as off after adding your bot to your group. This will make sure only the users in your group can use the bot.
  • Group Privacy should be disabled. This will make sure your bot has access to the group messages.

For convience, you can also set supported commands in @BotFather, so that Telegram will suggest a list of supported commands with descriptions to help the user enter commands quickly when the user enters a / in the group. To set this, enter /setcommands in @BotFather, and choose your bot username. Copy the following texts and send them to BotFather, and the setting will be effective in several minutes.

help - Get help
delete - [admin only] Reply to a bot message to delete it
top - Show users with most duplicated messages
topics - Show most duplicated messages
me - Show the number of duplicate messages I sent
resettop - [admin only] Reset the top record for the current chat

Get the code

git clone https://github.com/yangsheng6810/no_dup_bot.git
cd no_dup_bot # Assume this folder is in <YOUR_PATH>.

Install Rust and dependencies

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
sudo apt-get update
sudo apt install build-essential pkg-config libssl-dev

Modify & build the bot

First, you need to modify no_dup_bot in L26 of main.rs to your own bot username (i.e., the one that ends with bot).

static BOT_NAME: &str = "no_dup_bot";

Then you can start to build the bot.

cargo check # Check the bot package and all of its dependencies for errors.
cargo build --release
cd <YOUR_PATH>/no_dup_bot/target/release

Setup environment variables and start the bot

Get <YOUR_BOT_TOKEN> from @BotFather, and get <ADMIN_USER_ID> from @userinfobot.

export TELOXIDE_TOKEN=<YOUR_BOT_TOKEN>
export NO_DUP_BOT_ADMIN=<ADMIN_USER_ID>

Finally, start the bot and enjoy it!

cd <YOUR PATH>/no_dup_bot/target/release && ./no_dup_bot

Usage

Use the following command format in a group to interact with the bot.

/<COMMAND> @<YOUR_BOT_USERNAME>

For example, assume that your bot username is no_dup_bot, then you can use the following command in a group to show the help messages.

/help @no_dup_bot

You will see the following reply:

These commands are supported:
/help - Get help
/delete - Reply to a bot message to delete it
/top - Show users with most duplicated messages
/topics - Show most duplicated messages
/me - Show the number of duplicate messages I sent
/resettop - Reset the top record for the current chat

When you send a command to the bot by replying to a bot’s message, you only need to send /<COMMAND>.

If you already tell BotFather the commands supported by the bot (see Prepare the bot), you only need to enter a /, and Telegram will suggest all the available commands to help you enter the entire command quickly.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published