我希望我的电报机器人等待,直到用户回答电话机器人和回调处理程序



我正在用telebot编写一个电报机器人。我有以下代码:

@bot.message_handler(commands=["play"])
def game(message):
bot.send_message(message.chat.id, 'Start')
process(message)
process2(message)
def process(message):
arr = ['Ans1', 'Ans2', 'Ans3', 'Ans4']
ans = ['1', '2', '3', '4']
keyboard = keyboard_gen(arr, ans)
bot.send_message(message.chat.id, text = 'Question1', reply_markup=keyboard)
def process2(message):
pass
@bot.callback_query_handler(func=lambda call: True) 
def callback_worker(call):
if call.data == 1:
bot.send_message(call.message.chat.id, 'True')
if call.data in [2, 3, 4]:
bot.send_message(call.message.chat.id, 'False')

keyboard_gen生成键盘。在启动process2之前,我需要process1让用户在process中选择正确的选项。有什么办法吗?我的代码立即启动process2,但我必须确保用户选择了正确的选项。

不建议使用这种方式处理电报机器人程序。因为每次更新都是一个单独的请求。

您需要使用数据库来存储用户的状态,并根据该状态进行回复。

但在这里,您可以将process2移动到callback_worker内部,并在if条件之后调用它。

@bot.callback_query_handler(func=lambda call: True) 
def callback_worker(call):
if call.data == 1:
bot.send_message(call.message.chat.id, 'True')
if call.data in [2, 3, 4]:
bot.send_message(call.message.chat.id, 'False')
process2()

此外,您应该从中删除message参数,如果您提到要对process2()执行什么操作,则解决方案可能会有所不同。

在这里查看我关于将用户state存储在数据库中的答案。

相关内容

最新更新