Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dotcypress committed Nov 18, 2017
1 parent c8c5fc8 commit d5d6987
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 70 deletions.
6 changes: 1 addition & 5 deletions .eslintrc
@@ -1,7 +1,3 @@
{
"extends": ["standard"],
"plugins": ["mocha"],
"env": {
"mocha": true
}
"extends": ["standard"]
}
18 changes: 8 additions & 10 deletions examples/simple-bot.js
@@ -1,27 +1,25 @@
const Telegraf = require('telegraf')
const RateLimit = require('../lib/rate-limit')

const telegraf = new Telegraf(process.env.BOT_TOKEN)
const rateLimit = require('../lib/rate-limit')

// Set limit to 1 message per 3 seconds per chat per user
const limiter = new RateLimit({
const config = {
window: 3000,
limit: 1,
onLimitExceeded: (ctx, next) => ctx.reply('Rate limit exceeded')
})
telegraf.use(limiter.middleware())
}

// Set limit to 1 sticker per 1 minute per chat
const stickerLimiter = new RateLimit({
const stickerLimitConfig = {
window: 60 * 1000,
limit: 1,
keyGenerator: function (ctx) {
return ctx.chat.id
},
onLimitExceeded: (ctx, next) => ctx.reply('Sticker rate limit exceeded')
})
}

const telegraf = new Telegraf(process.env.BOT_TOKEN)
telegraf.use(rateLimit(config))
telegraf.on('sticker', rateLimit(stickerLimitConfig), (ctx) => ctx.reply('Nice sticker'))
telegraf.on('text', (ctx) => ctx.reply('Hey'))
telegraf.on('sticker', stickerLimiter.middleware(), (ctx) => ctx.reply('Nice sticker'))

telegraf.startPolling()
1 change: 0 additions & 1 deletion lib/memory-store.js
@@ -1,5 +1,4 @@
class MemoryStore {

constructor (clearPeriod) {
this.hits = new Map()
setInterval(this.reset.bind(this), clearPeriod)
Expand Down
41 changes: 17 additions & 24 deletions lib/rate-limit.js
@@ -1,30 +1,23 @@
const debug = require('debug')('telegraf:ratelimit')
const MemoryStore = require('./memory-store.js')

class RateLimit {
constructor (config) {
this.config = Object.assign({
window: 1000,
limit: 1,
keyGenerator: function (ctx) {
return ctx.from.id
},
onLimitExceeded: () => undefined
}, config)
this.store = new MemoryStore(this.config.window)
}

middleware () {
return (ctx, next) => {
const key = this.config.keyGenerator(ctx)
debug('key', key)
if (!key) {
return next()
}
const hit = this.store.incr(key)
return hit <= this.config.limit ? next() : this.config.onLimitExceeded(ctx, next)
module.exports = function limit (options) {
const config = Object.assign({
window: 1000,
limit: 1,
keyGenerator: function (ctx) {
return ctx.from && ctx.from.id
},
onLimitExceeded: () => {}
}, options)
const store = new MemoryStore(config.window)
return (ctx, next) => {
const key = config.keyGenerator(ctx)
if (!key) {
return next()
}
const hit = store.incr(key)
debug('key stats', key, hit)
return hit <= config.limit ? next() : config.onLimitExceeded(ctx, next)
}
}

module.exports = RateLimit
23 changes: 8 additions & 15 deletions package.json
Expand Up @@ -9,9 +9,7 @@
},
"keywords": [
"telegram",
"telegram bot",
"telegraf",
"bot framework",
"rate limit",
"middleware"
],
Expand All @@ -29,22 +27,17 @@
"lib/memory-store.js"
],
"scripts": {
"test": "mocha test --require should"
"test": "eslint ."
},
"dependencies": {
"debug": "^2.2.0"
},
"peerDependencies": {
"telegraf": "^2.0.0 || ^3.0.0"
"debug": "^3.1.0"
},
"devDependencies": {
"eslint": "^2.10.2",
"eslint-config-standard": "^5.3.1",
"eslint-plugin-mocha": "^3.0.0",
"eslint-plugin-promise": "^1.1.0",
"eslint-plugin-standard": "^1.3.2",
"mocha": "^2.4.5",
"should": "^9.0.2",
"telegraf": "^2.0.0"
"eslint": "^4.1.1",
"eslint-config-standard": "^10.2.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-node": "^5.1.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1"
}
}
14 changes: 6 additions & 8 deletions readme.md
Expand Up @@ -16,19 +16,17 @@ $ npm install telegraf-ratelimit

```js
const Telegraf = require('telegraf')
const RateLimit = require('telegraf-ratelimit')

const telegraf = new Telegraf(process.env.BOT_TOKEN)
const rateLimit = require('telegraf-ratelimit')

// Set limit to 1 message per 3 seconds
const limiter = new RateLimit({
const limitConfig = {
window: 3000,
limit: 1,
onLimitExceeded: (ctx, next) => ctx.reply('Rate limit exceeded')
})

telegraf.use(limiter.middleware())
telegraf.on('text', (ctx) => ctx.reply('Hey'))
}
const telegraf = new Telegraf(process.env.BOT_TOKEN)
telegraf.use(rateLimit(limitConfig))
telegraf.on('text', (ctx) => ctx.reply('Hello!'))
telegraf.startPolling()

```
Expand Down
7 changes: 0 additions & 7 deletions test/limiter-test.js

This file was deleted.

0 comments on commit d5d6987

Please sign in to comment.