I’m new to do this kind of project. My goals is to build a telegram bot to forward user(s) message from the bot to a channel. Right now I’m facing that some users abuse to send junk message that disturbs a lot. So, is it possible to blacklist some user from using the bot?
My sourcecode is here Go to GitHub
2
Answers
Bots can’t block users (like users can block bots), but you can choose to just not handle updates that come from a specific user id. What I usually do in such cases is to use a
telegram.ext.TypeHandler(telegram.Update, callback)
wherecallback
looks something likeThen register it to a low group for the dispatcher (
dispatcher.add_handler(…, group=-1)
).Please have a look at the docs of
TypeHandler
,DispatcherHandlerStop
andadd_handler
for more info 🙂One way to keep track of the
blocked_users
is to store that list incontext.bot_data
.Disclaimer: I’m currently the maintainer of
python-telegram-bot
.There isn’t any native way to do that, however you can have a list of blocked users (preferably a separate JSON file for that extra modularity) and every time the bot is used, check if the user is in that list: