Files
Checkmate/server/src/service/infrastructure/notificationProviders/discord.ts
T

59 lines
1.8 KiB
TypeScript
Raw Normal View History

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 21:58:48 +00:00
import { buildHardwareAlerts, buildDiscordBody, getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
2026-01-19 20:12:54 +00:00
import got from "got";
2026-01-19 19:58:15 +00:00
2026-01-19 18:58:17 +00:00
export class DiscordProvider implements INotificationProvider {
2026-01-19 21:58:48 +00:00
constructor(private logger: any) {}
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 {
2026-01-19 21:58:48 +00:00
this.logger?.debug?.("Sending Discord alert", { address: notification.address });
2026-01-19 19:58:15 +00:00
await got.post(notification.address, {
json: body,
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error: any) {
2026-01-19 21:58:48 +00:00
this.logger?.error?.("Discord alert failed", { error });
2026-01-19 19:58:15 +00:00
return false;
}
2026-01-19 18:58:17 +00:00
};
sendTestAlert = async (notification: Notification) => {
2026-01-19 21:58:48 +00:00
if (!notification.address) {
return false;
}
try {
this.logger?.debug?.("Sending Discord test alert", { address: notification.address });
await got.post(notification.address, {
json: { content: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Discord test alert failed", { error });
return false;
}
2026-01-19 18:58:17 +00:00
};
}