2026-05-27 16:57:11 +01:00
|
|
|
const SERVICE_NAME = "NtfyProvider";
|
2026-06-09 12:34:15 -07:00
|
|
|
import type { Notification } from "@/domain/notifications/notification.type.js";
|
|
|
|
|
import { NotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
|
|
|
|
import type { NotificationMessage } from "@/domain/notifications/notification.type.js";
|
|
|
|
|
import { getTestMessage } from "@/domain/notifications/providers/utils.js";
|
2026-05-27 16:57:11 +01:00
|
|
|
import got from "got";
|
|
|
|
|
|
|
|
|
|
export class NtfyProvider extends NotificationProvider {
|
|
|
|
|
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
|
|
|
|
|
if (!notification.address || !notification.topic) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 14:01:06 +03:00
|
|
|
const authHeaders = this.buildAuthHeaders(notification, "sendTestAlert");
|
|
|
|
|
if (authHeaders === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 16:57:11 +01:00
|
|
|
try {
|
|
|
|
|
await got.post(this.buildTopicUrl(notification.address, notification.topic), {
|
|
|
|
|
body: getTestMessage(),
|
|
|
|
|
headers: {
|
|
|
|
|
Title: "Checkmate test notification",
|
|
|
|
|
Priority: "default",
|
2026-08-14 14:01:06 +03:00
|
|
|
...authHeaders,
|
2026-05-27 16:57:11 +01:00
|
|
|
},
|
|
|
|
|
...this.gotRequestOptions(),
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const err = error as Error;
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "ntfy test alert failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendTestAlert",
|
|
|
|
|
stack: err?.stack,
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> {
|
|
|
|
|
if (!notification.address || !notification.topic) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 14:01:06 +03:00
|
|
|
const authHeaders = this.buildAuthHeaders(notification, "sendMessage");
|
|
|
|
|
if (authHeaders === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 16:57:11 +01:00
|
|
|
try {
|
|
|
|
|
await got.post(this.buildTopicUrl(notification.address, notification.topic), {
|
|
|
|
|
body: this.buildNtfyText(message),
|
|
|
|
|
headers: {
|
|
|
|
|
Title: message.content.title,
|
|
|
|
|
Priority: this.mapPriority(message.severity),
|
|
|
|
|
Tags: this.mapTags(message.severity),
|
2026-08-14 14:01:06 +03:00
|
|
|
...authHeaders,
|
2026-05-27 16:57:11 +01:00
|
|
|
},
|
|
|
|
|
...this.gotRequestOptions(),
|
|
|
|
|
});
|
|
|
|
|
this.logger.info({
|
|
|
|
|
message: "ntfy notification sent",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendMessage",
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const err = error as Error;
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "ntfy alert failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendMessage",
|
|
|
|
|
stack: err?.stack,
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private buildTopicUrl(address: string, topic: string): string {
|
|
|
|
|
return `${address.replace(/\/+$/, "")}/${encodeURIComponent(topic)}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 14:01:06 +03:00
|
|
|
/**
|
|
|
|
|
* Returns the Authorization header for the configured auth type, or null when the
|
|
|
|
|
* credentials are incomplete. Null is treated as a send failure rather than falling
|
|
|
|
|
* back to an anonymous post, so a misconfigured channel surfaces instead of quietly
|
|
|
|
|
* publishing to a topic the server may leave world-readable.
|
|
|
|
|
*/
|
|
|
|
|
private buildAuthHeaders(notification: Partial<Notification>, method: string): Record<string, string> | null {
|
|
|
|
|
const { ntfyAuthType, accessToken, ntfyUsername } = notification;
|
|
|
|
|
|
|
|
|
|
if (!ntfyAuthType || ntfyAuthType === "none") {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ntfyAuthType === "token") {
|
|
|
|
|
if (!accessToken) {
|
|
|
|
|
this.logMissingCredentials("access token", method);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return { Authorization: `Bearer ${accessToken}` };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ntfyUsername || !accessToken) {
|
|
|
|
|
this.logMissingCredentials("username and password", method);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return { Authorization: `Basic ${Buffer.from(`${ntfyUsername}:${accessToken}`).toString("base64")}` };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private logMissingCredentials(missing: string, method: string): void {
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: `ntfy notification is configured for authentication but is missing its ${missing}`,
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 16:57:11 +01:00
|
|
|
private buildNtfyText(message: NotificationMessage): string {
|
|
|
|
|
const lines = [
|
|
|
|
|
message.content.summary,
|
|
|
|
|
"",
|
|
|
|
|
"Monitor Details:",
|
|
|
|
|
`- Name: ${message.monitor.name}`,
|
|
|
|
|
`- URL: ${message.monitor.url}`,
|
|
|
|
|
`- Type: ${message.monitor.type}`,
|
|
|
|
|
`- Status: ${message.monitor.status}`,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (message.content.details && message.content.details.length > 0) {
|
|
|
|
|
lines.push("", "Additional Information:");
|
|
|
|
|
message.content.details.forEach((detail) => lines.push(`- ${detail}`));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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})`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message.content.incident) {
|
|
|
|
|
lines.push("", `${message.clientHost}/infrastructure/${message.monitor.id}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines.join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private mapPriority(severity: NotificationMessage["severity"]): string {
|
|
|
|
|
switch (severity) {
|
|
|
|
|
case "critical":
|
|
|
|
|
return "high";
|
|
|
|
|
case "warning":
|
|
|
|
|
return "default";
|
|
|
|
|
case "success":
|
|
|
|
|
return "low";
|
|
|
|
|
default:
|
|
|
|
|
return "default";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private mapTags(severity: NotificationMessage["severity"]): string {
|
|
|
|
|
switch (severity) {
|
|
|
|
|
case "critical":
|
|
|
|
|
return "rotating_light";
|
|
|
|
|
case "warning":
|
|
|
|
|
return "warning";
|
|
|
|
|
case "success":
|
|
|
|
|
return "white_check_mark";
|
|
|
|
|
default:
|
|
|
|
|
return "information_source";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|