grammyjs / files

Handle and download received media files.
MIT License
9 stars 0 forks source link

File handling simplified in grammY

This plugin allows you to easily download files from Telegram servers. Check out the official plugin page for further documentation.

Example

import { Bot, type Context } from "grammy";
import { type FileFlavor, hydrateFiles } from "@grammyjs/files";

// Transformative API flavor
type MyContext = FileFlavor<Context>;

// Create bot
const bot = new Bot<MyContext>("");

// Install plugin
bot.api.config.use(hydrateFiles(bot.token));

// Download videos and GIFs to temporary files
bot.on([":video", ":animation"], async (ctx) => {
    // Prepare file for download
    const file = await ctx.getFile();
    // Download file to temporary location on your disk
    const path = await file.download();
    // Print file path
    console.log("File saved at", path);
});

You can pass a string with a file path to download if you don't want to create a temporary file. Just do await file.download('/path/to/file').