53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { getNotificationSettings, NotificationSettings } from './appDb'
|
|
|
|
export async function sendStatusAlert(
|
|
siteName: string,
|
|
siteUrl: string,
|
|
oldStatus: string,
|
|
newStatus: string,
|
|
ms: number | null
|
|
) {
|
|
let settings: NotificationSettings | null
|
|
try {
|
|
settings = await getNotificationSettings()
|
|
} catch {
|
|
return
|
|
}
|
|
if (!settings || !settings.enabled) return
|
|
|
|
const isDown = newStatus !== 'up'
|
|
const emoji = newStatus === 'up' ? '✅' : newStatus === 'degraded' ? '⚠️' : '🔴'
|
|
const statusLabel = newStatus === 'up' ? 'Düzeldi (UP)' : newStatus === 'degraded' ? 'Yavaş (DEGRADED)' : 'Çöktü (DOWN)'
|
|
const msText = ms ? ` — ${ms}ms` : ''
|
|
|
|
const text = `${emoji} *${siteName}*\nDurum: ${statusLabel}${msText}\nURL: ${siteUrl}`
|
|
|
|
if (settings.webhook_url) {
|
|
fetch(settings.webhook_url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
text,
|
|
site: siteName,
|
|
url: siteUrl,
|
|
status: newStatus,
|
|
previous_status: oldStatus,
|
|
ms,
|
|
timestamp: new Date().toISOString(),
|
|
}),
|
|
}).catch(() => {})
|
|
}
|
|
|
|
if (settings.telegram_token && settings.telegram_chat_id) {
|
|
fetch(`https://api.telegram.org/bot${settings.telegram_token}/sendMessage`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
chat_id: settings.telegram_chat_id,
|
|
text: `🔔 *VPS Panel*\n\n${text}`,
|
|
parse_mode: 'Markdown',
|
|
}),
|
|
}).catch(() => {})
|
|
}
|
|
}
|