问题描述
使用 python-telegram-bot,我有使用与 其他示例.另一方面,我有并行进程,允许我定期向与机器人交互的用户发送消息.并行进程使用以下方式与用户通信:
Using python-telegram-bot, I have a bot running with very similar settings as for the other examples. On the other side i have parallel processes that allow me to send messages to users interacting with the bot periodically. The parallel proccess communicates with the users using:
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 在机器人.
The thing is, even if I'm capable to send messages to the user, the above code does not allow the user to interact with the bot (By that I mean that, once the user presses the button, the bot doesn't execute any callback). This is because I need either a CallbackQueryHandler or ConversationHandler within the bot.
但是对于 ConversationHandler
我似乎没有找到任何合适的入口点,因为它是从并行进程发送的消息.另一方面,使用以下 CallbackQueryHandler
(取自 示例) 在主机器人中似乎没有任何效果:
However for the ConversationHandler
I don't seem to find any appropiate entry_point, since it is a message sent from a parallel process. On the other hand, using the following CallbackQueryHandler
(taken from the example) in the main bot doesn't seem to have any effect:
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))
关于如何解决这个问题的任何想法?
Any ideas on how to solve that?
推荐答案
如@CallMeStag所说,解决方案如下:
As said by @CallMeStag, the solution is the following:
ConversationHandler(
entry_points=[CallbackQueryHandler(button)])
正确的入口点是一个CallbackQueryHandler
,然后它会捕获选定的按钮
the correct entry point is a CallbackQueryHandler
, so then it captures the selected button
这篇关于用于从机器人类发送的消息的 CallbackQueryHandler 或 ConversationHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!