使用 python-telegram-bot ,我有一个运行的机器人,其设置与 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())
问题是,即使我能够向用户发送消息,上面的代码也不允许用户与机器人交互(我的意思是,一旦用户按下按钮,机器人就不会'执行任何回调)。这是因为我需要 CallbackQueryHandler或 ConversationHandler在机器人中。
但是对于 ConversationHandler
我似乎没有找到任何合适的入口点,因为它是从并行进程发送的消息。另一方面,在主机器人中使用以下 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
,因此它会捕获选定的按钮
关于telegram-bot - 用于从机器人类发送的消息的 CallbackQueryHandler 或 ConversationHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66998849/
相关文章:
google-apps-script - 在多个工作表中搜索一个词
javascript - Css 过渡不适用于 React 中的条件渲染
c# - 尽管有效的正则表达式和激活的解析在 gitlab 中没有覆盖率报告
typescript - 类型错误 : Property 'currentTime' does no
azure - Get-AzRoleAssignment 在 Azure Runbook 中引发 M
angular - 如何在 Angular 中将 patchValue 与 FormArray 一起
java - 未找到 native micronaut 数据插入查询的可能实现