ReminderBot/bot/handlers/callback.py
leo 5b329d0afc fix: correct interval reminder scheduling and done action behavior
- Replace IntervalTrigger with OrTrigger of CronTriggers for interval
  reminders, ensuring triggers align to window start each day without drift
- Refactor executor to extract _send_reminder_message helper, removing
  the runtime window check (now handled at scheduling layer)
- Change "done" button to only acknowledge the notification without
  deactivating the reminder

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-05 17:54:33 +08:00

80 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime, timedelta, timezone
import pytz
from telegram import Update
from telegram.ext import ContextTypes
from bot.models.database import Session
from bot.models.reminder import Reminder
from bot.scheduler.job_manager import add_reminder_job, remove_reminder_job
async def handle_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query
await query.answer()
data = query.data
# Support compound actions like "cancel_snooze_123"
if data.startswith("cancel_snooze_"):
action = "cancel_snooze"
reminder_id = int(data[len("cancel_snooze_"):])
else:
action, reminder_id_str = data.split("_", 1)
reminder_id = int(reminder_id_str)
session = Session()
try:
reminder = session.get(Reminder, reminder_id)
if reminder is None:
await query.edit_message_text("提醒不存在或已被删除。")
return
if action == "done":
# Just acknowledge, don't stop the reminder
await query.edit_message_text(f"✅ 已确认提醒:{reminder.title}")
elif action == "snooze":
# Ask user for snooze minutes
context.user_data["snooze_reminder_id"] = reminder_id
context.user_data["snooze_chat_id"] = query.message.chat_id
await query.edit_message_text(
"⏰ 请输入延期的分钟数例如10"
)
elif action == "cancel_snooze":
job_name = f"snooze_{reminder_id}"
jobs = context.job_queue.get_jobs_by_name(job_name)
if jobs:
for job in jobs:
job.schedule_removal()
await query.edit_message_text(f"❌ 已关闭延期提醒:{reminder.title}")
else:
await query.edit_message_text("延期提醒不存在或已过期。")
elif action == "pause":
reminder.is_active = False
session.commit()
remove_reminder_job(reminder_id)
await query.edit_message_text(f"⏸ 已暂停提醒:{reminder.title}")
elif action == "resume":
reminder.is_active = True
session.commit()
add_reminder_job(reminder_id)
await query.edit_message_text(f"▶️ 已恢复提醒:{reminder.title}")
elif action == "delete":
title = reminder.title
session.delete(reminder)
session.commit()
remove_reminder_job(reminder_id)
await query.edit_message_text(f"🗑 已删除提醒:{title}")
except Exception:
session.rollback()
await query.edit_message_text("操作失败,请稍后重试。")
finally:
Session.remove()