add webhook
This commit is contained in:
1 parent
d2d75cd035
commit
e18681606c
4 files changed
+132
-21
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import type { Monitor, Notification, Alert, MonitorStatusResponse } from "@/types/index.js";
|
||||
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
|
||||
import type { NotificationMessage } from "@/types/notificationMessage.js";
|
||||
|
||||
export interface INotificationProvider {
|
||||
sendAlert: (
|
||||
@@ -9,5 +10,6 @@ export interface INotificationProvider {
|
||||
decision: MonitorActionDecision,
|
||||
clientHost: string
|
||||
) => Promise<boolean>;
|
||||
sendMessage?: (notification: Notification, message: NotificationMessage) => Promise<boolean>;
|
||||
sendTestAlert(notification: Notification): Promise<boolean>;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ const SERVICE_NAME = "WebhookProvider";
|
||||
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
|
||||
import { INotificationProvider } from "@/service/index.js";
|
||||
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
|
||||
import type { NotificationMessage } from "@/types/notificationMessage.js";
|
||||
import {
|
||||
buildHardwareAlerts,
|
||||
buildHardwareNotificationMessage,
|
||||
@@ -75,6 +76,98 @@ export class WebhookProvider implements INotificationProvider {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* New unified message format - builds webhook payload from NotificationMessage
|
||||
*/
|
||||
sendMessage = async (notification: Notification, message: NotificationMessage): Promise<boolean> => {
|
||||
if (!notification.address) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build webhook payload from unified message
|
||||
const payload = this.buildWebhookPayload(message);
|
||||
|
||||
try {
|
||||
await got.post(notification.address, {
|
||||
json: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
this.logger.info({
|
||||
message: "[NEW] Webhook notification sent via sendMessage",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendMessage",
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
this.logger.warn({
|
||||
message: "[NEW] Webhook alert failed via sendMessage",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendMessage",
|
||||
stack: err?.stack,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Build webhook payload from NotificationMessage
|
||||
* Format: { text: string, severity: string, monitor: object, details: object }
|
||||
*/
|
||||
private buildWebhookPayload(message: NotificationMessage): object {
|
||||
const lines: string[] = [];
|
||||
|
||||
// Title and summary
|
||||
lines.push(`**${message.content.title}**`);
|
||||
lines.push(message.content.summary);
|
||||
lines.push("");
|
||||
|
||||
// Monitor information
|
||||
lines.push("**Monitor Details:**");
|
||||
lines.push(`- Name: ${message.monitor.name}`);
|
||||
lines.push(`- URL: ${message.monitor.url}`);
|
||||
lines.push(`- Type: ${message.monitor.type}`);
|
||||
lines.push(`- Status: ${message.monitor.status}`);
|
||||
lines.push("");
|
||||
|
||||
// Additional details
|
||||
if (message.content.details && message.content.details.length > 0) {
|
||||
lines.push("**Additional Information:**");
|
||||
message.content.details.forEach((detail) => lines.push(`- ${detail}`));
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
// Threshold breaches (for hardware monitors)
|
||||
if (message.content.thresholds && message.content.thresholds.length > 0) {
|
||||
lines.push("**Threshold Breaches:**");
|
||||
message.content.thresholds.forEach((breach) => {
|
||||
lines.push(`- ${breach.metric.toUpperCase()}: ${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`);
|
||||
});
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
// Incident link
|
||||
if (message.content.incident) {
|
||||
lines.push(`[View Incident](${message.clientHost}/infrastructure/${message.monitor.id})`);
|
||||
}
|
||||
|
||||
// Return webhook payload with both text and structured data
|
||||
return {
|
||||
text: lines.join("\n"),
|
||||
severity: message.severity,
|
||||
type: message.type,
|
||||
monitor: {
|
||||
id: message.monitor.id,
|
||||
name: message.monitor.name,
|
||||
url: message.monitor.url,
|
||||
status: message.monitor.status,
|
||||
},
|
||||
timestamp: message.content.timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
sendTestAlert = async (notification: Notification) => {
|
||||
if (!notification.address) {
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user