Create GramIO Plugin
You are creating a custom plugin for the GramIO framework.
Arguments
The user provides [plugin-name] and optionally describes what the plugin should do.
Steps
- •
Choose approach — there are two ways:
Option A: Use scaffolding tool (recommended for standalone packages)
bashnpm create gramio-plugin ./plugin-name
Option B: Create inline plugin (for project-internal use)
Create the plugin file directly in the user's project.
- •
For inline plugins, create the plugin file:
typescript// src/plugins/plugin-name.ts import { Plugin } from "gramio"; // Optional: custom error class export class PluginNameError extends Error { code: string; constructor(message: string, code: string) { super(message); this.code = code; } } export const pluginName = new Plugin("plugin-name") // Register custom error type (optional) .error("PLUGIN_NAME", PluginNameError) // Derive new properties onto context .derive((context) => { return { // These properties become available on context myHelper() { return "hello from plugin"; }, }; }); - •
Register the plugin in the bot:
typescriptimport { pluginName } from "./plugins/plugin-name"; const bot = new Bot(process.env.BOT_TOKEN as string) .extend(pluginName) // Now context.myHelper() is available and typed .command("test", (context) => { return context.send(context.myHelper()); }); - •
Plugin capabilities — remind the user what plugins can do:
- •
.derive()— add properties/methods to context (type-safe) - •
.error()— register custom error types for.onError()handling - •
.on()/.use()— add middleware - •
.group()— create handler groups - •Plugins can also use
.preRequest(),.onResponse(),.onResponseError()hooks
- •
- •
If the plugin needs state/storage:
typescriptimport { sessionPlugin } from "@gramio/session"; // Combine with session for stateful plugins const bot = new Bot("TOKEN") .extend(sessionPlugin({ key: "myState", initial: () => ({}) })) .extend(pluginName); - •
Test the plugin — suggest the user test with a simple command:
typescriptbot.command("test-plugin", (context) => { // verify derived properties work return context.send(String(context.myHelper())); });