fix(core): 🐛 支持回复消息时的 Topic ID 关联

通过查询数据库中的消息记录,获取回复消息对应的 topic_message_id。这确保了在 Telegram 话题(Topics)中回复消息时,机器人能够正确关联并保持在相同的话题线内。

主要变更:
- 引入 topicReplyToMsgId 变量处理话题关联
- 增加数据库查询逻辑,根据 user_id 和 message_id 检索原始话题消息 ID
- 优化了特殊引用语法的降级渲染逻辑位置
This commit is contained in:
2026-05-04 02:32:18 +08:00
parent 737e83541d
commit e749c4633f

View File

@@ -457,6 +457,11 @@ async function relayToTopic(msg, u, env) {
try {
let forwardedMsg;
const rawText = msg.text || "";
let topicReplyToMsgId = undefined;
if (msg.reply_to_message) {
const ref = await sql(env, "SELECT topic_message_id FROM messages WHERE user_id=? AND message_id=?", [uid, msg.reply_to_message.message_id.toString()], 'first');
if (ref?.topic_message_id) topicReplyToMsgId = ref.topic_message_id;
}
// 特殊引用语法降级渲染支持
if (rawText && (rawText.startsWith('>') || rawText.startsWith('》') || rawText.startsWith('>'))) {
@@ -469,11 +474,16 @@ async function relayToTopic(msg, u, env) {
chat_id: env.ADMIN_GROUP_ID,
message_thread_id: tid,
text: customHtml,
parse_mode: "HTML"
parse_mode: "HTML",
reply_to_message_id: topicReplyToMsgId
});
} else {
// 标准转发处理尝试,若触碰受限隐私配置则回退到原生复制
try {
if (topicReplyToMsgId) {
forwardedMsg = await api(env.BOT_TOKEN, "copyMessage", {
chat_id: env.ADMIN_GROUP_ID, from_chat_id: uid, message_id: msg.message_id, message_thread_id: tid, reply_to_message_id: topicReplyToMsgId
});
} else try {
forwardedMsg = await api(env.BOT_TOKEN, "forwardMessage", {
chat_id: env.ADMIN_GROUP_ID, from_chat_id: uid, message_id: msg.message_id, message_thread_id: tid
});
@@ -668,7 +678,12 @@ async function handleAdminReply(msg, env) {
}
try {
await api(env.BOT_TOKEN, "copyMessage", { chat_id: uid, from_chat_id: msg.chat.id, message_id: msg.message_id, reply_to_message_id: replyToMsgId });
const sent = await api(env.BOT_TOKEN, "copyMessage", { chat_id: uid, from_chat_id: msg.chat.id, message_id: msg.message_id, reply_to_message_id: replyToMsgId });
if (sent && sent.message_id) {
const storeText = msg.text || msg.caption || "[Admin Message]";
await sql(env, "INSERT OR REPLACE INTO messages (user_id, message_id, text, date, topic_message_id) VALUES (?,?,?,?,?)",
[uid, sent.message_id.toString(), storeText, msg.date || Math.floor(Date.now() / 1000), msg.message_id.toString()]);
}
// 此处为管理员端给用户下发消息的主逻辑。根据之前的版本,管理员侧发送成功后的回执代码也已经去除
} catch (e) { api(env.BOT_TOKEN, "sendMessage", { chat_id: msg.chat.id, message_thread_id: msg.message_thread_id, text: "❌ 内部投递失败" }); }
}