Media Groups (built-in)
The media group plugin helps you send media groups by letting you build Input
objects. Incidentally, Input
object are also used when editing media messages, so this plugin also helps you to edit media.
Remember that Input
objects are specified here.
Building an InputMedia
Object
You can use this plugin like so:
import { InputMediaBuilder } from "grammy";
const photo = InputMediaBuilder.photo(new InputFile("/tmp/photo.mp4"));
const video = InputMediaBuilder.video(new InputFile("/tmp/video.mp4"));
// etc
2
3
4
5
const { InputMediaBuilder } = require("grammy");
const photo = InputMediaBuilder.photo(new InputFile("/tmp/photo.mp4"));
const video = InputMediaBuilder.video(new InputFile("/tmp/video.mp4"));
// etc
2
3
4
5
import { InputMediaBuilder } from "https://deno.land/x/grammy@v1.30.0/mod.ts";
const photo = InputMediaBuilder.photo(new InputFile("/tmp/photo.mp4"));
const video = InputMediaBuilder.video(new InputFile("/tmp/video.mp4"));
// etc
2
3
4
5
Check out all methods of Input
in the API reference.
You can also directly pass public URLs which Telegram fetches.
const photo = InputMediaBuilder.photo("https://grammy.dev/images/grammY.png");
Further options can be provided in an options object at the end.
const photo = InputMediaBuilder.photo("https://grammy.dev/images/grammY.png", {
caption: "grammY is awesome",
// etc
});
2
3
4
Sending a Media Group
You can send a media group as follows:
await ctx.replyWithMediaGroup([photo0, photo1, photo2, video]);
Likewise, you can pass an array of Input
objects to ctx
or bot
.
Editing a Media Message
Since Input
objects are also used to edit media messages, this plugin will assist you here, too:
const newMedia = InputMediaBuilder.photo(
"https://grammy.dev/images/grammY.png",
);
await ctx.editMessageMedia(newMedia);
2
3
4
As always, this works for ctx
and bot
, too.
Plugin Summary
This plugin is built-in into the core of grammY. You don’t need to install anything to use it. Simply import everything from grammY itself.
Also, both the documentation and the API reference of this plugin are unified with the core package.