diff --git a/SubMind.py b/SubMind.py index 6f56eec..ad3cb5b 100644 --- a/SubMind.py +++ b/SubMind.py @@ -289,6 +289,11 @@ async def get_subs_list_keyboard(user_id: int, category_filter: str = None) -> I return InlineKeyboardMarkup(keyboard) +def _clear_action_state(context: CallbackContext, keys: list[str]): + for key in keys: + context.user_data.pop(key, None) + + def _get_new_sub_data_or_end(update: Update, context: CallbackContext): sub_data = context.user_data.get('new_sub_data') if sub_data is None: @@ -772,10 +777,13 @@ async def add_renewal_type_received(update: Update, context: CallbackContext): async def add_notes_received(update: Update, context: CallbackContext): - sub_data = context.user_data.get('new_sub_data') - if not sub_data: - await update.message.reply_text("发生错误,请重试。") + sub_data, err_msg_obj = _get_new_sub_data_or_end(update, context) + if sub_data is None: + _clear_action_state(context, ['new_sub_data']) + if err_msg_obj: + await err_msg_obj.reply_text("会话已过期,请重新使用 /add_sub 开始。") return ConversationHandler.END + note = update.message.text.strip() if len(note) > MAX_NOTES_LEN: await update.message.reply_text(f"备注过长,请控制在 {MAX_NOTES_LEN} 个字符以内。") @@ -789,10 +797,13 @@ async def add_notes_received(update: Update, context: CallbackContext): async def skip_notes(update: Update, context: CallbackContext): - sub_data = context.user_data.get('new_sub_data') - if not sub_data: - await update.message.reply_text("发生错误,请重试。") + sub_data, err_msg_obj = _get_new_sub_data_or_end(update, context) + if sub_data is None: + _clear_action_state(context, ['new_sub_data']) + if err_msg_obj: + await err_msg_obj.reply_text("会话已过期,请重新使用 /add_sub 开始。") return ConversationHandler.END + sub_data['notes'] = None save_subscription(update.effective_user.id, sub_data) await update.message.reply_text(text=f"✅ 订阅 '{escape_markdown(sub_data.get('name', ''), version=2)}' 已添加!",