Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我希望我的电报机器人等待,直到用户回答与telebot和回调处理程序_Python 3.x_Telegram_Telegram Bot - Fatal编程技术网

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()
执行的操作,则解决方案可能会有所不同

检查我关于在数据库中存储用户
状态的回答