discord
This commit is contained in:
1 parent
fd33b903ed
commit
be82a7ee2d
3 files changed
+76
-16
No files matched your search
@@ -1,8 +1,40 @@
|
|||||||
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 got, { HTTPError } from "got";
|
||||||
|
|
||||||
export class DiscordProvider implements INotificationProvider {
|
export class DiscordProvider implements INotificationProvider {
|
||||||
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
|
const { discordPayload } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||||
|
return discordPayload;
|
||||||
|
};
|
||||||
|
|
||||||
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
sendAlert = async (notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
return false;
|
let body;
|
||||||
|
if (monitor.type === "hardware") {
|
||||||
|
body = this.getHardwareContent(monitor, monitorStatusResponse);
|
||||||
|
} else {
|
||||||
|
body = buildDiscordBody(monitor, monitorStatusResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!notification.address) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log({ body });
|
||||||
|
|
||||||
|
try {
|
||||||
|
await got.post(notification.address, {
|
||||||
|
json: body,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} catch (error: any) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
sendTestAlert = async (notification: Notification) => {
|
sendTestAlert = async (notification: Notification) => {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -1,5 +1,23 @@
|
|||||||
import { Monitor, HardwareStatusPayload, MonitorStatusResponse } from "@/types/index.js";
|
import { Monitor, HardwareStatusPayload, MonitorStatusResponse } from "@/types/index.js";
|
||||||
|
|
||||||
|
const getTime = (): { localTimeZone: string; localTime: string; utcTime: string } => {
|
||||||
|
const now = new Date();
|
||||||
|
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
const localTime = new Intl.DateTimeFormat(undefined, {
|
||||||
|
year: "numeric",
|
||||||
|
month: "2-digit",
|
||||||
|
day: "2-digit",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
second: "2-digit",
|
||||||
|
hour12: false,
|
||||||
|
timeZone: localTimeZone,
|
||||||
|
timeZoneName: "short",
|
||||||
|
}).format(now);
|
||||||
|
const utcTime = now.toUTCString();
|
||||||
|
return { localTimeZone, localTime, utcTime };
|
||||||
|
};
|
||||||
|
|
||||||
export const buildHardwareAlerts = (
|
export const buildHardwareAlerts = (
|
||||||
clientHost: string,
|
clientHost: string,
|
||||||
monitor: Monitor,
|
monitor: Monitor,
|
||||||
@@ -134,20 +152,7 @@ export const shouldSendHardwareAlert = (monitor: Monitor, networkResponse: Monit
|
|||||||
|
|
||||||
export const buildWebhookBody = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
export const buildWebhookBody = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
const { status, code } = monitorStatusResponse;
|
const { status, code } = monitorStatusResponse;
|
||||||
const now = new Date();
|
const { localTimeZone, localTime, utcTime } = getTime();
|
||||||
const localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
||||||
const localTime = new Intl.DateTimeFormat(undefined, {
|
|
||||||
year: "numeric",
|
|
||||||
month: "2-digit",
|
|
||||||
day: "2-digit",
|
|
||||||
hour: "2-digit",
|
|
||||||
minute: "2-digit",
|
|
||||||
second: "2-digit",
|
|
||||||
hour12: false,
|
|
||||||
timeZone: localTimeZone,
|
|
||||||
timeZoneName: "short",
|
|
||||||
}).format(now);
|
|
||||||
const utcTime = now.toUTCString();
|
|
||||||
return `Monitor: ${monitor.name}\nLocal Time (${localTimeZone}): ${localTime}\nUTC Time: ${utcTime}\nStatus: ${status ? "UP" : "DOWN"}\nStatus Code: ${code}\n\u200B\n`;
|
return `Monitor: ${monitor.name}\nLocal Time (${localTimeZone}): ${localTime}\nUTC Time: ${utcTime}\nStatus: ${status ? "UP" : "DOWN"}\nStatus Code: ${code}\n\u200B\n`;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -165,3 +170,25 @@ export const buildEmail = async (emailService: any, monitor: Monitor): Promise<s
|
|||||||
console.log({ html: typeof html });
|
console.log({ html: typeof html });
|
||||||
return html;
|
return html;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const buildDiscordBody = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
|
const { localTime, utcTime } = getTime();
|
||||||
|
let body = {
|
||||||
|
embeds: [
|
||||||
|
{
|
||||||
|
title: `Monitor ${monitor.name}`,
|
||||||
|
color: monitor.status ? 5763719 : 15548997,
|
||||||
|
|
||||||
|
fields: [
|
||||||
|
{ name: "Monitor", value: monitor.name, inline: true },
|
||||||
|
{ name: "Status", value: monitor.status ? "Up" : "Down", inline: true },
|
||||||
|
{ name: "Status Code", value: String(monitorStatusResponse.code), inline: true },
|
||||||
|
{ name: "Time", value: `${localTime} (Local) / ${utcTime} (UTC)`, inline: true },
|
||||||
|
{ name: "URL", value: monitor.url, inline: false },
|
||||||
|
],
|
||||||
|
footer: { text: "Checkmate" },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
return body;
|
||||||
|
};
|
||||||
@@ -15,7 +15,8 @@ export class WebhookProvider implements INotificationProvider {
|
|||||||
return body;
|
return body;
|
||||||
};
|
};
|
||||||
|
|
||||||
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