grammy-json-query
v0.0.2
Published
JSON Callback Query Plugin for grammY
Maintainers
Readme
JSON Callback Query Plugin for grammY
A grammY plugin that extends InlineKeyboard with a
convenient .json() method for creating buttons with JSON-encoded callback
data, and provides middleware to automatically parse callback query data as JSON.
Features
- InlineKeyboard — extends grammY's
InlineKeyboardwith a.json()method that encodes callback data as JSON. - jsonQuery middleware — hydrates
ctx.jsonQuerywith the parsed JSON from callback query data. - JsonQueryFlavor — TypeScript context flavor for type-safe access to
ctx.jsonQuery.
Installation
Node.js
npm install grammy-json-queryDeno
import {
InlineKeyboard,
jsonQuery,
type JsonQueryFlavor,
} from "npm:grammy-json-query";Usage
import { Bot, Context } from "grammy";
import {
InlineKeyboard,
jsonQuery,
type JsonQueryFlavor,
} from "grammy-json-query";
type MyContext = Context & JsonQueryFlavor;
const bot = new Bot<MyContext>("<your-bot-token>");
// Install the JSON query middleware
bot.use(jsonQuery());
// Send an inline keyboard with JSON-encoded buttons
bot.command("start", async (ctx) => {
const keyboard = new InlineKeyboard()
.json("Like 👍", { action: "vote", value: "like" })
.json("Dislike 👎", { action: "vote", value: "dislike" });
await ctx.reply("Rate this bot:", { reply_markup: keyboard });
});
// Handle callback queries with parsed JSON data
bot.on("callback_query:data", async (ctx) => {
const data = ctx.jsonQuery as { action: string; value: string };
if (data?.action === "vote") {
await ctx.answerCallbackQuery({
text: `You voted: ${data.value}`,
});
}
});
bot.start();Static Method
You can also use the static .json() method to create a button in one step:
const button = InlineKeyboard.json("Click me", { id: 42 });