2026-01-19 22:16:33 +00:00
|
|
|
const SERVICE_NAME = "WebhookProvider";
|
2026-01-18 16:59:38 +00:00
|
|
|
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
|
|
|
|
|
import { INotificationProvider } from "@/service/index.js";
|
2026-01-19 21:58:48 +00:00
|
|
|
import {
|
|
|
|
|
buildHardwareAlerts,
|
|
|
|
|
buildHardwareWebhookBody,
|
|
|
|
|
buildWebhookBody,
|
|
|
|
|
getTestMessage,
|
|
|
|
|
} from "@/service/infrastructure/notificationProviders/utils.js";
|
2026-01-19 18:58:17 +00:00
|
|
|
import got from "got";
|
|
|
|
|
|
2026-01-18 16:59:38 +00:00
|
|
|
export class WebhookProvider implements INotificationProvider {
|
2026-01-19 22:16:33 +00:00
|
|
|
private logger: any;
|
|
|
|
|
|
|
|
|
|
constructor(logger: any) {
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
2026-01-19 18:58:17 +00:00
|
|
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
|
|
|
|
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
|
|
|
|
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
|
|
|
|
return body;
|
2026-01-18 16:59:38 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-19 21:58:48 +00:00
|
|
|
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): string => {
|
2026-01-19 18:58:17 +00:00
|
|
|
const body = buildWebhookBody(monitor, monitorStatusResponse);
|
|
|
|
|
return body;
|
2026-01-18 16:59:38 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-19 19:58:15 +00:00
|
|
|
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
2026-01-19 18:58:17 +00:00
|
|
|
let body;
|
|
|
|
|
if (monitor.type === "hardware") {
|
|
|
|
|
body = this.getHardwareContent(monitor, monitorStatusResponse);
|
|
|
|
|
} else {
|
|
|
|
|
body = this.getContent(monitor, monitorStatusResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!notification.address) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await got.post(notification.address, {
|
2026-01-19 21:58:48 +00:00
|
|
|
json: { text: body },
|
2026-01-19 18:58:17 +00:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
2026-01-19 22:16:33 +00:00
|
|
|
const err = error as Error;
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Webhook alert failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendAlert",
|
|
|
|
|
stack: err?.stack,
|
|
|
|
|
});
|
2026-01-19 18:58:17 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sendTestAlert = async (notification: Notification) => {
|
2026-01-19 21:58:48 +00:00
|
|
|
if (!notification.address) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await got.post(notification.address, {
|
|
|
|
|
json: { text: getTestMessage() },
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
2026-01-19 22:16:33 +00:00
|
|
|
const err = error as Error;
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Webhook test alert failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendTestAlert",
|
|
|
|
|
stack: err?.stack,
|
|
|
|
|
});
|
2026-01-19 21:58:48 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-01-19 18:58:17 +00:00
|
|
|
};
|
2026-01-18 16:59:38 +00:00
|
|
|
}
|