skip to Main Content

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


  1. 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) where callback looks something like

    def callback(update, context):
        if update.effective_user and update.effective_user in blocked_users:
            # This stops any other handlers in higher groups from running   
            raise DispatcherHandlerStop 
    

    Then register it to a low group for the dispatcher (dispatcher.add_handler(…, group=-1)).
    Please have a look at the docs of TypeHandler, DispatcherHandlerStop and add_handler for more info 🙂

    One way to keep track of the blocked_users is to store that list in context.bot_data.


    Disclaimer: I’m currently the maintainer of python-telegram-bot.

    Login or Signup to reply.
  2. 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:

    def start(update, context):
      if update.effective_user.id in blacklist:
        pass # or do whatever you want
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search