T
T
Trefame2020-06-23 01:43:02
PHP
Trefame, 2020-06-23 01:43:02

How to protect a Telegram bot from spam?

There was a problem with my telegram bot, one of its users decided to spam it with some software - it turned out quite successfully, the bot lay down. How can you implement an anti-spam system so that when you try to send, let's say more than one message in 5 seconds, a stop message arrives: There must be a pause of 5 seconds between messages! You may be blocked for continuing to spam. If the user ignored the warning and sent, let's say, more than 5 messages without a pause, then his account, which I store in the database, acquired the status - Blocked, and the bot checked the user's status and if it was blocked, it simply exited via exit(), but it doesn't matter, the main thing is how do I store the remaining time and so on? Nothing comes to mind..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2020-06-23
@Trefame

To do this, you need to keep track of the user_id and timestamp of the last message in the bot.
You can create a table in the database with 2 columns: user_id (unique), last_message_time.
When a new message arrives from a user, you get the last_message_time for the current user_id and see how many seconds have passed between the last message and the current time.
If less than 5 seconds have passed, then send a warning and consider that there was a violation from user_id, if more than 5 seconds have passed, then we consider the message valid and do not forget to update last_message_time for user_id.
The only way to spam the bot in this case is to send messages from many different accounts, which is already much more difficult for an attacker.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question