From f03da1c541e0fa7bf48ffc860556ed470cfd8df5 Mon Sep 17 00:00:00 2001 From: Eddy de Vink Date: Sun, 12 Jul 2026 13:46:49 +0200 Subject: [PATCH] fix: dienst-bot startup notification via Telegram, fix first health check no-op --- src/index.js | 12 ++++++++++++ src/telegramBot.js | 16 ++++++++-------- src/whatsappApi.js | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 8319573..9a398de 100644 --- a/src/index.js +++ b/src/index.js @@ -2,11 +2,22 @@ require('dotenv').config() const http = require('http') const cron = require('node-cron') const express = require('express') +const axios = require('axios') const { handleWebhook } = require('./messageHandler') const { loadDiensten } = require('./dienstService') const { resetDay, getYesterdayTotal } = require('./dailyStats') const { sendTextMessage } = require('./whatsappApi') +async function notifyTelegramOnline() { + const url = process.env.TELEGRAM_BOT_URL + if (!url) return + try { + await axios.post(`${url}/notify`, { text: 'dienst-bot online' }, { timeout: 5000 }) + } catch (err) { + console.error('Telegram startup notification failed:', err.message) + } +} + const app = express() app.use(express.json({ limit: '10mb' })) @@ -72,6 +83,7 @@ async function startServer() { console.log(`Allowed group: ${process.env.ALLOWED_GROUP}`) console.log(`Bot name: ${process.env.BOT_NAME}`) console.log('Daily summary cron job scheduled for 10:00') + notifyTelegramOnline() }) function shutdown(signal) { diff --git a/src/telegramBot.js b/src/telegramBot.js index ff27557..efccff7 100644 --- a/src/telegramBot.js +++ b/src/telegramBot.js @@ -12,8 +12,8 @@ const CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_INTERVAL_MS || '300000' const app = express() app.use(express.json()) -let dienstBotOk = true -let wwebjsOk = true +let dienstBotOk = null +let wwebjsOk = null function sendTelegram(text) { return axios.post(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, { @@ -49,7 +49,7 @@ async function checkHealth() { currentWwebjsOk = false } - if (dienstBotOk !== currentDienstBotOk) { + if (dienstBotOk !== null && dienstBotOk !== currentDienstBotOk) { const icon = currentDienstBotOk ? '✅' : '❌' const status = currentDienstBotOk ? 'weer online' : 'niet bereikbaar' try { @@ -57,10 +57,10 @@ async function checkHealth() { } catch (err) { console.error('Failed to send dienst-bot status:', err.message) } - dienstBotOk = currentDienstBotOk } + dienstBotOk = currentDienstBotOk - if (wwebjsOk !== currentWwebjsOk) { + if (wwebjsOk !== null && wwebjsOk !== currentWwebjsOk) { const icon = currentWwebjsOk ? '✅' : '❌' const status = currentWwebjsOk ? 'weer online' : 'niet bereikbaar' try { @@ -68,8 +68,8 @@ async function checkHealth() { } catch (err) { console.error('Failed to send wwebjs-api status:', err.message) } - wwebjsOk = currentWwebjsOk } + wwebjsOk = currentWwebjsOk console.log(`Health check: dienst-bot=${currentDienstBotOk ? 'UP' : 'DOWN'}, wwebjs-api=${currentWwebjsOk ? 'UP' : 'DOWN'}`) } @@ -80,7 +80,7 @@ app.post('/notify', async (req, res) => { return res.status(400).json({ success: false, error: 'missing text' }) } try { - await sendTelegram(`⚠️ ${text}`) + await sendTelegram(text) res.json({ success: true }) } catch (err) { console.error('Failed to forward notification:', err.message) @@ -94,7 +94,7 @@ app.get('/health', (req, res) => { startupNotification() setInterval(checkHealth, CHECK_INTERVAL) -checkHealth() +setTimeout(checkHealth, 10000) app.listen(PORT, () => { console.log(`telegram-bot listening on port ${PORT}`) diff --git a/src/whatsappApi.js b/src/whatsappApi.js index b4eb39d..e0e65b9 100644 --- a/src/whatsappApi.js +++ b/src/whatsappApi.js @@ -77,7 +77,7 @@ async function notifyAdmin(errorText) { try { const telegramUrl = process.env.TELEGRAM_BOT_URL if (telegramUrl) { - await axios.post(`${telegramUrl}/notify`, { text: errorText }, { timeout: 5000 }) + await axios.post(`${telegramUrl}/notify`, { text: `[dienst-bot fout] ${errorText}` }, { timeout: 5000 }) } } catch (telegramErr) { console.error('Telegram notification also failed:', telegramErr.message)