@yaebal/files
resolve and download Telegram files. adds ctx.files with two helpers: get a CDN URL for any file_id, or buffer the whole file into a Uint8Array.
install
pnpm add @yaebal/filesregistration
call bot.install(files()) once. the plugin adds ctx.files to every subsequent handler in the chain.
import { Bot } from "@yaebal/core";
import { files } from "@yaebal/files";
const bot = new Bot(token);
bot.install(files());api
| export | kind | signature | description |
|---|---|---|---|
files | function | () => Plugin | returns the plugin. install with bot.install(files()). |
FilesControl | interface | — | the type of ctx.files. |
ctx.files
| method | signature | description |
|---|---|---|
fileLink | (fileId: string) => Promise<string> | calls getFile then returns the full CDN URL. throws if file_path is absent. |
download | (fileId: string) => Promise<Uint8Array> | resolves the URL via fileLink then fetches it into memory. throws on a non-2xx response. |
example
bot.on("message:photo", async (ctx) => {
const photo = ctx.photo[ctx.photo.length - 1];
// get a download URL for the file
const url = await ctx.files.fileLink(photo.file_id);
console.log("url:", url);
// download the whole file into memory
const bytes = await ctx.files.download(photo.file_id);
console.log("size:", bytes.byteLength, "bytes");
});buffers the whole file.
download uses fetch + arrayBuffer() — the entire file lands in memory before you get the bytes. for large files (video, documents) consider streaming from the URL returned by fileLink instead.