diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..952158d --- /dev/null +++ b/.env.example @@ -0,0 +1,15 @@ +# WhatsApp API Configuration +WHATSAPP_API_ENDPOINT=http://localhost:3000 +WHATSAPP_SESSION=EdGpt + +# Webhook Configuration +WEBHOOK_PORT=3002 +ALLOWED_GROUP=120363407356808413@g.us +BOT_NAME=EdGpt + +# Paths +DIENSTEN_JSON=./diensten/dienst-tijden.json +DIENSTEN_PNG=./diensten/png + +# Admin notifications +ADMIN_NUMBER=31612345678@c.us diff --git a/.gitignore b/.gitignore index 965b3a1..c4dd84a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ +.env diensten/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5f2e4e --- /dev/null +++ b/README.md @@ -0,0 +1,127 @@ +# Dienst Bot + +WhatsApp bot die dienstschema's (PNG) verstuurt op basis van een dienstnummer in een WhatsApp groep. + +## Hoe het werkt + +1. Iemand tagt **@EdGpt** in de toegestane groep met een 4-cijferig dienstnummer (bijv. `@EdGpt 4002`) +2. De bot zoekt het dienstschema op in `dienst-tijden.json` +3. De bijbehorende PNG-bestanden worden privé naar de afzender gestuurd + +## Vereisten + +- [Node.js](https://nodejs.org/) 18+ +- [wwebjs-api](https://github.com/pedroslopez/whatsapp-web.js) Docker container (poort 3000) +- WhatsApp sessie "EdGpt" actief in wwebjs-api +- PM2 (optioneel, voor procesbeheer) + +## Installatie + +```bash +git clone +cd dienst_echo_whatsappgroep +npm install +``` + +## Configuratie + +Kopieer `.env.example` naar `.env` en vul de waarden in: + +```env +# WhatsApp API +WHATSAPP_API_ENDPOINT=http://localhost:3000 +WHATSAPP_SESSION=EdGpt + +# Webhook +WEBHOOK_PORT=3002 +ALLOWED_GROUP=120363407356808413@g.us +BOT_NAME=EdGpt + +# Bestanden +DIENSTEN_JSON=./diensten/dienst-tijden.json +DIENSTEN_PNG=./diensten/png + +# Admin +ADMIN_NUMBER=31620248011@c.us +``` + +### wwebjs-api webhook config + +In de `.env` van wwebjs-api: + +```env +BASE_WEBHOOK_URL=http://host.docker.internal:3002/webhook +ENABLE_LOCAL_CALLBACK_EXAMPLE=FALSE +ENABLE_WEBHOOK=TRUE +``` + +Belangrijk: gebruik **`docker compose up -d`** (niet `docker restart`) om `.env` wijzigingen door te voeren. + +## Data + +De map `diensten/` bevat: + +- `dienst-tijden.json` — array met dienstmetadata: + ```json + [ + { + "dienstnummer": "4002", + "startTime": "09:30", + "endTime": "10:30", + "paginas": [ + { "bestandsnaam": "4002-001.png" }, + { "bestandsnaam": "4002-002.png" } + ] + } + ] + ``` +- `png/` — PNG bestanden met dienstschema's (bijv. `4002-001.png`) + +Beide zijn uitgesloten van git via `.gitignore`. + +## Gebruik + +### Starten + +```bash +npm start +``` + +Of met PM2: + +```bash +npx pm2 start src/index.js --name dienst-bot +npx pm2 save +``` + +### Stoppen + +```bash +npx pm2 stop dienst-bot +``` + +### Logs bekijken + +```bash +npx pm2 logs dienst-bot +``` + +## Projectstructuur + +``` +src/ + index.js # Express webhook server + messageHandler.js # Berichtverwerking en filtering + dienstService.js # JSON lookup en PNG bestandspaden + whatsappApi.js # WhatsApp API client (bericht + afbeelding) +``` + +## Foutafhandeling + +- **Ongeldig dienstnummer**: de bot stuurt een melding terug naar de afzender +- **Fout bij verzenden PNG**: de admin krijgt een WhatsApp melding +- **Webhook fout**: wordt gelogd in PM2 logs + +## Licentie + +Privé project — niet voor distributie. diff --git a/src/messageHandler.js b/src/messageHandler.js index e947a3b..d57d9f7 100644 --- a/src/messageHandler.js +++ b/src/messageHandler.js @@ -13,8 +13,11 @@ function isBotMentioned(message) { if (message._data?.mentionedJidList?.length > 0) { return true } - if (message.body && message.body.toLowerCase().includes(`@${BOT_NAME}`)) { - 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 } @@ -32,6 +35,8 @@ async function handleWebhook(payload) { const dienstnummer = extractDienstnummer(message.body || '') if (!dienstnummer) return + console.log(`Processing dienst ${dienstnummer} for ${message.author}`) + const author = message.author if (!author) return