docs: add Telegram watchdog to README, update project structure and error handling

This commit is contained in:
Eddy de Vink 2026-07-12 13:50:21 +02:00
commit 9bb5849969

View file

@ -18,6 +18,7 @@ De bot gebruikt een **speciale groep** (Dienst opvragen EdGPT-EBS) waarin **elk
- [Node.js](https://nodejs.org/) 18+
- [wwebjs-api](https://github.com/pedroslopez/whatsapp-web.js) Docker container op nextcloud (poort 3001)
- WhatsApp sessie "EdGpt" actief in wwebjs-api
- Telegram bot token (voor watchdog, optioneel)
## Installatie
@ -46,6 +47,11 @@ DIENSTEN_PNG=./diensten/png
# Admin
ADMIN_NUMBER=31620248011@c.us
# Telegram watchdog (optioneel, backup kanaal)
TELEGRAM_BOT_URL=http://localhost:3004
TELEGRAM_BOT_TOKEN=
TELEGRAM_ADMIN_CHAT_ID=
```
### wwebjs-api webhook config
@ -115,6 +121,7 @@ src/
dienstService.js # JSON lookup en PNG bestandspaden
whatsappApi.js # WhatsApp API client (bericht + afbeelding)
pinnedMessageService.js # Vastgezet welkomstbericht beheer
telegramBot.js # Telegram watchdog (apart process, health checks)
```
## Welkomstbericht
@ -128,10 +135,11 @@ Bij elke interactie met de groep controleert de bot of het pinned bericht nog be
## Foutafhandeling
- **Ongeldig dienstnummer**: de bot stuurt een melding terug naar de afzender
- **Fout bij verzenden tekst/PNG**: maximaal 3 pogingen met exponential backoff (1s, 2s, 4s) voor 5xx HTTP-fouten en connectieproblemen (ECONNRESET, ECONNREFUSED, ETIMEDOUT). Bij definitieve fout krijgt de admin een WhatsApp-melding.
- **Fout bij verzenden tekst/PNG**: maximaal 3 pogingen met exponential backoff (1s, 2s, 4s) voor 5xx HTTP-fouten en connectieproblemen (ECONNRESET, ECONNREFUSED, ETIMEDOUT). Bij definitieve fout krijgt de admin een melding.
- **Admin meldingen**: eerst via WhatsApp (`ADMIN_NUMBER`), bij falen (WhatsApp API down) via Telegram watchdog
- **EADDRINUSE (poort 3002 in gebruik)**: de server wacht 5 seconden en herhaalt de bind-poging, met `SO_REUSEADDR` om TIME_WAIT te omzeilen
- **Graceful shutdown**: de server handelt SIGTERM en SIGINT af door actieve connecties netjes te sluiten (10s timeout)
- **Restart-loop preventie**: de LaunchDaemon heeft `ThrottleInterval=10` — maximaal 1 herstart per 10 seconden
- **Restart-loop preventie**: systemd `Restart=always` met `RestartSec=5`
- **Webhook fout**: wordt gelogd in de logs
## Spam protectie
@ -148,6 +156,42 @@ Elke dag om **10:00** stuurt de bot een WhatsApp-bericht naar `ADMIN_NUMBER` met
De teller wordt bijgehouden in memory door `dailyStats.js` en gereset na verzending van de samenvatting. De scheduling gebruikt `node-cron` (`0 10 * * *`).
## Telegram Watchdog
Sinds juli 2026 draait er een apart **telegram-bot** proces (`src/telegramBot.js`) als backup communicatiekanaal. Dit proces draait als eigen systemd service (`telegram-bot.service`) op nextcloud.
### Wat het doet
- **Startup**: stuurt "online" melding naar admin via Telegram
- **Health checks** (elke 5 min): checkt dienst-bot (`/health`) en wwebjs-api (`/session/status`)
- **Statusovergangen**: alleen bij up→down of down→up wordt een notificatie gestuurd (geen spam)
- **Fallback**: als `notifyAdmin()` in de dienst-bot faalt (WhatsApp API onbereikbaar), wordt het bericht doorgestuurd naar Telegram
### Configuratie
| Variabele | Doel |
|-----------|------|
| `TELEGRAM_BOT_TOKEN` | Token van @BotFather |
| `TELEGRAM_ADMIN_CHAT_ID` | Jouw Telegram chat-ID |
| `TELEGRAM_BOT_PORT=3004` | Poort voor intern `/notify` endpoint |
| `DIENST_BOT_HEALTH_URL` | URL voor dienst-bot health check |
| `WWEBJS_API_SESSION_URL` | URL voor wwebjs-api session status |
| `HEALTH_CHECK_INTERVAL_MS=300000` | Interval tussen health checks |
| `TELEGRAM_BOT_URL` | (in dienst-bot .env) URL voor Telegram fallback |
### Commando's
```bash
# Status
ssh aiaiaiaiagent@nextcloud "sudo -A systemctl status telegram-bot"
# Herstarten
ssh aiaiaiaiagent@nextcloud "sudo -A systemctl restart telegram-bot"
# Logs
ssh aiaiaiaiagent@nextcloud "sudo -A journalctl -u telegram-bot -n 50 -f"
```
## Licentie
Public Domain