This commit is contained in:
Vaica committed 2026-06-22 17:21:57 +08:00
1 parent 0cc176e867
commit 60354b982e
3 files changed
+32 -5

No files matched your search

+12 -4
View File
@@ -100,9 +100,10 @@ export async function startPolling(): Promise<void> {
// 处理 GIF / 文档 / 视频
if ((msg.animation || msg.document || msg.video) && hasSession(chatId)) {
const fileType = msg.animation ? "animation" : msg.video ? "video" : "document";
const file = msg.animation || msg.document || msg.video;
if (file) {
await handleDocument(chatId, file.file_id, file.file_name, msg.caption);
await handleDocument(chatId, file.file_id, file.file_name, msg.caption, fileType);
}
continue;
}
@@ -228,7 +229,7 @@ async function handlePhoto(chatId: number, photos: { file_id: string }[], captio
}
}
async function handleDocument(chatId: number, fileId: string, fileName?: string, caption?: string) {
async function handleDocument(chatId: number, fileId: string, fileName?: string, caption?: string, fileType?: string) {
try {
const fs = await import("fs");
const path = await import("path");
@@ -257,14 +258,21 @@ async function handleDocument(chatId: number, fileId: string, fileName?: string,
const filename = Date.now() + "-" + fileId.slice(0, 8) + "." + ext;
fs.writeFileSync(path.join(articleDir, filename), Buffer.from(fileBuffer));
// GIF 用图片语法,视频用 shortcode,其他用链接
// 根据 Telegram 消息类型判断,而不是猜文件名
const extLower = ext.toLowerCase();
const isImage = ["gif", "webp", "png", "jpg", "jpeg"].includes(extLower);
const isVideo = ["mp4", "webm", "mov", "avi", "mkv", "flv"].includes(extLower);
const fileCaption = caption || fileName || "file";
if (isImage) {
appendContent(chatId, `\n![${fileCaption}](${filename})\n`);
} else if (isVideo) {
} else if (fileType === "animation") {
// Telegram GIF 动画 → 自动播放、循环、静音
appendContent(chatId, `\n{{< video src="${filename}" loop="true" muted="true" >}}\n`);
await sendMessage(chatId, `🎞️ GIF 已保存:${filename}`);
return;
} else if (fileType === "video" || isVideo) {
// 普通视频 → 带控制栏
appendContent(chatId, `\n{{< video src="${filename}" caption="${fileCaption}" >}}\n`);
await sendMessage(chatId, `🎬 视频已保存:${filename}`);
return;