fix(core): 🐛 优化管理员消息转发逻辑
重构管理员回复处理机制,引入 withReplyTarget 函数用于统一处理回复目标 ID 的校验与构建。新增 deliverAdminMessageToUser 异步函数,封装了将管理员回复内容投递给特定用户的核心逻辑,提高了代码的可读性和可维护性。 主要变更: - 修复了管理员回复逻辑中的状态校验与参数提取 - 优化了对回复目标 ID 的数值解析与有效性检查 - 简化了向目标用户发送文本或媒体消息的调用流程
This commit is contained in:
@@ -931,6 +931,71 @@ async function handleBackup(msg, meta, env) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --- 6. 管理员功能模块 (双向交互枢纽) ---
|
// --- 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) {
|
async function handleAdminReply(msg, env) {
|
||||||
if (!msg.message_thread_id || msg.from.is_bot || !(await isAuthAdmin(msg.from.id, env))) return;
|
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 {
|
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) {
|
if (sent && sent.message_id) {
|
||||||
const storeText = msg.text || msg.caption || "[Admin Message]";
|
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 (?,?,?,?,?)",
|
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()]);
|
[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) {
|
async function handleEdit(msg, env) {
|
||||||
|
|||||||
Reference in New Issue
Block a user