2358
This commit is contained in:
1 parent
92ecf6bba0
commit
ed14361e7b
5 files changed
+86
-6
No files matched your search
@@ -70,6 +70,9 @@ export async function startPolling(): Promise<void> {
|
||||
const msg = update.message as {
|
||||
chat?: { id: number };
|
||||
photo?: { file_id: string }[];
|
||||
document?: { file_id: string; file_name?: string; mime_type?: string };
|
||||
animation?: { file_id: string; file_name?: string; mime_type?: string };
|
||||
video?: { file_id: string; file_name?: string };
|
||||
text?: string;
|
||||
caption?: string;
|
||||
reply_to_message?: { text?: string };
|
||||
@@ -78,11 +81,21 @@ export async function startPolling(): Promise<void> {
|
||||
const chatId = msg.chat.id;
|
||||
if (!allowed(chatId)) continue;
|
||||
|
||||
// 处理图片
|
||||
if (msg.photo && hasSession(chatId)) {
|
||||
await handlePhoto(chatId, msg.photo, msg.caption);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 处理 GIF / 文档 / 视频
|
||||
if ((msg.animation || msg.document || msg.video) && hasSession(chatId)) {
|
||||
const file = msg.animation || msg.document || msg.video;
|
||||
if (file) {
|
||||
await handleDocument(chatId, file.file_id, file.file_name, msg.caption);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!msg.text) continue;
|
||||
const fullText = msg.text.trim();
|
||||
|
||||
@@ -204,6 +217,50 @@ async function handlePhoto(chatId: number, photos: { file_id: string }[], captio
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDocument(chatId: number, fileId: string, fileName?: string, caption?: string) {
|
||||
try {
|
||||
const fs = await import("fs");
|
||||
const path = await import("path");
|
||||
const { getSession } = await import("./sessions");
|
||||
|
||||
const s = getSession(chatId);
|
||||
if (!s.dirPath) {
|
||||
await sendMessage(chatId, "❌ 请先发 /new 或 /edit 开始写作");
|
||||
return;
|
||||
}
|
||||
|
||||
const fileRes = await tg<{ file_path?: string }>("getFile", { file_id: fileId });
|
||||
if (!fileRes.ok || !fileRes.result?.file_path) {
|
||||
await sendMessage(chatId, "❌ 文件获取失败(可能文件太大,Telegram 限制 20MB)");
|
||||
return;
|
||||
}
|
||||
|
||||
const filePath = fileRes.result.file_path;
|
||||
const fileUrl = "https://api.telegram.org/file/bot" + TG_TOKEN + "/" + filePath;
|
||||
const fileBuffer = await fetch(fileUrl).then((r) => r.arrayBuffer());
|
||||
|
||||
const articleDir = s.dirPath;
|
||||
if (!fs.existsSync(articleDir)) fs.mkdirSync(articleDir, { recursive: true });
|
||||
|
||||
const ext = fileName?.split(".").pop() || filePath.split(".").pop() || "gif";
|
||||
const filename = Date.now() + "-" + fileId.slice(0, 8) + "." + ext;
|
||||
fs.writeFileSync(path.join(articleDir, filename), Buffer.from(fileBuffer));
|
||||
|
||||
// GIF 用图片语法,其他用链接
|
||||
const isImage = ["gif", "webp", "png", "jpg", "jpeg"].includes(ext.toLowerCase());
|
||||
const fileCaption = caption || fileName || "file";
|
||||
if (isImage) {
|
||||
appendContent(chatId, `\n\n`);
|
||||
} else {
|
||||
appendContent(chatId, `\n[${fileCaption}](${filename})\n`);
|
||||
}
|
||||
await sendMessage(chatId, `📎 文件已保存:${filename}`);
|
||||
} catch (e) {
|
||||
console.error("[bot:document]", e instanceof Error ? e.message : String(e));
|
||||
await sendMessage(chatId, "❌ 文件保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCommand(chatId: number, cmd: string, arg?: string) {
|
||||
try {
|
||||
if (cmd === "noop") return;
|
||||
|
||||
Reference in new issue
Block a user