How to write dynamic keyboard with conversationhandler in python telegram bot?

不打扰是莪最后的温柔 提交于 2020-01-16 11:59:44

问题


I am trying to write dynamic keyboard (buttons from a table in database ) But I am unable to get response in callbackqueryhandler in conversation handler.

I am unable to go to next state which is response stored from a database column,

My code :

def menu(update,context):
    bot = context.bot

    name = db_execute("SELECT event_name FROM menu ", fetchall=True)
    button_list = []
    for each in name:
        button_list.append(InlineKeyboardButton(each, callback_data = each))
    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)
    return FIRST


def one(update, context):
    query = update.callback_query
    bot = context.bot
    print(query)

    name = db_execute("SELECT event_name FROM menu ", fetchall=True)
    content = db_execute("SELECT event_content FROM menu ", fetchall=True)

    post = dict(zip(name, content))

    for k,v in post.items():
        if query == k:
            bot.edit_message_text(chat_id=query.message.chat_id,
            message_id=query.message.message_id,
            text= "{}".format(v),
            reply_markup=reply_markup
            )
    return SECOND

updater = Updater(token , use_context=True)
create_db()
dp = updater.dispatcher

conv_handler = ConversationHandler(
    entry_points=[CommandHandler('menu', menu)],
    states={
        FIRST: [CallbackQueryHandler(one)],

        SECOND: [CallbackQueryHandler(end)]

    },
   fallbacks=[CommandHandler('menu', menu)]
    )

dp.add_handler(conv_handler)
updater.start_polling() 

来源:https://stackoverflow.com/questions/59732090/how-to-write-dynamic-keyboard-with-conversationhandler-in-python-telegram-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!