Rate-limiting middleware for Telegraf
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples refactoring Nov 18, 2017
lib refactoring Nov 18, 2017
.editorconfig initial commit Jun 28, 2016
.eslintignore initial commit Jun 28, 2016
.eslintrc refactoring Nov 18, 2017
.gitignore initial commit Jun 28, 2016
.travis.yml initial commit Jun 28, 2016
LICENSE initial commit Jun 28, 2016
package.json bump version Nov 18, 2017
readme.md refactoring Nov 18, 2017

readme.md

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

  • window: how long to keep records of requests in memory in ms (default: 1 second)
  • limit: max number of messages during window (default: 1)
  • keyGenerator: key generator function (context -> any)
  • onLimitExceeded: rate-limit middleware

Default implementation of keyGenerator:

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