问题
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