Telegram Bot问题修复请求:/start不重置流程&无法重复提交请求
解决Telegram Bot的流程重置与重复提交问题
问题说明
当前Bot存在两个核心问题:
- 用户在请求提交中途中断,通过
/start重启Bot时,不会从头开始流程,仍继续处理原有未完成的请求 - 用户提交完一次请求后,无法发起新的请求
解决思路
- 会话隔离:用字典存储每个用户的请求数据,替代全局变量,避免用户之间的数据干扰
- 强制重置:在
/start命令中清除用户的未处理步骤处理器,同时重置该用户的会话数据,确保流程从头开始 - 重复提交支持:请求完成后重置用户会话数据,并重新展示请求入口按钮,允许立即发起新请求
修改后的完整代码
import telebot from telebot import types TOKEN = 'TOKENFROMBOTFATHER' bot = telebot.TeleBot(TOKEN) # 目标接收请求的聊天ID user2_chat_id = 'IDFROMCHAT' # 存储每个用户的会话数据,key为chat_id,value为请求字段字典 user_data = {} class Application: @staticmethod def display(message): chat_id = message.chat.id # 初始化当前用户的会话数据 user_data[chat_id] = {} bot.send_message(chat_id, "Your name:") bot.register_next_step_handler(message, process_name_step) def process_name_step(message): chat_id = message.chat.id user_data[chat_id]['name'] = message.text markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2) leftside = types.KeyboardButton('ADRESS1') rightside = types.KeyboardButton('ADRESS2') markup.add(leftside, rightside) bot.send_message(chat_id, "Choose your adress:", reply_markup=markup) bot.register_next_step_handler(message, process_bereg_step) def process_bereg_step(message): chat_id = message.chat.id user_data[chat_id]['bereg'] = message.text markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2) DHC = types.KeyboardButton('SERVICE1') TO = types.KeyboardButton('SERVICE2') comp = types.KeyboardButton('SERVICE3') buy = types.KeyboardButton('SERVICE4') GRM = types.KeyboardButton('SERVICE5') doos = types.KeyboardButton('SERVICE6') inshe = types.KeyboardButton('SERVICE7') markup.add(DHC, TO, comp, buy, GRM, doos, inshe) bot.send_message(chat_id, "Choose your service:", reply_markup=markup) bot.register_next_step_handler(message, process_posluga_step) def process_posluga_step(message): chat_id = message.chat.id user_data[chat_id]['posluga'] = message.text bot.send_message(chat_id, "Your phone number:") bot.register_next_step_handler(message, process_phone_step) def process_phone_step(message): chat_id = message.chat.id user_data[chat_id]['phone'] = message.text bot.send_message(chat_id, "Your tech code:") bot.register_next_step_handler(message, process_text_step) def process_text_step(message): chat_id = message.chat.id user_data[chat_id]['code'] = message.text # 拼接请求内容发送给目标用户 request_content = ( f"Name: {user_data[chat_id]['name']}\n\n" f"Adress: {user_data[chat_id]['bereg']}\n\n" f"Service: {user_data[chat_id]['posluga']}\n\n" f"Phone: {user_data[chat_id]['phone']}\n\n" f"Code: {user_data[chat_id]['code']}" ) bot.send_message(user2_chat_id, request_content) bot.send_message(chat_id, f"Thanks. We'll call you") # 重置当前用户会话数据,允许发起新请求 del user_data[chat_id] # 重新展示请求入口按钮 markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2) itembtn1 = types.KeyboardButton('Make request') markup.add(itembtn1) bot.send_message(chat_id, "Push the button for request", reply_markup=markup) @bot.message_handler(commands=['start']) def send_welcome(message): chat_id = message.chat.id # 清除当前用户的未处理步骤处理器,强制重置流程 bot.clear_step_handler_by_chat_id(chat_id) # 重置用户会话数据 if chat_id in user_data: del user_data[chat_id] photo = open('HelloPicture.jpg', 'rb') bot.send_photo(chat_id, photo) bot.send_message(chat_id, f"HELLO TEXT", parse_mode="HTML") markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2) itembtn1 = types.KeyboardButton('Make request') markup.add(itembtn1) bot.send_message(chat_id, "Push the button for request", reply_markup=markup) @bot.message_handler(func=lambda message: True) def echo_all(message): if message.text == 'Make request': Application.display(message) bot.polling(none_stop=True)
关键修改点
- 新增
user_data字典,为每个用户独立存储请求数据,避免全局变量的冲突问题 /start命令中调用bot.clear_step_handler_by_chat_id()清除用户之前的步骤处理器,确保流程完全重置- 请求完成后删除用户的会话数据,并重新展示"Make request"按钮,支持重复提交
- 所有步骤函数从
user_data中读取和写入当前用户的请求数据,不再依赖全局变量
内容的提问来源于stack exchange,提问作者igorzyden
相关产品推荐
相关产品推荐

