【问题标题】:Telegram Bot Use Button in QueryTelegram Bot 在查询中使用按钮
【发布时间】:2021-05-25 06:57:50
【问题描述】:

我正在 Python 上创建我的第一个 Telegram Bot。现在,我设法获得了一个内联菜单,但我不明白如何从最后一个按钮(也就是被按下的按钮上的文本)获取信息,以便在查询谷歌文档时使用它。例如,如果他们检查 Button 3,我想以某种方式访问​​文本“Button 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

【问题讨论】:

    标签: telegram-bot python-telegram-bot


    【解决方案1】:

    请参阅Creating a handler by clicking on dynamic inline buttons 了解访问单击按钮的文本。

    两个补充说明:

    • 您将use_context=True 传递给您的Updater,但使用旧式回调签名(def callback(bot, update): 而不是def callback(update, context):)。请参阅v12 transition guide for details
    • 四个回调的名称(main_menufirst_menu)暗示您正在构建某种交互式菜单。如果您向您的机器人添加更多功能,这可能会变得难以以这种形式维护。我建议您查看ConversationHandlerconversationbot2.py example,看看这对您的用例是否有帮助。

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

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2019-01-12
      • 2018-02-10
      相关资源
      最近更新 更多