What are Plugins?
In grammY, a plugin is simply middleware that provides a specific feature or functionality. Since grammY’s middleware system is extremely flexible, plugins can:- Add properties to the context object
- Process updates before they reach your handlers
- Provide helper methods and utilities
- Manage state and session data
- Integrate with external services
The Middleware Foundation
Every plugin in grammY is built on top of the middleware system. Understanding middleware is key to understanding plugins:Plugin Types
grammY plugins come in two main categories:Built-in Plugins
These are utilities that ship with grammY core:- Session - Persistent data storage per chat/user
- Keyboard Builders - Simplified keyboard creation (Keyboard, InlineKeyboard)
- Webhook Callback - Easy webhook integration
- Inline Query Builder - Helper for inline query results
Official Plugins
These are maintained by the grammY team but distributed separately:- Router - Route updates to different handlers
- Menu - Interactive menus with inline keyboards
- Conversations - Stateful, step-by-step user interactions
- I18n - Internationalization and localization
- Hydrate - Easy message and callback query editing
- Files - Simplified file handling
- Transformer - Modify API requests before sending
- Ratelimiter - Prevent spam and abuse
- Stateless Question - Ask questions without sessions
- Autoquote - Automatically quote replied messages
- Parse Mode - Set default parse mode
- Emoji - Type-safe emoji support
Using Plugins
Most plugins are installed using thebot.use() method:
Plugin Installation Pattern
Typical plugin usage follows this pattern:Context Flavors
Many plugins extend the context object with additional properties and methods using context flavors:Plugin Composition
Plugins can be composed together using theComposer class:
Plugin Execution Order
Plugins execute in the order they’re installed:Conditional Plugin Installation
You can install plugins conditionally:Error Handling in Plugins
Plugins can throw errors that bubble up to your error handler:Plugin Best Practices
- Install plugins early - Install plugins like session before your handlers
- Use type-safe flavors - Leverage TypeScript’s context flavors for type safety
- Keep plugins focused - Each plugin should do one thing well
- Document dependencies - Note which plugins depend on others
- Handle errors gracefully - Don’t let plugin errors crash your bot
- Test plugin interactions - Ensure plugins work well together
Finding Plugins
Official plugins are available in the@grammyjs namespace on npm:
Next Steps
- Learn about Built-in Plugins that ship with grammY
- Discover how to Create Your Own Plugins
- Explore the official plugins on the grammY website