我在处理传入消息时遇到问题。我的机器人中有一个带有InlineKeyboardMarkup的主菜单。当按下按钮时,机器人应等待用户输入,然后发送一条消息。但是在我的情况下,它发送消息,然后等待用户输入,然后再次发送消息。这是一个大问题,因为在消息发送后,它应该返回主菜单。
我正在使用pythonTelegramBotAPI(telebot)
那是代码:
@bot.callback_query_handler(func = lambda call: True)
def query_handler(call):
bot.answer_callback_query(callback_query_id = call.id, text = '')
cid = call.message.chat.id
mid = call.message.message_id
msg = call.message
if call.data == 'request':
bot.edit_message_text('Test - Answer to request', cid, mid, reply_markup = markup)
request_handler(msg)
################# Keyboard sections #################
@bot.message_handler()
def request_handler(msg):
if msg.content_type == 'text':
bot.send_message(msg.chat.id, 'Request accepted')
# and here the code to go back, that I didn't do yet
答案 0 :(得分:0)
也许conversational bot example可以帮助您了解如何与用户建立对话框。
答案 1 :(得分:0)
装饰器@bot.messge_handler()
用于告诉函数它必须处理传入的消息。因此,当有新消息时,该函数将自动在其中执行代码。这意味着该功能一定不能手动调用,这不是必需的。实际上,调用该函数将在其中运行代码,而不是对其进行初始化并等待输入。
这里有个提示:如果用户发送一条消息,该函数将自动被调用。但是,当机器人发送消息时会发生什么?该函数也被称为,因为它与谁发送消息没有区别。因此,为避免也将此功能用于bot消息,只需将if放入这样:
if msg.from_user.is_bot == False:
# Here put what the function should do
如您所见,如果检查消息是否来自机器人。