This commit is contained in:
Alex Holliday
2026-01-19 20:12:54 +00:00
parent 9006707e57
commit 1d36f40caf
4 changed files with 39 additions and 7 deletions
@@ -1,7 +1,7 @@
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import { buildHardwareAlerts, buildDiscordBody } from "@/service/infrastructure/notificationProviders/utils.js";
import got, { HTTPError } from "got";
import got from "got";
export class DiscordProvider implements INotificationProvider {
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
@@ -1,9 +1,44 @@
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody } from "@/service/infrastructure/notificationProviders/utils.js";
import got from "got";
export class SlackProvider implements INotificationProvider {
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
return body;
};
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const body = buildWebhookBody(monitor, monitorStatusResponse);
return body;
};
async sendAlert(notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): Promise<boolean> {
return false;
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, {
json: { text: body },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
return false;
}
}
async sendTestAlert(notification: Notification): Promise<boolean> {
@@ -118,10 +118,9 @@ export const buildHardwareNotificationMessage = (clientHost: string, alerts: any
return alertText.map((alert) => alert).join("\n");
};
export const buildHardwareWebhookBody = (alerts: string[], monitor: Monitor): { text: string; name: string; url: string } => {
export const buildHardwareWebhookBody = (alerts: string[], monitor: Monitor): string => {
const content = alerts.map((alert) => alert).join("\n");
const body = { text: content, name: monitor.name, url: monitor.url };
return body;
return content;
};
export const shouldSendHardwareAlert = (monitor: Monitor, networkResponse: MonitorStatusResponse): boolean => {
@@ -167,7 +166,6 @@ export const buildEmail = async (emailService: any, monitor: Monitor): Promise<s
const template = monitor.status === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
const context = { monitor: monitor.name, url: monitor.url };
const html = await emailService.buildEmail(template, context);
console.log({ html: typeof html });
return html;
};
@@ -16,7 +16,6 @@ export class WebhookProvider implements INotificationProvider {
};
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
console.log("WebhookProvider.sendAlert called");
let body;
if (monitor.type === "hardware") {
body = this.getHardwareContent(monitor, monitorStatusResponse);