fix: restore clear helper and unify notes/skip expiry handling

This commit is contained in:
Xiaolan Bot
2026-02-22 11:44:13 +08:00
parent 210af75e2c
commit 5eebf4bf66

View File

@@ -289,6 +289,11 @@ async def get_subs_list_keyboard(user_id: int, category_filter: str = None) -> I
return InlineKeyboardMarkup(keyboard) 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): def _get_new_sub_data_or_end(update: Update, context: CallbackContext):
sub_data = context.user_data.get('new_sub_data') sub_data = context.user_data.get('new_sub_data')
if sub_data is None: 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): async def add_notes_received(update: Update, context: CallbackContext):
sub_data = context.user_data.get('new_sub_data') sub_data, err_msg_obj = _get_new_sub_data_or_end(update, context)
if not sub_data: if sub_data is None:
await update.message.reply_text("发生错误,请重试。") _clear_action_state(context, ['new_sub_data'])
if err_msg_obj:
await err_msg_obj.reply_text("会话已过期,请重新使用 /add_sub 开始。")
return ConversationHandler.END return ConversationHandler.END
note = update.message.text.strip() note = update.message.text.strip()
if len(note) > MAX_NOTES_LEN: if len(note) > MAX_NOTES_LEN:
await update.message.reply_text(f"备注过长,请控制在 {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): async def skip_notes(update: Update, context: CallbackContext):
sub_data = context.user_data.get('new_sub_data') sub_data, err_msg_obj = _get_new_sub_data_or_end(update, context)
if not sub_data: if sub_data is None:
await update.message.reply_text("发生错误,请重试。") _clear_action_state(context, ['new_sub_data'])
if err_msg_obj:
await err_msg_obj.reply_text("会话已过期,请重新使用 /add_sub 开始。")
return ConversationHandler.END return ConversationHandler.END
sub_data['notes'] = None sub_data['notes'] = None
save_subscription(update.effective_user.id, sub_data) save_subscription(update.effective_user.id, sub_data)
await update.message.reply_text(text=f"✅ 订阅 '{escape_markdown(sub_data.get('name', ''), version=2)}' 已添加!", await update.message.reply_text(text=f"✅ 订阅 '{escape_markdown(sub_data.get('name', ''), version=2)}' 已添加!",