Add basic webhook option to workerConfig.notification as alternative to apprise

pull/143/head
Steven Crocker 2025-09-25 21:40:42 -05:00
parent 65408d4962
commit 9705d6cd91
2 changed files with 57 additions and 23 deletions

View File

@ -54,7 +54,15 @@ export type Notification = {
recipientUrl?: string
timeZone?: string
gracePeriod?: number
skipNotificationIds?: string[]
skipNotificationIds?: string[],
webhook?: Webhook
}
export type Webhook = {
url: string,
method?: 'POST' | 'GET',
headers?: { [key: string]: string | number },
timeout?: number
}
export type Callbacks<TEnv = Env> = {

View File

@ -1,8 +1,8 @@
import { workerConfig, maintenances } from '../../uptime.config'
import { formatStatusChangeNotification, getWorkerLocation, notifyWithApprise } from './util'
import { MonitorState, MonitorTarget } from '../../types/config'
import { getStatus } from './monitor'
import { DurableObject } from 'cloudflare:workers'
import { MonitorState, MonitorTarget } from '../../types/config'
import { maintenances, workerConfig } from '../../uptime.config'
import { getStatus } from './monitor'
import { formatStatusChangeNotification, getWorkerLocation, notifyWithApprise } from './util'
export interface Env {
UPTIMEFLARE_STATE: KVNamespace
@ -46,6 +46,31 @@ const Worker = {
return
}
if(workerConfig.notification?.webhook?.url) {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), workerConfig.notification.webhook.timeout ?? 30000) // 30 second default timeout
const headers = { 'Content-Type': 'application/json' }
if(workerConfig.notification.webhook.headers) {
Object.assign(headers, workerConfig.notification.webhook.headers)
}
try {
await fetch(workerConfig.notification.webhook.url, {
method: workerConfig.notification.webhook.method ?? 'POST',
headers,
body: JSON.stringify({
event: 'status_change',
monitor,
isUp,
timeIncidentStart,
timeNow,
reason,
}),
signal: controller.signal,
})
} finally {
clearTimeout(timeoutId)
}
} else {
if (workerConfig.notification?.appriseApiServer && workerConfig.notification?.recipientUrl) {
const notification = formatStatusChangeNotification(
monitor,
@ -67,6 +92,7 @@ const Worker = {
)
}
}
}
// Read state, set init state if it doesn't exist
let state =