refactor: verwijder alle Appium-fallback (wa-appium-api afgedaan, nooit meer)

This commit is contained in:
Eddy de Vink 2026-09-24 23:08:08 +02:00
commit 431d74ed70
3 changed files with 6 additions and 112 deletions

View file

@ -1,6 +1,3 @@
const fs = require('fs')
const path = require('path')
const API_BASE = process.env.WHATSAPP_API_ENDPOINT
const { findDienst, getPngFilePaths } = require('./dienstService')
const { sendTextMessage, sendImageMessage, notifyAdmin, sendChatSeen } = require('./whatsappApi')
const { checkLimit, increment, MAX_REQUESTS_PER_DAY } = require('./rateLimiter')
@ -13,23 +10,6 @@ const ALLOWED_GROUP_NAMES = (process.env.ALLOWED_GROUP_NAME || '')
.map(name => name.trim())
.filter(Boolean)
const ALLOWED_CHAT_IDS = new Set([ALLOWED_GROUP, ...ALLOWED_GROUP_NAMES].filter(Boolean))
const appiumEventCooldown = 24 * 60 * 60 * 1000
const appiumEventStatePath = process.env.APPIUM_EVENT_STATE_PATH ||
path.join(__dirname, '..', 'appium-event-state.json')
let appiumEventState = { events: [] }
try {
appiumEventState = JSON.parse(fs.readFileSync(appiumEventStatePath, 'utf8'))
if (!Array.isArray(appiumEventState.events)) appiumEventState = { events: [] }
appiumEventState.events = appiumEventState.events.map(event => {
const parts = event.key.split('\u0000')
return {
key: [normalizeChatId(parts[0]), parts[1], parts[2]].join('\u0000'),
processedAt: event.processedAt || event.timestamp || 0,
}
})
} catch {
appiumEventState = { events: [] }
}
function normalizeChatId(chatId) {
return (chatId || '')
@ -38,30 +18,6 @@ function normalizeChatId(chatId) {
.trim()
}
function getAppiumEventKey(payload) {
return [normalizeChatId(payload.chatId), payload.sender, payload.text].join('\u0000')
}
function isDuplicateAppiumEvent(payload) {
const now = Date.now()
appiumEventState.events = appiumEventState.events.filter(event =>
now - (event.processedAt || event.timestamp || 0) < appiumEventCooldown
)
return appiumEventState.events.some(event =>
event.key === getAppiumEventKey(payload)
)
}
function markAppiumEventProcessed(payload) {
appiumEventState.events.push({
key: getAppiumEventKey(payload),
processedAt: Date.now(),
})
appiumEventState.events = appiumEventState.events.slice(-500)
const temporaryPath = `${appiumEventStatePath}.tmp`
fs.writeFileSync(temporaryPath, JSON.stringify(appiumEventState))
fs.renameSync(temporaryPath, appiumEventStatePath)
}
function extractDienstnummer(body) {
const match = body.match(/^\s*(\d{4,5})\s*$/)
return match ? match[1] : null
@ -75,19 +31,10 @@ async function handleWebhook(payload) {
if (id) handleAck(id, ack)
return
}
const isWWebEvent = payload.dataType === 'message'
const isAppiumEvent = Boolean(payload.sender && payload.chatId)
if (!isWWebEvent && !isAppiumEvent) return
if (isAppiumEvent && isDuplicateAppiumEvent(payload)) return
if (isWWebEvent && payload.sessionId !== process.env.WHATSAPP_SESSION) return
const message = isWWebEvent
? payload.data?.message
: {
fromMe: false,
from: payload.chatId,
author: payload.sender,
body: payload.text || '',
}
if (payload.dataType !== 'message') return
if (payload.sessionId !== process.env.WHATSAPP_SESSION) return
const message = payload.data?.message
if (!message) return
if (message.fromMe) return
if (!ALLOWED_CHAT_IDS.has(normalizeChatId(message.from))) return
@ -103,9 +50,7 @@ async function handleWebhook(payload) {
console.log(`Processing dienst ${dienstnummer} for ${message.author}`)
const author = isWWebEvent
? message.author || message.from
: message.author
const author = message.author || message.from
if (!author) return
const limit = checkLimit(author)
@ -120,32 +65,25 @@ async function handleWebhook(payload) {
const dienst = findDienst(dienstnummer)
if (!dienst) {
await sendTextMessage(author, `Dienstnummer ${dienstnummer} niet gevonden.`)
if (isAppiumEvent) markAppiumEventProcessed(payload)
return
}
const pngPaths = getPngFilePaths(dienst)
if (!pngPaths || pngPaths.length === 0) {
await sendTextMessage(author, `Geen pagina's gevonden voor dienst ${dienstnummer}.`)
if (isAppiumEvent) markAppiumEventProcessed(payload)
return
}
let sendFailed = false
for (const pngPath of pngPaths) {
const filename = pngPath.split('/').pop()
try {
await sendImageMessage(author, pngPath, filename)
} catch (err) {
sendFailed = true
const errorMsg = `Fout bij verzenden ${filename} naar ${author}: ${err.message}`
console.error(errorMsg)
if (API_BASE.includes(':3001')) {
await notifyAdmin(errorMsg)
}
await notifyAdmin(errorMsg)
}
}
if (!sendFailed && isAppiumEvent) markAppiumEventProcessed(payload)
}
module.exports = { handleWebhook, extractDienstnummer }