我希望我的电报机器人等到用户用远程机器人和回调处理程序回答
I want my telegram bot wait until user answers with telebot and callback handler
我正在用 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
的回答 。
我正在用 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
的回答