slack
This commit is contained in:
1 parent
9006707e57
commit
1d36f40caf
4 files changed
+39
-7
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import { INotificationProvider } from "@/service/index.js";
|
import { INotificationProvider } from "@/service/index.js";
|
||||||
import { buildHardwareAlerts, buildDiscordBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
import { buildHardwareAlerts, buildDiscordBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
||||||
import got, { HTTPError } from "got";
|
import got from "got";
|
||||||
|
|
||||||
export class DiscordProvider implements INotificationProvider {
|
export class DiscordProvider implements INotificationProvider {
|
||||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
|
|||||||
@@ -1,9 +1,44 @@
|
|||||||
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import { INotificationProvider } from "@/service/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 {
|
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> {
|
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> {
|
async sendTestAlert(notification: Notification): Promise<boolean> {
|
||||||
|
|||||||
@@ -118,10 +118,9 @@ export const buildHardwareNotificationMessage = (clientHost: string, alerts: any
|
|||||||
return alertText.map((alert) => alert).join("\n");
|
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 content = alerts.map((alert) => alert).join("\n");
|
||||||
const body = { text: content, name: monitor.name, url: monitor.url };
|
return content;
|
||||||
return body;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const shouldSendHardwareAlert = (monitor: Monitor, networkResponse: MonitorStatusResponse): boolean => {
|
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 template = monitor.status === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
||||||
const context = { monitor: monitor.name, url: monitor.url };
|
const context = { monitor: monitor.name, url: monitor.url };
|
||||||
const html = await emailService.buildEmail(template, context);
|
const html = await emailService.buildEmail(template, context);
|
||||||
console.log({ html: typeof html });
|
|
||||||
return html;
|
return html;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ export class WebhookProvider implements INotificationProvider {
|
|||||||
};
|
};
|
||||||
|
|
||||||
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
console.log("WebhookProvider.sendAlert called");
|
|
||||||
let body;
|
let body;
|
||||||
if (monitor.type === "hardware") {
|
if (monitor.type === "hardware") {
|
||||||
body = this.getHardwareContent(monitor, monitorStatusResponse);
|
body = this.getHardwareContent(monitor, monitorStatusResponse);
|
||||||
|
|||||||
Reference in new issue
Block a user