Introduction
Lightweight and simple library that helps manage callback data when using inline
keyboards.
Installation
Node
$ npm install callback-data
Deno
import { createCallbackData } from "https://deno.land/x/callback_data/mod.ts";
Example
import { Bot, InlineKeyboard } from "grammy";
import { createCallbackData } from "callback-data";
const bot = new Bot(process.env.BOT_TOKEN);
const greetingData = createCallbackData("greeting", {
type: String,
});
const greetingKeyboard = new InlineKeyboard()
.text(
"oldschool",
greetingData.pack({
type: "oldschool",
}),
)
.text(
"modern",
greetingData.pack({
type: "modern",
}),
);
bot.command("start", (ctx) =>
ctx.reply("How to greet?", {
reply_markup: greetingKeyboard,
}));
bot.callbackQuery(
greetingData.filter({
type: "oldschool",
}),
(ctx) => ctx.answerCallbackQuery("Hello"),
);
bot.callbackQuery(
greetingData.filter({
type: "modern",
}),
(ctx) => ctx.answerCallbackQuery("Yo"),
);
bot.start();
There's more complex example
counter-bot.
Usage
Create callback data builder
const callbackData = createCallbackData(
"data",
{
id: Number,
name: String,
isAdmin: Boolean,
},
);
Pack callback data
const packedData = callbackData.pack({
id: 1337,
name: "John",
isAdmin: true,
});
console.log(packedData);
Unpack callback data
const unpackedData = callbackData.unpack(packedData);
console.log(unpackedData);
Filter by callback data
const regex = callbackData.filter({
id: 1337,
});
console.log(regex);