skip to Main Content

I created a bot with the python-telegram-bot library and the user has the ability to navigate through the states, but when there is a bug, I need to restart the bot and when I restart the bot, the user needs to type /start again in order to communicate with the bot, instead of this, is there a method that I can let the user to continue from whichever state the user was in the bot before?

2

Answers


  1. Chosen as BEST ANSWER

    By adding a MessageHandler to entry_points in addition to CommandHandler, you can check whether the user has been in your program before and you can redirect the registered user without the need to type /start

    async def check_user_state(update: Update, context:ContextTypes):
        if user_exists:
            return MENU_EXE
        elif user_exists_is_not_active:
            return NONEACTIVE
        else:
            return START
    

    Define additional message handlers for specific states

    def main():
        app = Application.builder().token(Token).build()
        conv_handler = ConversationHandler(
            entry_points=[
                MessageHandler(None, check_user_state),
                CommandHandler("start", start_command)
            ],
    

  2. The states of ConversationHandler are stored in memory by default, that’s true. If you want these states to be persisted across reboots of your python script, please have a look at PTBs built-in persistency setup.


    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