Deno grammY Middlewares
A port of grammy-middlewares for Deno users of grammY, with fixes and additional functionality.
⚠️ JSR Publication Blocked ⚠️
IMPORTANT: This package cannot currently be published on JSR due to dependency constraints.
The package requires types from grammy_types which are only available via HTTPS imports from deno.land. JSR does not allow HTTPS imports in packages as documented in JSR migration guide.
JSR publication will remain blocked until:
- grammy and grammy_types are published on JSR officially
- We can update our dependencies accordingly
The jsr-blocked branch preserves our JSR publishing setup for when this becomes possible.
Motivation
- issue #4 in the original repository. The issue has been unresolved for over a year and involves a dependency that isn’t necessary on Deno so we can remove the dependency and fix the issue.
- Some functions weren’t working properly in Deno, such as
onlyAdmin. onlySuperAdminwas inconsistent as it lacked theerrorHandlermany other middlewares had.onlySuperAdminnow also supports an array of super admins.sonlyPublicexisted andonlyPrivatefelt a natural addition.
Installation
Using deno.land/x
// Import from deno.land
import { ignoreOld, onlyAdmin /* etc */ } from "https://deno.land/x/deno-grammy-middlewares/mod.ts";Usage
Importing Individual Middlewares
import {
ignoreOld,
onlyAdmin,
onlyPublic,
onlyPrivate,
onlySuperAdmin,
sequentialize,
onlyMenuAuthor,
} from "https://deno.land/x/deno-grammy-middlewares/mod.ts";Importing All Middlewares
import * as grammy_middlewares from "https://deno.land/x/deno-grammy-middlewares/mod.ts";
// Use middlewares with namespace
// grammy_middlewares.ignoreOld()Example
bot.use(
ignoreOld(),
onlyAdmin(ctx => ctx.reply(
'Only admins can do this'
)),
onlyPublic(ctx => ctx.reply(
'You can only use public chats'
)),
onlyPrivate(ctx => ctx.reply(
'You can only use private chats'
)),
onlySuperAdmin(env.SUPER_ADMIN_ID),
sequentialize()
);
// ...
menu.text(
'Only menu creator',
onlyMenuAuthor(ctx =>
ctx.reply('Only menu creator can do this')
),
ctx => // ...
);Middlewares
ignoreOld
Ignores old updates, useful when bot has been down for a while. You can optionally specify the timeout in seconds which defaults to 5 * 60.
onlyAdmin
Checks if the user is an admin. You can optionally specify errorHandler that is called with the context if the user is not an admin.
onlyPublic
Checks if it is a group chat or a channel. You can optionally specify errorHandler that is called with the context if it is not a group chat or a channel.
onlyPrivate ✨ (New)
Checks if it is a private chat. You can optionally specify errorHandler that is called with the context if it is not a private chat.
onlySuperAdmin
Checks if the user is a super admin. You have to provide the super admin id.
✨ Now supports an optional errorHandler for consistency with other middlewares.
sequentialize
The basic sequentialize middleware that takes the chat id as a sequential identifier.
- TODO: Update when grammY 2.0 is released.
onlyMenuAuthor
@grammyjs/menu middleware that checks if the user sending the callback is the menu author. To use it the menu has to reply to the menu caller.
License
MIT — use for any purpose. Would be great if you could leave a note about the original developers. Thanks!
Original package by @backmeupplz
Ported, fixed and enhanced by @adriangalilea