@why2

Telebot. Как сделать защиту от спама?

Пишу. Бота в тг(отслеживает курс доллара и биткоина). Есть встроенная клавиатура, например: Rate dollar. Как сделать так, что бы пользователь не мог спамить Rate Dollar и другим текстом. Библиотека telebot
if message.chat.type == 'private':
	
		if message.text == ' Rate dollar ':
			

			bot.send_sticker(message.chat.id, dol)
			bot.send_message(message.chat.id,' Dollar now: ' + str(cur) + ' ₽ ')
  • Вопрос задан
  • 1968 просмотров
Пригласить эксперта
Ответы на вопрос 1
@SashaN69
Школота
Закинуть в текстовый хендлер и вот вам защита от спама
import time as tm
spams = {}
msgs = 4 # Messages in
max = 5 # Seconds
ban = 300 # Seconds
def is_spam(user_id):
    try:
        usr = spams[user_id]
        usr["messages"] += 1
    except:
        spams[user_id] = {"next_time": int(tm.time()) + max, "messages": 1, "banned": 0}
        usr = spams[user_id]
    if usr["banned"] >= int(tm.time()):
        return True
    else:
        if usr["next_time"] >= int(tm.time()):
            if usr["messages"] >= msgs:
                spams[user_id]["banned"] = tm.time() + ban
                # text = """You're banned for {} minutes""".format(ban/60)
                # bot.send_message(user_id, text)
                # User is banned! alert him...
                return True
        else:
            spams[user_id]["messages"] = 1
            spams[user_id]["next_time"] = int(tm.time()) + max
    return False
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы