telegraf / telegraf-ratelimit

Rate-limiting middleware for Telegraf
MIT License
37 stars 5 forks source link

Build Status NPM Version js-standard-style

Telegraf Rate Limit

Rate-limiting middleware for Telegraf (Telegram bot framework).

Installation

$ npm install telegraf-ratelimit

Example

const Telegraf = require('telegraf')
const rateLimit = require('telegraf-ratelimit')

// Set limit to 1 message per 3 seconds
const limitConfig = {
  window: 3000,
  limit: 1,
  onLimitExceeded: (ctx, next) => ctx.reply('Rate limit exceeded')
}
const telegraf = new Telegraf(process.env.BOT_TOKEN)
telegraf.use(rateLimit(limitConfig))
telegraf.on('text', (ctx) => ctx.reply('Hello!'))
telegraf.startPolling()

API

Options

Default implementation of keyGenerator:

function keyGenerator(ctx) {
  return ctx.from.id
}