mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-24 12:48:47 +00:00
feat(notifications): integrate custom notification handling in server threshold alerts
This commit is contained in:
@@ -2,6 +2,7 @@ import { and, eq } from "drizzle-orm";
|
|||||||
import { db } from "../../db";
|
import { db } from "../../db";
|
||||||
import { notifications } from "../../db/schema";
|
import { notifications } from "../../db/schema";
|
||||||
import {
|
import {
|
||||||
|
sendCustomNotification,
|
||||||
sendDiscordNotification,
|
sendDiscordNotification,
|
||||||
sendSlackNotification,
|
sendSlackNotification,
|
||||||
sendTelegramNotification,
|
sendTelegramNotification,
|
||||||
@@ -34,6 +35,7 @@ export const sendServerThresholdNotifications = async (
|
|||||||
discord: true,
|
discord: true,
|
||||||
telegram: true,
|
telegram: true,
|
||||||
slack: true,
|
slack: true,
|
||||||
|
custom: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -41,7 +43,7 @@ export const sendServerThresholdNotifications = async (
|
|||||||
const typeColor = 0xff0000; // Rojo para indicar alerta
|
const typeColor = 0xff0000; // Rojo para indicar alerta
|
||||||
|
|
||||||
for (const notification of notificationList) {
|
for (const notification of notificationList) {
|
||||||
const { discord, telegram, slack } = notification;
|
const { discord, telegram, slack, custom } = notification;
|
||||||
|
|
||||||
if (discord) {
|
if (discord) {
|
||||||
const decorate = (decoration: string, text: string) =>
|
const decorate = (decoration: string, text: string) =>
|
||||||
@@ -151,5 +153,20 @@ export const sendServerThresholdNotifications = async (
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (custom) {
|
||||||
|
await sendCustomNotification(custom, {
|
||||||
|
title: `Server ${payload.Type} Alert`,
|
||||||
|
message: payload.Message,
|
||||||
|
serverName: payload.ServerName,
|
||||||
|
type: payload.Type,
|
||||||
|
currentValue: payload.Value,
|
||||||
|
threshold: payload.Threshold,
|
||||||
|
timestamp: date.toISOString(),
|
||||||
|
date: date.toLocaleString(),
|
||||||
|
status: "alert",
|
||||||
|
alertType: "server-threshold",
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type {
|
import type {
|
||||||
|
custom,
|
||||||
discord,
|
discord,
|
||||||
email,
|
email,
|
||||||
gotify,
|
gotify,
|
||||||
@@ -11,7 +12,7 @@ import nodemailer from "nodemailer";
|
|||||||
export const sendEmailNotification = async (
|
export const sendEmailNotification = async (
|
||||||
connection: typeof email.$inferInsert,
|
connection: typeof email.$inferInsert,
|
||||||
subject: string,
|
subject: string,
|
||||||
htmlContent: string,
|
htmlContent: string
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
@@ -41,7 +42,7 @@ export const sendEmailNotification = async (
|
|||||||
|
|
||||||
export const sendDiscordNotification = async (
|
export const sendDiscordNotification = async (
|
||||||
connection: typeof discord.$inferInsert,
|
connection: typeof discord.$inferInsert,
|
||||||
embed: any,
|
embed: any
|
||||||
) => {
|
) => {
|
||||||
// try {
|
// try {
|
||||||
await fetch(connection.webhookUrl, {
|
await fetch(connection.webhookUrl, {
|
||||||
@@ -60,7 +61,7 @@ export const sendTelegramNotification = async (
|
|||||||
inlineButton?: {
|
inlineButton?: {
|
||||||
text: string;
|
text: string;
|
||||||
url: string;
|
url: string;
|
||||||
}[][],
|
}[][]
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const url = `https://api.telegram.org/bot${connection.botToken}/sendMessage`;
|
const url = `https://api.telegram.org/bot${connection.botToken}/sendMessage`;
|
||||||
@@ -85,7 +86,7 @@ export const sendTelegramNotification = async (
|
|||||||
|
|
||||||
export const sendSlackNotification = async (
|
export const sendSlackNotification = async (
|
||||||
connection: typeof slack.$inferInsert,
|
connection: typeof slack.$inferInsert,
|
||||||
message: any,
|
message: any
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
await fetch(connection.webhookUrl, {
|
await fetch(connection.webhookUrl, {
|
||||||
@@ -101,7 +102,7 @@ export const sendSlackNotification = async (
|
|||||||
export const sendGotifyNotification = async (
|
export const sendGotifyNotification = async (
|
||||||
connection: typeof gotify.$inferInsert,
|
connection: typeof gotify.$inferInsert,
|
||||||
title: string,
|
title: string,
|
||||||
message: string,
|
message: string
|
||||||
) => {
|
) => {
|
||||||
const response = await fetch(`${connection.serverUrl}/message`, {
|
const response = await fetch(`${connection.serverUrl}/message`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -123,7 +124,7 @@ export const sendGotifyNotification = async (
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to send Gotify notification: ${response.statusText}`,
|
`Failed to send Gotify notification: ${response.statusText}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -133,7 +134,7 @@ export const sendNtfyNotification = async (
|
|||||||
title: string,
|
title: string,
|
||||||
tags: string,
|
tags: string,
|
||||||
actions: string,
|
actions: string,
|
||||||
message: string,
|
message: string
|
||||||
) => {
|
) => {
|
||||||
const response = await fetch(`${connection.serverUrl}/${connection.topic}`, {
|
const response = await fetch(`${connection.serverUrl}/${connection.topic}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -151,3 +152,42 @@ export const sendNtfyNotification = async (
|
|||||||
throw new Error(`Failed to send ntfy notification: ${response.statusText}`);
|
throw new Error(`Failed to send ntfy notification: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const sendCustomNotification = async (
|
||||||
|
connection: typeof custom.$inferInsert,
|
||||||
|
payload: Record<string, any>
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
// Parse headers if provided
|
||||||
|
let headers: Record<string, string> = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
};
|
||||||
|
if (connection.headers) {
|
||||||
|
try {
|
||||||
|
headers = { ...headers, ...JSON.parse(connection.headers) };
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error parsing headers:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default body with payload
|
||||||
|
const body = JSON.stringify(payload);
|
||||||
|
|
||||||
|
const response = await fetch(connection.endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(
|
||||||
|
`Failed to send custom notification: ${response.statusText}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error sending custom notification:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user