{"id":18846,"library":"telegraf-ratelimit","title":"telegraf-ratelimit","description":"Rate-limiting middleware for the Telegraf Telegram bot framework (v2.0.0). Simple in-memory rate limiter with configurable time window, request limit, custom key generation, and exceeded-limit callback. Lightweight, no external dependencies, designed for ease of integration with Telegraf. Suitable for basic bot rate limiting but not distributed or persistent (in-memory only). Actively maintained as part of the Telegraf ecosystem.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/telegraf/telegraf-ratelimit","tags":["javascript","telegram","telegraf","rate limit","middleware"],"install":[{"cmd":"npm install telegraf-ratelimit","lang":"bash","label":"npm"},{"cmd":"yarn add telegraf-ratelimit","lang":"bash","label":"yarn"},{"cmd":"pnpm add telegraf-ratelimit","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export; use import or require with default.","wrong":"const { rateLimit } = require('telegraf-ratelimit')","symbol":"default","correct":"import rateLimit from 'telegraf-ratelimit'"},{"note":"CommonJS require returns the function directly, not an object.","wrong":"const { rateLimit } = require('telegraf-ratelimit')","symbol":"rateLimit (named require)","correct":"const rateLimit = require('telegraf-ratelimit')"},{"note":"Use default import for TypeScript; the module does not have proper type definitions.","wrong":"import * as rateLimit from 'telegraf-ratelimit'","symbol":"TypeScript usage","correct":"import rateLimit from 'telegraf-ratelimit'"}],"quickstart":{"code":"const Telegraf = require('telegraf')\nconst rateLimit = require('telegraf-ratelimit')\n\nconst bot = new Telegraf(process.env.BOT_TOKEN ?? '')\n\nconst limitConfig = {\n  window: 3000,\n  limit: 1,\n  onLimitExceeded: (ctx, next) => ctx.reply('Rate limit exceeded')\n}\n\nbot.use(rateLimit(limitConfig))\nbot.on('text', (ctx) => ctx.reply('Hello!'))\nbot.launch()","lang":"javascript","description":"Create a Telegram bot with Telegraf and apply rate limiting: 1 message per 3 seconds per user."},"warnings":[{"fix":"Use an external store (e.g., Redis) with a custom keyGenerator and external state.","message":"In-memory rate limit state is not shared across processes; restarting the bot resets all counters.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Provide a custom keyGenerator that handles missing ctx.from.","message":"The default keyGenerator uses ctx.from.id; if ctx.from is undefined (e.g., in channels), it will throw.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use a keyGenerator that includes chat ID, e.g., (ctx) => `${ctx.from.id}:${ctx.chat.id}`.","message":"The middleware does not differentiate between different chat contexts; same user across chats is counted together.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Ensure Node.js >=12 (recommended) as older versions are out of support.","message":"Node.js engine requirement >=6.2.1; may not work on very old or very new Node versions.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `const rateLimit = require('telegraf-ratelimit')` (CommonJS) or `import rateLimit from 'telegraf-ratelimit'` (ESM).","cause":"Using named destructuring import on the default export.","error":"TypeError: rateLimit is not a function"},{"fix":"Always provide a custom keyGenerator: `keyGenerator: (ctx) => ctx.from?.id ?? ctx.chat?.id`","cause":"Default keyGenerator tries to access ctx.from.id when ctx.from is undefined.","error":"Cannot read property 'id' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}