Python 3.x 我希望我的电报机器人等待,直到用户回答与telebot和回调处理程序
我正在用电传机器人写电报机器人。我有以下代码:Python 3.x 我希望我的电报机器人等待,直到用户回答与telebot和回调处理程序,python-3.x,telegram,telegram-bot,Python 3.x,Telegram,Telegram Bot,我正在用电传机器人写电报机器人。我有以下代码: @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']
@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')
键盘生成键盘。在启动process2之前,我需要process1让用户在进程中选择正确的选项。有什么办法吗?我的代码立即启动process2,但我必须确保用户选择了正确的选项。不建议使用这种处理电报机器人的方法。因为每个更新都是一个单独的请求 您需要使用数据库来存储用户的状态,并根据该状态进行回复 但在这里,您可以将
process2
移动到callback\u 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()
执行的操作,则解决方案可能会有所不同
检查我关于在数据库中存储用户状态的回答