fix: dienst-bot startup notification via Telegram, fix first health check no-op
This commit is contained in:
parent
92289adc03
commit
f03da1c541
3 changed files with 21 additions and 9 deletions
12
src/index.js
12
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) {
|
||||
|
|
|
|||
|
|
@ -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}`)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue