From 917a21416184d8b2702aef7d5d8f22c49070a684 Mon Sep 17 00:00:00 2001 From: Orion Date: Fri, 8 May 2026 23:14:29 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=F0=9F=90=9B=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E6=B6=88=E6=81=AF=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构管理员回复处理机制,引入 withReplyTarget 函数用于统一处理回复目标 ID 的校验与构建。新增 deliverAdminMessageToUser 异步函数,封装了将管理员回复内容投递给特定用户的核心逻辑,提高了代码的可读性和可维护性。 主要变更: - 修复了管理员回复逻辑中的状态校验与参数提取 - 优化了对回复目标 ID 的数值解析与有效性检查 - 简化了向目标用户发送文本或媒体消息的调用流程 --- telegram/tg-bot.js | 76 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/telegram/tg-bot.js b/telegram/tg-bot.js index c7e0bf6..6309fcf 100644 --- a/telegram/tg-bot.js +++ b/telegram/tg-bot.js @@ -931,6 +931,71 @@ async function handleBackup(msg, meta, env) { } // --- 6. 管理员功能模块 (双向交互枢纽) --- +const withReplyTarget = (replyToMsgId) => { + const id = parseInt(replyToMsgId); + return Number.isFinite(id) ? { reply_to_message_id: id } : {}; +}; + +async function deliverAdminMessageToUser(msg, uid, replyToMsgId, env) { + const reply = withReplyTarget(replyToMsgId); + const base = { chat_id: uid, ...reply }; + + if (msg.text) { + const body = { ...base, text: msg.text }; + if (msg.entities) body.entities = msg.entities; + return api(env.BOT_TOKEN, "sendMessage", body); + } + + if (msg.photo) { + const body = { ...base, photo: msg.photo[msg.photo.length - 1].file_id }; + if (msg.caption) body.caption = msg.caption; + if (msg.caption_entities) body.caption_entities = msg.caption_entities; + return api(env.BOT_TOKEN, "sendPhoto", body); + } + + const mediaMap = [ + ["animation", "sendAnimation", "animation"], + ["video", "sendVideo", "video"], + ["document", "sendDocument", "document"], + ["audio", "sendAudio", "audio"], + ["voice", "sendVoice", "voice"], + ["video_note", "sendVideoNote", "video_note"], + ["sticker", "sendSticker", "sticker"] + ]; + + for (const [field, method, param] of mediaMap) { + if (!msg[field]) continue; + const body = { ...base, [param]: msg[field].file_id }; + if (msg.caption) body.caption = msg.caption; + if (msg.caption_entities) body.caption_entities = msg.caption_entities; + return api(env.BOT_TOKEN, method, body); + } + + if (msg.location) { + return api(env.BOT_TOKEN, "sendLocation", { + ...base, + latitude: msg.location.latitude, + longitude: msg.location.longitude + }); + } + + if (msg.contact) { + return api(env.BOT_TOKEN, "sendContact", { + ...base, + phone_number: msg.contact.phone_number, + first_name: msg.contact.first_name, + last_name: msg.contact.last_name || "" + }); + } + + return api(env.BOT_TOKEN, "copyMessage", { + chat_id: uid, + from_chat_id: msg.chat.id, + message_id: msg.message_id, + ...reply + }); +} + async function handleAdminReply(msg, env) { if (!msg.message_thread_id || msg.from.is_bot || !(await isAuthAdmin(msg.from.id, env))) return; @@ -968,14 +1033,21 @@ async function handleAdminReply(msg, env) { } try { - 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 }); + const sent = await deliverAdminMessageToUser(msg, uid, replyToMsgId, env); 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) { await api(env.BOT_TOKEN, "sendMessage", { chat_id: msg.chat.id, message_thread_id: msg.message_thread_id, text: "❌ 内部投递失败" }); } + } catch (e) { + console.error("Admin Delivery Failed:", e); + await api(env.BOT_TOKEN, "sendMessage", { + chat_id: msg.chat.id, + message_thread_id: msg.message_thread_id, + text: `❌ 内部投递失败:${e.message || "Unknown error"}` + }); + } } async function handleEdit(msg, env) {