mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:24:33 +00:00
refactor(telegram): dedupe media download
This commit is contained in:
@@ -309,6 +309,16 @@ export async function resolveMedia(
|
|||||||
stickerMetadata?: StickerMetadata;
|
stickerMetadata?: StickerMetadata;
|
||||||
} | null> {
|
} | null> {
|
||||||
const msg = ctx.message;
|
const msg = ctx.message;
|
||||||
|
const downloadAndSaveTelegramFile = async (filePath: string, fetchImpl: typeof fetch) => {
|
||||||
|
const url = `https://api.telegram.org/file/bot${token}/${filePath}`;
|
||||||
|
const fetched = await fetchRemoteMedia({
|
||||||
|
url,
|
||||||
|
fetchImpl,
|
||||||
|
filePathHint: filePath,
|
||||||
|
});
|
||||||
|
const originalName = fetched.fileName ?? filePath;
|
||||||
|
return saveMediaBuffer(fetched.buffer, fetched.contentType, "inbound", maxBytes, originalName);
|
||||||
|
};
|
||||||
|
|
||||||
// Handle stickers separately - only static stickers (WEBP) are supported
|
// Handle stickers separately - only static stickers (WEBP) are supported
|
||||||
if (msg.sticker) {
|
if (msg.sticker) {
|
||||||
@@ -333,20 +343,7 @@ export async function resolveMedia(
|
|||||||
logVerbose("telegram: fetch not available for sticker download");
|
logVerbose("telegram: fetch not available for sticker download");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`;
|
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
|
||||||
const fetched = await fetchRemoteMedia({
|
|
||||||
url,
|
|
||||||
fetchImpl,
|
|
||||||
filePathHint: file.file_path,
|
|
||||||
});
|
|
||||||
const originalName = fetched.fileName ?? file.file_path;
|
|
||||||
const saved = await saveMediaBuffer(
|
|
||||||
fetched.buffer,
|
|
||||||
fetched.contentType,
|
|
||||||
"inbound",
|
|
||||||
maxBytes,
|
|
||||||
originalName,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check sticker cache for existing description
|
// Check sticker cache for existing description
|
||||||
const cached = sticker.file_unique_id ? getCachedSticker(sticker.file_unique_id) : null;
|
const cached = sticker.file_unique_id ? getCachedSticker(sticker.file_unique_id) : null;
|
||||||
@@ -431,20 +428,7 @@ export async function resolveMedia(
|
|||||||
if (!fetchImpl) {
|
if (!fetchImpl) {
|
||||||
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
||||||
}
|
}
|
||||||
const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`;
|
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
|
||||||
const fetched = await fetchRemoteMedia({
|
|
||||||
url,
|
|
||||||
fetchImpl,
|
|
||||||
filePathHint: file.file_path,
|
|
||||||
});
|
|
||||||
const originalName = fetched.fileName ?? file.file_path;
|
|
||||||
const saved = await saveMediaBuffer(
|
|
||||||
fetched.buffer,
|
|
||||||
fetched.contentType,
|
|
||||||
"inbound",
|
|
||||||
maxBytes,
|
|
||||||
originalName,
|
|
||||||
);
|
|
||||||
let placeholder = "<media:document>";
|
let placeholder = "<media:document>";
|
||||||
if (msg.photo) {
|
if (msg.photo) {
|
||||||
placeholder = "<media:image>";
|
placeholder = "<media:image>";
|
||||||
|
|||||||
Reference in New Issue
Block a user