feat: integrate rate limiter into message handler
This commit is contained in:
parent
52c51fbf3f
commit
3d933c96a6
1 changed files with 10 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
const { findDienst, getPngFilePaths } = require('./dienstService')
|
const { findDienst, getPngFilePaths } = require('./dienstService')
|
||||||
const { sendTextMessage, sendImageMessage, notifyAdmin } = require('./whatsappApi')
|
const { sendTextMessage, sendImageMessage, notifyAdmin } = require('./whatsappApi')
|
||||||
|
const { checkLimit, increment, MAX_REQUESTS_PER_DAY } = require('./rateLimiter')
|
||||||
|
|
||||||
const BOT_NAME = (process.env.BOT_NAME || 'EdGpt').toLowerCase()
|
const BOT_NAME = (process.env.BOT_NAME || 'EdGpt').toLowerCase()
|
||||||
const ALLOWED_GROUP = process.env.ALLOWED_GROUP
|
const ALLOWED_GROUP = process.env.ALLOWED_GROUP
|
||||||
|
|
@ -40,6 +41,15 @@ async function handleWebhook(payload) {
|
||||||
const author = message.author
|
const author = message.author
|
||||||
if (!author) return
|
if (!author) return
|
||||||
|
|
||||||
|
const limit = checkLimit(author)
|
||||||
|
if (!limit.allowed) {
|
||||||
|
await sendTextMessage(author, `Je hebt vandaag het maximum van ${limit.max} diensten bereikt. Probeer het morgen opnieuw.`)
|
||||||
|
console.log(`Rate limit reached for ${author} (${limit.count}/${limit.max})`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
increment(author)
|
||||||
|
|
||||||
const dienst = findDienst(dienstnummer)
|
const dienst = findDienst(dienstnummer)
|
||||||
if (!dienst) {
|
if (!dienst) {
|
||||||
await sendTextMessage(author, `Dienstnummer ${dienstnummer} niet gevonden.`)
|
await sendTextMessage(author, `Dienstnummer ${dienstnummer} niet gevonden.`)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue