2026-01-19 18:58:17 +00:00
|
|
|
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
|
|
|
|
import { INotificationProvider } from "@/service/index.js";
|
2026-01-19 19:58:15 +00:00
|
|
|
import { buildHardwareAlerts, buildDiscordBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
|
|
|
|
import got, { HTTPError } from "got";
|
|
|
|
|
|
2026-01-19 18:58:17 +00:00
|
|
|
export class DiscordProvider implements INotificationProvider {
|
2026-01-19 19:58:15 +00:00
|
|
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
|
|
|
|
const { discordPayload } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
|
|
|
|
return discordPayload;
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-19 18:58:17 +00:00
|
|
|
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
2026-01-19 19:58:15 +00:00
|
|
|
let body;
|
|
|
|
|
if (monitor.type === "hardware") {
|
|
|
|
|
body = this.getHardwareContent(monitor, monitorStatusResponse);
|
|
|
|
|
} else {
|
|
|
|
|
body = buildDiscordBody(monitor, monitorStatusResponse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!notification.address) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await got.post(notification.address, {
|
|
|
|
|
json: body,
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
responseType: "json",
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-01-19 18:58:17 +00:00
|
|
|
};
|
|
|
|
|
sendTestAlert = async (notification: Notification) => {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
}
|