skip to Main Content

Telegram allows the commands to be updated using setMyCommands. I can successfully update the commands in realtime based on the user input using a python API, pyTelegramBotAPI.

However, the problem is the user has to exit the chat with the bot and then come back to the chat again to see the new commands (by typing /).

Is there any way that I can have the bot update the list of commands in realtime with the user still in the chat?

2

Answers


  1. So I tried it with telegram client (on linux and on android) and the commands do not change unless the user re-enters the chat. This I think is because the telegram-client only loads the commands when the user enters the chat.

    But I also tried this with telegram web and found that the commands changed immidiately after I changed the command sets from BotFather. The webapp actually did load the command set without me leaving the chat, after sending a single message/command

    So its definitely a problem with the telegram-client.

    Login or Signup to reply.
  2. Actually it is possible to do this real time if you set the scope to chat with specific user. There is parameter scope in set_my_commands function. By default set_my_commands affects all the chats of your bot. However, you can provide telegram.BotCommandScopeChat as a value of scope param. See the code below, it updates the command menu immediately without need to switch chats.

    def _set_menu(commands: List[BotCommand], update: Update):
    ''' sets the chat commands menu '''
    bot = Bot(os.getenv('TELEGRAM_TOKEN'))
    bot.delete_my_commands()
    if update:
        bot.set_my_commands(
            commands=commands,
            scope=telegram.BotCommandScopeChat(
                chat_id=update.effective_chat.id
        ))
    else:
        bot.set_my_commands(commands=commands)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search