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
typescript
bot.command("commandname", (context) => {
// context.args — command arguments after /commandname
return context.send("Response text");
});
Hears (Text Pattern)
typescript
bot.hears(/pattern|text/, (context) => {
return context.send("Matched!");
});
Callback Query
typescript
bot.callbackQuery("callback_data", (context) => {
// context.data — the callback data string
return context.answer("Callback processed!");
});
Inline Query
typescript
bot.inlineQuery(/pattern/, (context) => {
return context.answer([
{
type: "article",
id: "1",
title: "Result",
input_message_content: {
message_text: "Selected result",
},
},
]);
});
Reaction Handler
typescript
bot.reaction("👍", (context) => {
return context.reply("Thanks for the reaction!");
});
Generic Update Handler
typescript
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:typescriptimport { sessionPlugin } from "@gramio/session"; // ensure bot.extend(sessionPlugin(...)) is called - •
If the handler needs scenes (multi-step flows), check if
@gramio/scenesis installed. - •
Show the user the added code and suggest testing with the bot.