Telegram Bot在查询中使用按钮



我正在Python上创建我的第一个Telegram Bot。目前,我设法获得了一个内联菜单,但我不明白如何从最后一个按钮(也就是按下的按钮上的文本(中获得信息,以便在谷歌文档的查询中使用它。例如,如果他们选中按钮3,我想以某种方式访问文本"按钮3"并在查询中使用它(data[data['type']=='Button 3'](。以下是帮助理解的代码:

########################## Open connection with Google Sheets #############################
client = gspread.authorize(creds)
sheet = client.open('YodissiDay').sheet1
############################### Bot ############################################

def start(bot, update):
bot.message.reply_text(main_menu_message(), reply_markup=main_menu_keyboard())
def main_menu(bot, update):
bot.callback_query.message.edit_text(main_menu_message(), reply_markup=main_menu_keyboard())
def first_menu(bot, update):
bot.callback_query.message.edit_text(first_menu_message(),
reply_markup=first_menu_keyboard())

def second_menu(bot, update):
bot.callback_query.message.edit_text(second_menu_message(),
reply_markup=second_menu_keyboard())

def third_menu(bot, update):
bot.callback_query.message.edit_text(second_menu_message(),
reply_markup=second_menu_keyboard())
def first_submenu(bot, update):
bot.callback_query.message.edit_text(first_submenu_message(),
reply_markup=first_submenu_keyboard(bot, update))

def second_submenu(bot, update):
pass
def error(update, context):
print(f'Update {update} caused error {context.error}')
############################ Keyboards #########################################
def main_menu_keyboard():
keyboard = [[InlineKeyboardButton('First Choice', callback_data='m1')],
[InlineKeyboardButton('Second Choice', callback_data='m2')],
[InlineKeyboardButton('Third Choice', callback_data='m3')]]
return InlineKeyboardMarkup(keyboard)
def first_menu_keyboard():
keyboard = [[InlineKeyboardButton('Beauty', callback_data='Beauty')],
[InlineKeyboardButton('One', callback_data='One')],
[InlineKeyboardButton('Two', callback_data='Two')],
[InlineKeyboardButton('Three', callback_data='Three')],
[InlineKeyboardButton('Main menu', callback_data='main')]]    
return InlineKeyboardMarkup(keyboard)
def second_menu_keyboard():
pass
def first_submenu_keyboard(bot, update):      
data = gspread_dataframe.get_as_dataframe(sheet)
data = data.astype({'Taken' : 'float64'})
data = data[data['Taken']==0]
gift_type = update.callback_query.data       
gift_ideas = data[data['Type']==gift_type]    
keyboard = []
for row in range(0,gift_ideas.shape[0]):
keyboard[row]=InlineKeyboardButton(text=gift_ideas['Name'][row],       url=gift_ideas['Link'][row])
keyboard[gift_ideas.shape[0]+1]=InlineKeyboardButton('Main menu',    callback_data='main')
return InlineKeyboardMarkup(keyboard)

############################# Messages #########################################

def main_menu_message():
return 'Hello 1:'
def first_menu_message():
return 'Hello 1_1:'
def second_menu_message():
return 'Hello 1_2:'
def third_menu_message():
return 'Hello 1_3:'
def first_submenu_message():
return 'Hello 1_1_1:'
############################# Handlers #########################################
# 
Create bot object:
bot = telegram.Bot(token=BOT_KEY)
updater = Updater(BOT_KEY, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_menu, pattern='m1'))
updater.dispatcher.add_handler(CallbackQueryHandler(second_menu, pattern='m2'))
updater.dispatcher.add_handler(CallbackQueryHandler(third_menu, pattern='m3'))
updater.dispatcher.add_handler(CallbackQueryHandler(first_submenu, pattern='Beauty'))
updater.dispatcher.add_error_handler(error)    

# Start polling:
updater.start_polling()
updater.idle()```
All other recommendations/advices are also welcome! Keep in mind, that I try it step by step and for now I want to see how this 'Beauty' button would work! Other buttons lead to nowhere for now

请参阅通过单击动态内联按钮创建处理程序来访问单击按钮的文本。

另外两条备注:

  • use_context=True传递给Updater,但使用旧式回调签名(def callback(bot, update):而不是def callback(update, context):(。有关详细信息,请参阅v12过渡指南
  • 四个回调(main_menufirst_menu(的名称表示您正在构建某种交互式菜单。如果你为你的机器人添加更多的功能,这可能会变得很难在这种形式下维护。我建议你看看ConversationHandler和conversationbot2.py示例,看看这对你的用例是否有帮助

免责声明:我目前是python-telegram-bot的维护人员

最新更新