Add basic webhook option to workerConfig.notification as alternative to apprise
parent
65408d4962
commit
9705d6cd91
|
|
@ -54,7 +54,15 @@ export type Notification = {
|
||||||
recipientUrl?: string
|
recipientUrl?: string
|
||||||
timeZone?: string
|
timeZone?: string
|
||||||
gracePeriod?: number
|
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> = {
|
export type Callbacks<TEnv = Env> = {
|
||||||
|
|
|
||||||
|
|
@ -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 { 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 {
|
export interface Env {
|
||||||
UPTIMEFLARE_STATE: KVNamespace
|
UPTIMEFLARE_STATE: KVNamespace
|
||||||
|
|
@ -46,6 +46,31 @@ const Worker = {
|
||||||
return
|
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) {
|
if (workerConfig.notification?.appriseApiServer && workerConfig.notification?.recipientUrl) {
|
||||||
const notification = formatStatusChangeNotification(
|
const notification = formatStatusChangeNotification(
|
||||||
monitor,
|
monitor,
|
||||||
|
|
@ -67,6 +92,7 @@ const Worker = {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read state, set init state if it doesn't exist
|
// Read state, set init state if it doesn't exist
|
||||||
let state =
|
let state =
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue