Use passive QQ replies and preserve reminders while waiting for a reply window

This commit is contained in:
itxiaohui committed 2026-10-05 19:37:29 +08:00
1 parent cf10c43e61
commit 58d6807ed0
9 files changed
+495 -35

No files matched your search

+62 -6
View File
@@ -1,10 +1,61 @@
"""Deliver through the current adapter; never persist a live event or old msg_id."""
"""Deliver through the current adapter using a validated incoming reply context."""
import time
from datetime import datetime
from html import escape
OFFICIAL = {"qq_official", "qq_official_webhook"}
def incoming_reply(event):
"""Read an inbound message ID and its original timestamp, never a sent ID."""
raw = event.message_obj.raw_message
data = field(raw, "raw_data", raw)
msg_id = str(
field(raw, "id") or field(data, "id") or field(event.message_obj, "message_id", "") or ""
)
if not msg_id:
return None
timestamp = (
field(raw, "timestamp") or field(data, "timestamp") or field(event.message_obj, "timestamp")
)
try:
received_at = float(timestamp)
except (TypeError, ValueError):
try:
parsed = datetime.fromisoformat(str(timestamp).replace("Z", "+00:00"))
if parsed.tzinfo is None:
return None
received_at = parsed.timestamp()
except (TypeError, ValueError):
return None
return {"msg_id": msg_id, "received_at": min(received_at, time.time())}
def reply_unavailable(exc):
text = str(exc).lower()
return any(
token in text
for token in (
"22009",
"msg limit exceed",
"msg_id",
"msg id",
"msg_seq",
"message expired",
"message is expired",
"消息过期",
"消息已过期",
"被动消息",
"回复超",
"超频",
"无权限",
"permission",
"主动消息失败",
)
)
def field(value, name, default=None):
return value.get(name, default) if isinstance(value, dict) else getattr(value, name, default)
@@ -46,26 +97,31 @@ def reminder_text(rem):
return f"{prefix}⏰ {rem['content']}\n(提醒 #{rem['id']})"
async def deliver(platform, rem):
async def deliver(platform, rem, reply=None):
"""Official SDK return value must contain a message ID to count as sent."""
client = platform.get_client()
text = reminder_text(rem)
if rem["platform_name"] in OFFICIAL:
api = client.api
scene = rem["scene"]
kwargs = dict(reply or {})
if scene not in {"group", "c2c"}:
kwargs.pop("msg_seq", None)
if scene == "group":
mention = f'<qqbot-at-user id="{escape(rem["target_id"], quote=True)}" />'
result = await api.post_group_message(
group_openid=rem["destination"], msg_type=0, content=f"{mention}\n{text}"
group_openid=rem["destination"], msg_type=0, content=f"{mention}\n{text}", **kwargs
)
elif scene == "c2c":
result = await api.post_c2c_message(openid=rem["destination"], msg_type=0, content=text)
result = await api.post_c2c_message(
openid=rem["destination"], msg_type=0, content=text, **kwargs
)
elif scene == "channel":
result = await api.post_message(
channel_id=rem["destination"], content=f"<@{rem['target_id']}>\n{text}"
channel_id=rem["destination"], content=f"<@{rem['target_id']}>\n{text}", **kwargs
)
elif scene == "guild_dm":
result = await api.post_dms(guild_id=rem["destination"], content=text)
result = await api.post_dms(guild_id=rem["destination"], content=text, **kwargs)
else:
raise ValueError(f"未知官方 QQ 投递场景:{scene}")
if not field(result, "id"):
+100 -4
View File
@@ -64,7 +64,18 @@ class ReminderStore:
nickname TEXT NOT NULL DEFAULT '', seen_at REAL NOT NULL,
PRIMARY KEY(scope, user_id)
);
CREATE TABLE IF NOT EXISTS reply_contexts (
platform_id TEXT NOT NULL, scene TEXT NOT NULL, destination TEXT NOT NULL,
msg_id TEXT NOT NULL, received_at REAL NOT NULL,
used INTEGER NOT NULL DEFAULT 0, blocked INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(platform_id, scene, destination)
);
""")
columns = {row[1] for row in db.execute("PRAGMA table_info(reminders)")}
if "waiting_reason" not in columns:
db.execute(
"ALTER TABLE reminders ADD COLUMN waiting_reason TEXT NOT NULL DEFAULT ''"
)
await self._run(create)
@@ -153,7 +164,7 @@ class ReminderStore:
async def cancel(self, scope, creator_id, reminder_id=None):
def write(db):
sql = "UPDATE reminders SET status='cancelled', lease_owner='', lease_until=0 WHERE scope=? AND status='active'"
sql = "UPDATE reminders SET status='cancelled', waiting_reason='', lease_owner='', lease_until=0 WHERE scope=? AND status='active'"
params = [scope]
if creator_id is not None:
sql += " AND creator_id=?"
@@ -171,7 +182,7 @@ class ReminderStore:
dict(r)
for r in db.execute(
"SELECT * FROM reminders WHERE status='active' AND remind_at<=? "
"AND retry_at<=? AND lease_until<=? ORDER BY remind_at,id LIMIT 100",
"AND retry_at<=? AND lease_until<=? ORDER BY remind_at,id",
(now, now, now),
)
]
@@ -195,7 +206,7 @@ class ReminderStore:
lambda db: (
db.execute(
"UPDATE reminders SET status=?,remind_at=COALESCE(?,remind_at),failure_count=0,"
"last_error='',retry_at=0,lease_owner='',lease_until=0 WHERE id=? AND lease_owner=? AND status='active'",
"last_error='',waiting_reason='',retry_at=0,lease_owner='',lease_until=0 WHERE id=? AND lease_owner=? AND status='active'",
("active" if next_time is not None else "done", next_time, reminder_id, owner),
).rowcount
)
@@ -204,7 +215,7 @@ class ReminderStore:
async def failure(self, reminder_id, owner, error, maximum, retry_at):
def write(db):
db.execute(
"UPDATE reminders SET failure_count=failure_count+1,last_error=?,retry_at=?,"
"UPDATE reminders SET failure_count=failure_count+1,last_error=?,retry_at=?,waiting_reason='',"
"status=CASE WHEN failure_count+1>=? THEN 'cancelled' ELSE 'active' END,"
"lease_owner='',lease_until=0 WHERE id=? AND lease_owner=? AND status='active'",
(str(error)[:500], retry_at, maximum, reminder_id, owner),
@@ -221,3 +232,88 @@ class ReminderStore:
).rowcount
)
)
async def remember_reply(self, platform_id, scene, destination, msg_id, received_at):
# Duplicate/replayed events must not renew expiry or refill reply quota.
await self._run(
lambda db: (
db.execute(
"INSERT INTO reply_contexts(platform_id,scene,destination,msg_id,received_at) VALUES (?,?,?,?,?) "
"ON CONFLICT(platform_id,scene,destination) DO UPDATE SET "
"msg_id=excluded.msg_id,received_at=excluded.received_at,used=0,blocked=0 "
"WHERE reply_contexts.msg_id!=excluded.msg_id AND excluded.received_at>=reply_contexts.received_at",
(platform_id, scene, destination, msg_id, received_at),
).rowcount
)
)
async def reserve_reply(self, platform_id, scene, destination, now, window):
def reserve(db):
db.execute("BEGIN IMMEDIATE")
row = db.execute(
"SELECT * FROM reply_contexts WHERE platform_id=? AND scene=? AND destination=?",
(platform_id, scene, destination),
).fetchone()
# Reserve two of QQ's five replies for command responses/other plugins.
if (
not row
or row["blocked"]
or row["used"] >= 3
or not 0 <= now - row["received_at"] < window
):
return None
db.execute(
"UPDATE reply_contexts SET used=used+1 WHERE platform_id=? AND scene=? AND destination=?",
(platform_id, scene, destination),
)
# AstrBot's official event sender uses random sequences in 1..10000.
return {"msg_id": row["msg_id"], "msg_seq": 20001 + row["used"]}
return await self._run(reserve)
async def block_reply(self, platform_id, scene, destination, msg_id):
await self._run(
lambda db: (
db.execute(
"UPDATE reply_contexts SET blocked=1 WHERE platform_id=? AND scene=? AND destination=? AND msg_id=?",
(platform_id, scene, destination, msg_id),
).rowcount
)
)
async def wait_for_reply(self, reminder_id, owner, reason):
await self._run(
lambda db: (
db.execute(
"UPDATE reminders SET waiting_reason=?,retry_at=0,lease_owner='',lease_until=0 "
"WHERE id=? AND lease_owner=? AND status='active'",
(reason[:500], reminder_id, owner),
).rowcount
)
)
async def resume(self, scope, creator_id, reminder_id, limit):
def write(db):
db.execute("BEGIN IMMEDIATE")
row = db.execute(
"SELECT * FROM reminders WHERE scope=? AND id=?", (scope, reminder_id)
).fetchone()
if not row or (creator_id is not None and row["creator_id"] != creator_id):
raise ValueError("当前会话中找不到该提醒,或你没有操作权限。")
if row["status"] == "done" or (row["status"] == "cancelled" and not row["last_error"]):
raise ValueError("只能重试投递失败或正在等待的提醒。")
if row["lease_until"] > time.time():
raise ValueError("该提醒正在投递,请稍后重试。")
count = db.execute(
"SELECT COUNT(*) FROM reminders WHERE scope=? AND creator_id=? AND status='active' AND id!=?",
(scope, row["creator_id"], reminder_id),
).fetchone()[0]
if count >= limit:
raise ValueError(f"最多只能设置 {limit} 个活跃提醒,请先取消一些。")
db.execute(
"UPDATE reminders SET status='active',failure_count=0,last_error='',waiting_reason='',"
"retry_at=0,lease_owner='',lease_until=0 WHERE id=?",
(reminder_id,),
)
await self._run(write)