refactor: switch to dedicated group, remove @mention requirement, add pinned message service

- Change ALLOWED_GROUP to Dienst opvragen EdGPT-EBS (120363407613652082@g.us)
- Remove isBotMentioned check (no @mention needed)
- Match only exact 4-digit messages (regex /^\s*(\d{4})\s*$/)
- Add pinnedMessageService.js: auto-repins welcome message when expired
- Move incrementDaily() to count all 4-digit messages, not just successful ones
- Update daily summary with emoji
- Update docs and .env.example
This commit is contained in:
Eddy de Vink 2026-05-26 09:20:45 +02:00
commit e4d817ff80
5 changed files with 96 additions and 35 deletions

View file

@ -37,7 +37,7 @@ cron.schedule('0 10 * * *', () => {
resetDay()
const total = getYesterdayTotal()
sendTextMessage(adminNumber, `Aantal dienstaanvragen gisteren: ${total}`)
sendTextMessage(adminNumber, `📊 Aantal dienstaanvragen gisteren: ${total}`)
.then(() => console.log(`Daily summary sent: ${total} requests`))
.catch(err => console.error('Failed to send daily summary:', err.message))
})

View file

@ -2,28 +2,15 @@ const { findDienst, getPngFilePaths } = require('./dienstService')
const { sendTextMessage, sendImageMessage, notifyAdmin } = require('./whatsappApi')
const { checkLimit, increment, MAX_REQUESTS_PER_DAY } = require('./rateLimiter')
const { increment: incrementDaily } = require('./dailyStats')
const { checkAndRepinPinnedMessage } = require('./pinnedMessageService')
const BOT_NAME = (process.env.BOT_NAME || 'EdGpt').toLowerCase()
const ALLOWED_GROUP = process.env.ALLOWED_GROUP
function extractDienstnummer(body) {
const match = body.match(/\b(\d{4})\b/)
const match = body.match(/^\s*(\d{4})\s*$/)
return match ? match[1] : null
}
function isBotMentioned(message) {
if (message._data?.mentionedJidList?.length > 0) {
return true
}
if (message.body) {
const normalized = message.body.toLowerCase().replace(/\s+/g, '')
if (normalized.includes(`@${BOT_NAME.replace(/\s+/g, '')}`)) {
return true
}
}
return false
}
async function handleWebhook(payload) {
if (payload.dataType !== 'message') return
if (payload.sessionId !== process.env.WHATSAPP_SESSION) return
@ -32,11 +19,14 @@ async function handleWebhook(payload) {
if (!message) return
if (message.fromMe) return
if (message.from !== ALLOWED_GROUP) return
if (!isBotMentioned(message)) return
checkAndRepinPinnedMessage()
const dienstnummer = extractDienstnummer(message.body || '')
if (!dienstnummer) return
incrementDaily()
console.log(`Processing dienst ${dienstnummer} for ${message.author}`)
const author = message.author
@ -50,7 +40,6 @@ async function handleWebhook(payload) {
}
increment(author)
incrementDaily()
const dienst = findDienst(dienstnummer)
if (!dienst) {

View file

@ -0,0 +1,63 @@
const axios = require('axios')
const API_BASE = process.env.WHATSAPP_API_ENDPOINT
const SESSION = process.env.WHATSAPP_SESSION
const ALLOWED_GROUP = process.env.ALLOWED_GROUP
const PIN_DURATION = 30 * 24 * 60 * 60
const PINNED_TEXT = 'Welkom in Dienst opvragen EdGpt-EBS. Stuur hier je 4 cijferige dienstnummer en EdGpt zal het prive naar je toe sturen, alleen 4 cijfers meer niet.'
async function getPinnedMessages() {
try {
const url = `${API_BASE}/groupChat/runMethod/${SESSION}`
const response = await axios.post(url, {
chatId: ALLOWED_GROUP,
method: 'getPinnedMessages'
})
return response.data.data || []
} catch (err) {
console.error('Failed to get pinned messages:', err.message)
return []
}
}
async function sendPinnedMessage() {
const url = `${API_BASE}/client/sendMessage/${SESSION}`
const response = await axios.post(url, {
chatId: ALLOWED_GROUP,
contentType: 'string',
content: PINNED_TEXT
})
return response.data
}
async function pinMessage(messageId) {
const url = `${API_BASE}/message/runMethod/${SESSION}`
const response = await axios.post(url, {
chatId: ALLOWED_GROUP,
messageId,
method: 'pin',
options: PIN_DURATION
})
return response.data
}
async function checkAndRepinPinnedMessage() {
try {
const pinned = await getPinnedMessages()
if (pinned.length > 0) return
console.log('No pinned message found, sending and pinning...')
const sent = await sendPinnedMessage()
const messageId = sent?.message?.id?._serialized
if (!messageId) {
console.error('Failed to get messageId from sent message')
return
}
await pinMessage(messageId)
console.log(`Pinned message sent and pinned: ${messageId}`)
} catch (err) {
console.error('Failed to check/repin pinned message:', err.message)
}
}
module.exports = { checkAndRepinPinnedMessage }