1

I am unable to get the query.data second time when I click on inlinekeybord. In my code when the bot start it gives some choice to select. when one option is selected the bot send another options list respect to user selected option. Again when user select 2nd time from the list bot should sent another list of option. But I can not figureout how to get the query data second time.

Here is my code:

def build_menu(buttons,
               n_cols
               ):
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    return menu


def district(id):
    url = f"https://cdn-api.co-vin.in/api/v2/admin/location/districts/{id}"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"}
    response = requests.request("GET", url, headers=headers)
    data = response.json()
    for i in data['districts']:
        district = []
        district.append(i['district_id'])
        district.append(i['district_name'])
        districts.append(district)
    return districts


def state():

    url = 'https://cdn-api.co-vin.in/api/v2/admin/location/states'
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0"}
    
    response = requests.request("GET", url, headers=headers)
    data = response.json() 
    for i in data['states']:
        state = []
        state.append(i['state_id'])
        state.append(i['state_name'])
        states.append(state)
    return states
def get_list_inline(update, contex):
    state()
    button_list = []
    for i in states:
        button_list.append(InlineKeyboardButton(
            f'{i[1]}', callback_data=i[0]))
    reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=1))
    bot.send_message(chat_id=update.message.chat_id,
                     text='Choose from the following', reply_markup=reply_markup)


def button1(update: Update, context: CallbackContext):
    """Parses the CallbackQuery and updates the message text."""
    query = update.callback_query
    query.answer()
    state_id = query.data
    district(state_id)

    button_list = []
    for i in districts:
        button_list.append(InlineKeyboardButton(
            f'{i[1]}', callback_data=i[0]))
    reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=1))

    query.edit_message_text(text='Choose from the following',
                             reply_markup=reply_markup)
def button2(update: Update, context: CallbackContext):
    """Parses the CallbackQuery and updates the message text."""
    query = update.callback_query
    query.answer()
    district_id = query.data
    print(district_id)


def start_command(update, context):
    update.message.reply_text(f'Hello {update.message.chat.username} ')
    update.message.reply_text('Sent your pin to get available vaccine details')
    get_list_inline(update, context)
def main():
    PORT = int(os.environ.get('PORT', '443'))

    updater = Updater(token, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("start", start_command))
    dp.add_handler(CommandHandler("help", help_command))
    dp.add_handler(CallbackQueryHandler(button1))
    dp.add_handler(CallbackQueryHandler(button2))
    dp.add_handler(MessageHandler(Filters.text, handel_command))
    dp.add_error_handler(error)
    updater.start_polling()
    updater.idle()


main()

When I clicked the inline keybord second time i get this error enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Swagoto
  • 31
  • 4

1 Answers1

0

Looking at

    dp.add_handler(CallbackQueryHandler(button1))
    dp.add_handler(CallbackQueryHandler(button2))

one can notice that the second handler will never run because of the "order and priority counts" logic of add_handler - see the docs for detailed info.

You could either use the pattern argument of CallbackQueryHandler to make the handlers only catch CallbackQueries with specified callback_data and/or use a ConversationHandler. The latter makes sense because you have some sort of step-by-step setup. Please have a look at the conversationbot2.py example, which showcases both.


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

CallMeStag
  • 5,467
  • 1
  • 7
  • 22