ConversationHandler not moving to the next state

Viewed 13

I have the following ConversationHandler

conv_handler = ConversationHandler(
        entry_points=[CommandHandler('entry' entry)],
        states={
            ONE: [MessageHandler(filters.TEXT, one)],
            TWO: [MessageHandler(filters.TEXT, two)],
            THREE: [CallbackQueryHandler(three)],
            FOUR: [CallbackQueryHandler(four)]
        },
        fallbacks=[CommandHandler(Commands.CANCEL.value, cancel)],
    )

I defined the states keys as follows: ONE, TWO, THREE, FOUR = range(4)

Entry function:

async def entry(update: Update, context: ContextTypes) -> int:
    await update.message.reply_text("bla")
    return ONE

ONE function:

async def one(update: Update, context: ContextTypes) -> int:
    x = update.message.text
    await update.message.reply_text("yes. bla.")
    context.user_data["x"] = x
    return TWO

TWO function:

async def two(update: Update, context: ContextTypes) -> int:
    await update.message.reply_text("bla")
    keyboard = [
        ['7', '8', '9'],
        ['4', '5', '6'],
        ['1', '2', '3'],
        ['0']
    ]

    await update.message.reply_text("bla", reply_markup=InlineKeyboardMarkup(keyboard))
    query = update.callback_query
    await query.answer()
    context.user_data["x"] = query.data
    return THREE

The transition between the entry function to ONE works, but the next transition between ONE and TWO doesn't.

While debugging, when I am about to return ONE, the object self.map_to_parent is None so I suppose there was somewhat of a parsing error (?) in the ConversationHandler but I can't find it.

Additional info:

  • No errors displayed
  • python-telegram-bot = "==20.0a1"
1 Answers

With your code snippets, I can get from state ONE to state TWO just fine, but the function two is bugged.

Firstly, you should see an error message AttributeError: 'str' object has no attribute 'to_dict', where the traceback points you to the line

await update.message.reply_text("In two", reply_markup=InlineKeyboardMarkup(keyboard))

This is due to InlineKeyboardMarkup expecting a list of list of InlineKyeboardButton objects, not strings. Moreover, in two you're trying to call update.callback_query.answer(), but as two is a callback of a MessageHandler, update.callback_query will always be None.


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

Related