npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

grammy-media-group

v1.0.4

Published

Middleware and utilities for handling and grouping media messages (photos, videos) in grammY bots, enabling easy processing of media groups.

Readme

MediaGroup Middleware for grammY

A lightweight middleware for grammY Telegram bots that collects and groups media messages (like photos and videos) into a single media_group context property. This makes it easier to handle albums using a simple timeout-based strategy.


✨ Features

  • 🖼️ Handles both photos and videos (albums)
  • 🧩 Provides a mediaGroup filter for a clean handler with bot.filter()
  • 🧩 Provides copyMediaGroup function to copy media groups
  • ✅ Type-safe with TypeScript support

📦 Installation

npm install grammy-media-group

🚀 Usage

1. Setup (ESM / TypeScript)

import { Bot } from "grammy";
import { MediaGroupHandler, mediaGroup } from "grammy-media-group";

const bot = new Bot("<BOT_TOKEN>");
const mediaGroupHandler = new MediaGroupHandler(4000); // optional timeout in ms

bot.use(mediaGroupHandler.middleware());

1. Setup (CommonJS)

const { Bot } = require("grammy");
const { MediaGroupHandler, mediaGroup } = require("grammy-media-group");

const bot = new Bot("<BOT_TOKEN>");
const mediaGroupHandler = new MediaGroupHandler(4000); // optional timeout in ms

bot.use(mediaGroupHandler.middleware());

2. Handle Media Groups

This example shows how to use the mediaGroup filter to detect incoming media groups. When a media group is detected, the handler/middleware processes the grouped media files.

bot.filter(mediaGroup, async (ctx) => {
  const media = ctx.media_group;
  console.log(media.length); // outputs the media group (album) length
  // handle the media group...
});

📋 Copying a Media Group

You can use the copyMediaGroup utility function to copy a received media group, or copy it directly from the context into a format ready to be sent. This is useful if you want to resend an album with a custom caption or formatting.

// Import if you don't want to copy it directly from the context
import { copyMediaGroup } from "grammy-media-group";
// Or require if you are using CommonJS
const { copyMediaGroup } = require("grammy-media-group");

bot.filter(mediaGroup, async (ctx) => {
  // You can pass an optional options object to customize caption
  // and formatting
  const copiedMediaGroup = copyMediaGroup(ctx.media_group, {
    caption: "<b>Here's the album!</b>",
    parse_mode: "HTML"
  });

  // Or you can also copy the media group directly from the context
  // and pass the optional options object there as well
  const copiedMediaGroup = ctx.copyMediaGroup({
    caption: "<b>Here's the album!</b>",
    parse_mode: "HTML"
  });

  // Then you are ready to send the copied media group
  await ctx.api.sendMediaGroup(ctx.chat.id, copiedMediaGroup);
});

⚠️ Notes

  • Note: This workaround may be unreliable or imprecise, as it relies on a timeout to determine when a media group is complete, which may lead to inaccuracies in cases of delayed or dropped messages.