Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8c5fc8
commit d5d6987
Showing
7 changed files
with
40 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
{ | ||
"extends": ["standard"], | ||
"plugins": ["mocha"], | ||
"env": { | ||
"mocha": true | ||
} | ||
"extends": ["standard"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.