skip to Main Content

Is there anyway that telegram bot can set commands that is available for administrator only? Because I wanted to implement a function that only admins of the telegram bot can use, not for other users.

2

Answers


  1. Use a wrapper.

    def restricted(func):
        @wraps(func)
        def wrapped(update, context, *args, **kwargs):
            if not admin:
                return end_func(update, context, *args, **kwargs)
    
            return func(update, context, *args, **kwargs)
    
        return wrapped
    
    Login or Signup to reply.
  2. With python-telegram-bot there are several ways to do that. A decorator as inyrface suggested can work – see also here and here for a way to cache the admins list. Another option is to user Filters.user. Finally, ptbcontrib/roles has a built-in cached admin check.

    in addition to all of that, you can set up the commands such that in the gui they are only visible to admins – using BotCommandScopeChatAdministrators. This doesn’t prevent other users from manually typing the command, but’s an additional step.


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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search