fix: dienst-bot startup notification via Telegram, fix first health check no-op

This commit is contained in:
Eddy de Vink 2026-07-12 13:46:49 +02:00
commit f03da1c541
3 changed files with 21 additions and 9 deletions

View file

@ -2,11 +2,22 @@ require('dotenv').config()
const http = require('http') const http = require('http')
const cron = require('node-cron') const cron = require('node-cron')
const express = require('express') const express = require('express')
const axios = require('axios')
const { handleWebhook } = require('./messageHandler') const { handleWebhook } = require('./messageHandler')
const { loadDiensten } = require('./dienstService') const { loadDiensten } = require('./dienstService')
const { resetDay, getYesterdayTotal } = require('./dailyStats') const { resetDay, getYesterdayTotal } = require('./dailyStats')
const { sendTextMessage } = require('./whatsappApi') 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() const app = express()
app.use(express.json({ limit: '10mb' })) app.use(express.json({ limit: '10mb' }))
@ -72,6 +83,7 @@ async function startServer() {
console.log(`Allowed group: ${process.env.ALLOWED_GROUP}`) console.log(`Allowed group: ${process.env.ALLOWED_GROUP}`)
console.log(`Bot name: ${process.env.BOT_NAME}`) console.log(`Bot name: ${process.env.BOT_NAME}`)
console.log('Daily summary cron job scheduled for 10:00') console.log('Daily summary cron job scheduled for 10:00')
notifyTelegramOnline()
}) })
function shutdown(signal) { function shutdown(signal) {

View file

@ -12,8 +12,8 @@ const CHECK_INTERVAL = parseInt(process.env.HEALTH_CHECK_INTERVAL_MS || '300000'
const app = express() const app = express()
app.use(express.json()) app.use(express.json())
let dienstBotOk = true let dienstBotOk = null
let wwebjsOk = true let wwebjsOk = null
function sendTelegram(text) { function sendTelegram(text) {
return axios.post(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, { return axios.post(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
@ -49,7 +49,7 @@ async function checkHealth() {
currentWwebjsOk = false currentWwebjsOk = false
} }
if (dienstBotOk !== currentDienstBotOk) { if (dienstBotOk !== null && dienstBotOk !== currentDienstBotOk) {
const icon = currentDienstBotOk ? '✅' : '❌' const icon = currentDienstBotOk ? '✅' : '❌'
const status = currentDienstBotOk ? 'weer online' : 'niet bereikbaar' const status = currentDienstBotOk ? 'weer online' : 'niet bereikbaar'
try { try {
@ -57,10 +57,10 @@ async function checkHealth() {
} catch (err) { } catch (err) {
console.error('Failed to send dienst-bot status:', err.message) 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 icon = currentWwebjsOk ? '✅' : '❌'
const status = currentWwebjsOk ? 'weer online' : 'niet bereikbaar' const status = currentWwebjsOk ? 'weer online' : 'niet bereikbaar'
try { try {
@ -68,8 +68,8 @@ async function checkHealth() {
} catch (err) { } catch (err) {
console.error('Failed to send wwebjs-api status:', err.message) 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'}`) 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' }) return res.status(400).json({ success: false, error: 'missing text' })
} }
try { try {
await sendTelegram(`⚠️ ${text}`) await sendTelegram(text)
res.json({ success: true }) res.json({ success: true })
} catch (err) { } catch (err) {
console.error('Failed to forward notification:', err.message) console.error('Failed to forward notification:', err.message)
@ -94,7 +94,7 @@ app.get('/health', (req, res) => {
startupNotification() startupNotification()
setInterval(checkHealth, CHECK_INTERVAL) setInterval(checkHealth, CHECK_INTERVAL)
checkHealth() setTimeout(checkHealth, 10000)
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`telegram-bot listening on port ${PORT}`) console.log(`telegram-bot listening on port ${PORT}`)

View file

@ -77,7 +77,7 @@ async function notifyAdmin(errorText) {
try { try {
const telegramUrl = process.env.TELEGRAM_BOT_URL const telegramUrl = process.env.TELEGRAM_BOT_URL
if (telegramUrl) { 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) { } catch (telegramErr) {
console.error('Telegram notification also failed:', telegramErr.message) console.error('Telegram notification also failed:', telegramErr.message)