File Handling Simplified in grammY (files
)
This plugin allows you to easily download files from Telegram servers, and to obtain a URL so you can download the file yourself.
Remember how files work, and how to upload them.
Downloading Files
You need to pass your bot token to this plugin because it must authenticate as your bot when it downloads files. This plugin then installs the download
method on get
call results. Example:
import { Bot, Context } from "grammy";
import { FileFlavor, hydrateFiles } from "@grammyjs/files";
// Transformative Context flavor
type MyContext = FileFlavor<Context>;
// Create a bot.
const bot = new Bot<MyContext>("");
// Use the plugin.
bot.api.config.use(hydrateFiles(bot.token));
// Download videos and GIFs to temporary locations.
bot.on([":video", ":animation"], async (ctx) => {
// Prepare the file for download.
const file = await ctx.getFile();
// Download the file to a temporary location.
const path = await file.download();
// Print the file path.
console.log("File saved at ", path);
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { Bot } from "grammy";
import { hydrateFiles } from "@grammyjs/files";
// Create a bot.
const bot = new Bot("");
// Use the plugin.
bot.api.config.use(hydrateFiles(bot.token));
// Download videos and GIFs to temporary locations.
bot.on([":video", ":animation"], async (ctx) => {
// Prepare the file for download.
const file = await ctx.getFile();
// Download the file to a temporary location.
const path = await file.download();
// Print the file path.
console.log("File saved at ", path);
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { Bot, Context } from "https://deno.land/x/grammy@v1.27.0/mod.ts";
import {
FileFlavor,
hydrateFiles,
} from "https://deno.land/x/grammy_files@v1.1.1/mod.ts";
// Transformative Context flavor
type MyContext = FileFlavor<Context>;
// Create a bot.
const bot = new Bot<MyContext>("");
// Use the plugin.
bot.api.config.use(hydrateFiles(bot.token));
// Download videos and GIFs to temporary locations.
bot.on([":video", ":animation"], async (ctx) => {
// Prepare the file for download.
const file = await ctx.getFile();
// Download the file to a temporary location.
const path = await file.download();
// Print the file path.
console.log("File saved at ", path);
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
.
If you only want to get the URL of the file so you can download it yourself, use file
. This will return an HTTPS link to your file that is valid for at least one hour.
Local Bot API Server
If you are using a local Bot API server, then the get
call effectively downloads the file to your disk already.
In turn, you can call file
to access that file path. Note that await file
will now simply copy that locally present file to a temporary location (or to the given path if specified).
Supporting bot.api
Calls
By default, the results of await bot
will also be equipped with download
and get
methods. However, this is not reflected in the types. If you need these calls, you should also install an API flavor on the bot object called File
:
import { Api, Bot, Context } from "grammy";
import { FileApiFlavor, FileFlavor, hydrateFiles } from "@grammyjs/files";
type MyContext = FileFlavor<Context>;
type MyApi = FileApiFlavor<Api>;
const bot = new Bot<MyContext, MyApi>("");
// ...
2
3
4
5
6
7
8
import { Api, Bot, Context } from "https://deno.land/x/grammy@v1.27.0/mod.ts";
import {
FileApiFlavor,
FileFlavor,
hydrateFiles,
} from "https://deno.land/x/grammy_files@v1.1.1/mod.ts";
type MyContext = FileFlavor<Context>;
type MyApi = FileApiFlavor<Api>;
const bot = new Bot<MyContext, MyApi>("");
// ...
2
3
4
5
6
7
8
9
10
11
12