Conversation Handler does not transition to the next state

24 Views Asked by At

I'm trying to make a flight price finder in Telegram and how im doing it is first when I call /start, it will give me inline buttons which includes a flight button. When I click on the flight button, I want the bot to send me a message asking for the flight type and with it inline buttons containing "one-way" and "two-way". Then when I select either, it will send me a message to ask for my input for the departure date. Then I type the departure date and it will store that information in context_data. It's supposed to change state when it asks for the flight type and change again when asking for the dates but it refuses to work.

async def flights(update: Update, context: ContextTypes.DEFAULT_TYPE):
    trip_type_keyboard = [
        [
            InlineKeyboardButton("One-way", callback_data='oneway'),
            InlineKeyboardButton("Round-trip", callback_data='roundtrip')
        ]
    ]
    
    reply_markup = InlineKeyboardMarkup(trip_type_keyboard)
    
    await context.bot.send_message(chat_id=update.callback_query.message.chat_id, text='Please select the type of trip:', reply_markup=reply_markup)
    
    return PICKER

async def reply_with_message(update: Update, context: ContextTypes.DEFAULT_TYPE, message_func):
    message = await message_func()
    if update.message:
        await update.message.reply_text(message)
    else:
        query = update.callback_query
        await query.message.reply_text(message)

async def get_flights_one_way(update: Update, context: ContextTypes.DEFAULT_TYPE):
    print("\nEntered get_flights_one_way")
    date = await update.message.text
    context.user_data['departure_date'] = date
    print(f'Departure date: {date}')
    return FLIGHT

command_mapping = {
    'start': start,
    'help': help,
    'exchangerate': exchange_rate,
    'dailygain': daily_gain,
    'flights': flights,
    'oneway': one_way,
    'roundtrip': get_flights_two_way,
    'cancel': cancel
}

async def button(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.callback_query
    if query.data in command_mapping:
        await command_mapping[query.data](update, context)
    else:
        await query.answer("Unknown command...")
    await query.answer()

def main():
    print(f'{bot_username} is starting...')
    app = ApplicationBuilder().token(my_bot_token).build()

    command_handlers = [
        CommandHandler('start', start),
        CommandHandler('help', help),
        CommandHandler('exchangerate', exchange_rate),
        CommandHandler('dailygain', daily_gain),
        CommandHandler('flights', flights),
        CommandHandler('oneway', one_way),
        CommandHandler('roundtrip', get_flights_two_way),
        CommandHandler('cancel', cancel)
    ]

    # Add conversation handler
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            NORMAL: [
                CallbackQueryHandler(button),
                *command_handlers,
                MessageHandler(filters.TEXT, handle_message)],
            PICKER: [
                *command_handlers,
                MessageHandler(filters.TEXT, one_way)],
            ONEWAY: [
                *command_handlers,
                MessageHandler(filters.TEXT, get_flights_one_way)],
            DEPARTURE: [
                *command_handlers,
                MessageHandler(filters.TEXT, get_flights_two_way)],
            RETURN: [
                *command_handlers,
                MessageHandler(filters.TEXT, get_flights_two_way)],
            FLIGHT: [
                *command_handlers,
                MessageHandler(filters.TEXT, find_flights)]
        },
        fallbacks=[CommandHandler('cancel', cancel)]
    )
    
    # Add error handler
    app.add_error_handler(error)
    print('\nError handlers loaded...')
    
    # Add conversation handler
    app.add_handler(conv_handler)
    print('\nConversation handlers loaded...')

Full code is in: https://pastebin.com/Ndtcd3Lg

I tried several ways to catch the error but there was no error, it just refuses to change state. I am also having some trouble making the bot send a message, I'm not sure whether to use update.message.reply_text or await update.query_callback.reply_text.

0

There are 0 best solutions below