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 🙏

© 2025 – Pkg Stats / Ryan Hefner

grammy-callback-data

v2.0.7

Published

grammY-compatible CallbackData generator

Readme

grammy-callback-data

Type-safe utility for handling callback data in grammY bots.

Installation

npm install grammy-callback-data
# or
bun add grammy-callback-data

Quick Start

import { Bot, InlineKeyboard } from "grammy";
import { CallbackData, t } from "grammy-callback-data";

// 1. Define your callback data
const PostCD = new CallbackData("post", {
  action: t.string,
  postId: t.number,
  isConfirmed: t.boolean,
});

// 2. Create keyboard with callback data
const keyboard = new InlineKeyboard()
  .text("👍 Like", PostCD.pack({ action: "like", postId: 123 }))
  .text("❌ Delete", PostCD.pack({ action: "delete", postId: 123 }));

// 3. Use keyboard in your bot
bot.command("post", async (ctx) => {
  await ctx.reply("Post actions:", {
    reply_markup: keyboard,
  });
});

// 4. Handle specific actions using filter
bot.callbackQuery(PostCD.filter({ action: "like" }), async (ctx) => {
  const { postId } = PostCD.unpack(ctx.callbackQuery.data);
  await ctx.reply(`Post ${postId} liked!`);
});

// Handle delete with confirmation
bot.callbackQuery(PostCD.filter({ action: "delete" }), async (ctx) => {
  const { postId, isConfirmed } = PostCD.unpack(ctx.callbackQuery.data);

  if (!isConfirmed) {
    const confirmKeyboard = new InlineKeyboard().text(
      "Confirm Delete",
      PostCD.pack({ action: "delete", postId, isConfirmed: true })
    );

    await ctx.reply("Are you sure?", {
      reply_markup: confirmKeyboard,
    });
    return;
  }

  await ctx.reply(`Post ${postId} deleted!`);
});

Features

  • 🔒 Type-safe callback data handling with TypeScript
  • 🎯 Built-in data validation
  • 🔍 Powerful filtering by action and fields
  • 💪 Support for string, number and boolean values
  • 🎭 Compact data format for Telegram's 64-byte limit

API

Creating Callback Data

const callback = new CallbackData(prefix, {
  field1: { type: DataType.string },
  field2: { type: DataType.number, required: false },
  field3: { type: DataType.boolean },
});

Packing Data

const data = callback.pack({
  field1: "value",
  field2: 123,
  field3: true,
});

Unpacking Data

const { field1, field2, field3 } = callback.unpack(ctx.callbackQuery.data);

Filtering Callbacks

// Filter by prefix only
bot.callbackQuery(callback.filter(), handler);

// Filter by specific fields
bot.callbackQuery(callback.filter({ field1: "value" }), handler);

Documentation

For detailed documentation and examples, visit the package directory.

License

MIT