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.
CodePudding user response:
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
CodePudding user response:
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