从 bot class 发送的消息的 CallbackQueryHandler 或 ConversationHandler
CallbackQueryHandler or ConversationHandler for a message sent from bot class
使用python-telegram-bot, I have a bot running with very similar settings as for the other examples。另一方面,我有并行进程,允许我定期向与机器人交互的用户发送消息。并行进程使用以下方式与用户通信:
bot = Bot(token=TELEGRAM_TOKEN)
def get_button_options():
keyboard = [[ InlineKeyboardButton("reply", callback_data='reply),]]
reply_markup = InlineKeyboardMarkup(keyboard)
return reply_markup
bot.send_message(chat_id=self.telegram_id, text=text, reply_markup=get_button_options())
问题是,即使我能够向用户发送消息,上面的代码也不允许用户与机器人交互(我的意思是,一旦用户按下按钮, bot 不执行任何回调)。这是因为我需要在机器人中使用 CallbackQueryHandler or ConversationHandler。
但是对于 ConversationHandler
我似乎没有找到任何合适的 entry_point,因为它是从并行进程发送的消息。另一方面,在主机器人中使用以下 CallbackQueryHandler
(取自 the example)似乎没有任何效果:
def button(update: Update, _: CallbackContext) -> None:
query = update.callback_query
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
query.answer()
query.edit_message_text(text=f"Selected option: {query.data}")
在主函数中,我使用 updater.dispatcher.add_handler(CallbackQueryHandler(button))
添加了处理程序
有什么解决办法吗?
正如@CallMeStag所说,解决方案如下:
ConversationHandler(
entry_points=[CallbackQueryHandler(button)])
正确的入口点是 CallbackQueryHandler
,因此它会捕获选定的按钮
使用python-telegram-bot, I have a bot running with very similar settings as for the other examples。另一方面,我有并行进程,允许我定期向与机器人交互的用户发送消息。并行进程使用以下方式与用户通信:
bot = Bot(token=TELEGRAM_TOKEN)
def get_button_options():
keyboard = [[ InlineKeyboardButton("reply", callback_data='reply),]]
reply_markup = InlineKeyboardMarkup(keyboard)
return reply_markup
bot.send_message(chat_id=self.telegram_id, text=text, reply_markup=get_button_options())
问题是,即使我能够向用户发送消息,上面的代码也不允许用户与机器人交互(我的意思是,一旦用户按下按钮, bot 不执行任何回调)。这是因为我需要在机器人中使用 CallbackQueryHandler or ConversationHandler。
但是对于 ConversationHandler
我似乎没有找到任何合适的 entry_point,因为它是从并行进程发送的消息。另一方面,在主机器人中使用以下 CallbackQueryHandler
(取自 the example)似乎没有任何效果:
def button(update: Update, _: CallbackContext) -> None:
query = update.callback_query
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
query.answer()
query.edit_message_text(text=f"Selected option: {query.data}")
在主函数中,我使用 updater.dispatcher.add_handler(CallbackQueryHandler(button))
有什么解决办法吗?
正如@CallMeStag所说,解决方案如下:
ConversationHandler(
entry_points=[CallbackQueryHandler(button)])
正确的入口点是 CallbackQueryHandler
,因此它会捕获选定的按钮