Add a new command, event, or callback handler to a GramIO bot with proper typing, context usage, and best practices.
Install
npx skillscat add gramiojs/documentation/skills-gramio-add-handler Install via the SkillsCat registry.
SKILL.md
Add GramIO Handler
You are adding a new handler to an existing GramIO bot project.
Arguments
The user provides what kind of handler they need, e.g.:
/gramio-add-handler command /settings— a new/settingscommand/gramio-add-handler callback approve_*— a callback query handler/gramio-add-handler hears "hello"— a text pattern handler
Handler Types
Command Handler
bot.command("commandname", (context) => {
// context.args — command arguments after /commandname
return context.send("Response text");
});Hears (Text Pattern)
bot.hears(/pattern|text/, (context) => {
return context.send("Matched!");
});Callback Query
bot.callbackQuery("callback_data", (context) => {
// context.data — the callback data string
return context.answer("Callback processed!");
});Inline Query
bot.inlineQuery(/pattern/, (context) => {
return context.answer([
{
type: "article",
id: "1",
title: "Result",
input_message_content: {
message_text: "Selected result",
},
},
]);
});Reaction Handler
bot.reaction("👍", (context) => {
return context.reply("Thanks for the reaction!");
});Generic Update Handler
bot.on("message", (context) => {
// Handles all messages
});Steps
Read the existing bot code — find the main bot file (usually
src/index.tsor similar).Determine handler type from the user's request.
Add the handler to the bot chain, before
.start():- Place it logically near similar handlers.
- Use proper TypeScript types.
- Include keyboard responses if the handler needs them.
If the handler needs session/state, check if
@gramio/sessionis installed:import { sessionPlugin } from "@gramio/session"; // ensure bot.extend(sessionPlugin(...)) is calledIf the handler needs scenes (multi-step flows), check if
@gramio/scenesis installed.Show the user the added code and suggest testing with the bot.