How to add the user ban function by id in Telegram bot?
Ie so that I could enter /ban userid and this user could no longer write to the bot.
How to add the user ban function by id in Telegram bot?
Ie so that I could enter /ban userid and this user could no longer write to the bot.
The user in any case will be able to write to the bot, but it’s up to us to decide whether to answer it or not.
There are 2 ways to do this, the first with the help of a decorator, who will check whether our user is banned:
from telebot import TeleBot from functools import wraps bot = TeleBot('<token>') banned_users = [] def is_not_banned(func): @wraps(func) def decorator(message): if message.from_user.id not in banned_users: return func(message) return decorator @bot.message_handler(commands=['ban']) def ban_user(message): message_args = message.text.split() if len(message_args) == 2: banned_users.append(int(message_args[1])) bot.reply_to(message, text='OK') else: bot.reply_to(message, text='Wrong format') @bot.message_handler() @is_not_banned def foo(message): bot.reply_to(message, text='Hey!') How it works:
decorator function that has locked the original handler.This method has its drawback, we process the updates of banned users.
This can be avoided by overriding the get_updates method:
from telebot import TeleBot from telebot import apihelper from telebot import types class MyTeleBot(TeleBot): def get_updates(self, *args, **kwargs): json_updates = apihelper.get_updates(self.token, *args, **kwargs) ret = [] for ju in json_updates: if ju['message']['from']['id'] in banned_users: self.last_update_id = ju['update_id'] else: ret.append(types.Update.de_json(ju)) return ret Now create an instance of our class:
bot = MyTeleBot('<token>') Then we can write our handlers without any decorators, the ban_user function remains unchanged.
Well, do not forget to start polling:
if __name__ == '__main__': bot.infinity_polling() How it works:
infinity_polling > polling > __threaded_polling > __retrieve_updates > get_updates > process_new_updatesprocess_new_updates we "cut off" all unnecessary updates, thus our handler does not even work.You probably noticed this test in our class:
if ju['message']['from']['id'] in banned_users: self.last_update_id = ju['update_id'] The fact is that if you do not do self.last_update_id = ju['update_id'] , our top bot will get stuck on the latest update, which we want to skip.
Actually this is what process_new_updates does:
if update.update_id > self.last_update_id: self.last_update_id = update.update_id Instead of the banned_users array, banned_users advisable to take a database.
Source: https://ru.stackoverflow.com/questions/897864/
All Articles