@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

shell
pnpm add @yaebal/files

registration

call bot.install(files()) once. the plugin adds ctx.files to every subsequent handler in the chain.

bot.ts
import { Bot } from "@yaebal/core";
import { files } from "@yaebal/files";

const bot = new Bot(token);
bot.install(files());

api

exportkindsignaturedescription
filesfunction() => Pluginreturns the plugin. install with bot.install(files()).
FilesControlinterfacethe type of ctx.files.

ctx.files

methodsignaturedescription
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

download-photo.ts
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.