2026-08-02 01:59:11 -03:00
|
|
|
const SERVICE_NAME = "RocketChatProvider";
|
|
|
|
|
import type { Notification, NotificationMessage } from "@/domain/notifications/notification.type.js";
|
|
|
|
|
import { NotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
|
|
|
|
import { getTestMessage } from "@/domain/notifications/providers/utils.js";
|
|
|
|
|
import got from "got";
|
|
|
|
|
|
2026-08-06 10:55:11 -03:00
|
|
|
type RocketChatField = { title: string; value: string; short: boolean };
|
|
|
|
|
type RocketChatAttachment = {
|
|
|
|
|
color: string;
|
|
|
|
|
title: string;
|
|
|
|
|
title_link?: string;
|
|
|
|
|
fields?: RocketChatField[];
|
|
|
|
|
ts?: string;
|
|
|
|
|
};
|
|
|
|
|
type RocketChatPayload = { text: string; attachments: [RocketChatAttachment] };
|
|
|
|
|
|
2026-08-02 01:59:11 -03:00
|
|
|
export class RocketChatProvider extends NotificationProvider {
|
|
|
|
|
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
|
|
|
|
|
if (!notification.address) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await got.post(notification.address, {
|
2026-08-06 10:55:11 -03:00
|
|
|
json: this.buildTestPayload(),
|
2026-08-02 01:59:11 -03:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
...this.gotRequestOptions(),
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const err = error as Error;
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Rocket.Chat test alert failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendTestAlert",
|
|
|
|
|
stack: err?.stack,
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-06 10:55:11 -03:00
|
|
|
private buildTestPayload(): RocketChatPayload {
|
|
|
|
|
return {
|
|
|
|
|
text: getTestMessage(),
|
|
|
|
|
attachments: [
|
|
|
|
|
{
|
|
|
|
|
color: "#0000FF",
|
|
|
|
|
title: "Checkmate test notification",
|
|
|
|
|
fields: [
|
|
|
|
|
{ title: "Channel", value: "Rocket.Chat", short: true },
|
|
|
|
|
{ title: "Status", value: "Test notification", short: true },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-02 01:59:11 -03:00
|
|
|
async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> {
|
|
|
|
|
if (!notification.address) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await got.post(notification.address, {
|
|
|
|
|
json: { text: this.buildText(message) },
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
...this.gotRequestOptions(),
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const err = error as Error;
|
|
|
|
|
this.logger.warn({
|
|
|
|
|
message: "Rocket.Chat notification failed",
|
|
|
|
|
service: SERVICE_NAME,
|
|
|
|
|
method: "sendMessage",
|
|
|
|
|
stack: err?.stack,
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private buildText(message: NotificationMessage): string {
|
|
|
|
|
const lines = [
|
|
|
|
|
message.content.title,
|
|
|
|
|
message.content.summary,
|
|
|
|
|
"",
|
|
|
|
|
`Monitor: ${message.monitor.name}`,
|
|
|
|
|
`Type: ${message.monitor.type}`,
|
|
|
|
|
`Status: ${message.monitor.status}`,
|
|
|
|
|
`URL: ${message.monitor.url}`,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (message.content.details?.length) {
|
|
|
|
|
lines.push("", "Details:", ...message.content.details.map((detail) => `- ${detail}`));
|
|
|
|
|
}
|
|
|
|
|
if (message.content.thresholds?.length) {
|
|
|
|
|
lines.push(
|
|
|
|
|
"",
|
|
|
|
|
"Thresholds:",
|
|
|
|
|
...message.content.thresholds.map(
|
|
|
|
|
(threshold) => `- ${threshold.metric.toUpperCase()}: ${threshold.formattedValue} (threshold: ${threshold.threshold}${threshold.unit})`
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 08:33:29 -03:00
|
|
|
if (message.content.incident) {
|
|
|
|
|
lines.push("", `Incident: ${message.clientHost}/infrastructure/${message.monitor.id}`);
|
|
|
|
|
}
|
2026-08-02 01:59:11 -03:00
|
|
|
|
|
|
|
|
return lines.join("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|