add teams
This commit is contained in:
1 parent
26db2b3a41
commit
33201096b7
10 files changed
+517
-2
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
DiscordProvider,
|
||||
PagerDutyProvider,
|
||||
MatrixProvider,
|
||||
TeamsProvider,
|
||||
INotificationsService,
|
||||
} from "@/service/index.js";
|
||||
import SuperSimpleQueueHelper from "../service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
|
||||
@@ -187,6 +188,7 @@ export const initializeServices = async ({
|
||||
const discordProvider = new DiscordProvider(logger);
|
||||
const pagerDutyProvider = new PagerDutyProvider(logger);
|
||||
const matrixProvider = new MatrixProvider(logger);
|
||||
const teamsProvider = new TeamsProvider(logger);
|
||||
|
||||
const notificationsService = new NotificationsService(
|
||||
notificationsRepository,
|
||||
@@ -197,6 +199,7 @@ export const initializeServices = async ({
|
||||
discordProvider,
|
||||
pagerDutyProvider,
|
||||
matrixProvider,
|
||||
teamsProvider,
|
||||
settingsService,
|
||||
logger,
|
||||
notificationMessageBuilder
|
||||
|
||||
@@ -25,7 +25,7 @@ const NotificationSchema = new Schema<NotificationDocument>(
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: ["email", "slack", "discord", "webhook", "pager_duty", "matrix"] as NotificationChannel[],
|
||||
enum: ["email", "slack", "discord", "webhook", "pager_duty", "matrix", "teams"] as NotificationChannel[],
|
||||
required: true,
|
||||
},
|
||||
notificationName: {
|
||||
|
||||
@@ -6,6 +6,7 @@ export * from "@/service/infrastructure/notificationProviders/INotificationProvi
|
||||
export * from "@/service/infrastructure/notificationProviders/pagerduty.js";
|
||||
export * from "@/service/infrastructure/notificationProviders/matrix.js";
|
||||
export * from "@/service/infrastructure/notificationProviders/slack.js";
|
||||
export * from "@/service/infrastructure/notificationProviders/teams.js";
|
||||
export * from "@/service/infrastructure/notificationProviders/webhook.js";
|
||||
export * from "@/service/infrastructure/notificationsService.js";
|
||||
export * from "@/service/infrastructure/statusService.js";
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
const SERVICE_NAME = "TeamsProvider";
|
||||
import type { Notification } from "@/types/index.js";
|
||||
import { INotificationProvider } from "@/service/index.js";
|
||||
import type { NotificationMessage } from "@/types/notificationMessage.js";
|
||||
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
|
||||
import got, { HTTPError } from "got";
|
||||
|
||||
export class TeamsProvider implements INotificationProvider {
|
||||
private logger: any;
|
||||
|
||||
constructor(logger: any) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
async sendTestAlert(notification: Notification): Promise<boolean> {
|
||||
if (!notification.address) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = this.wrapAdaptiveCard({
|
||||
type: "AdaptiveCard",
|
||||
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
|
||||
version: "1.4",
|
||||
body: [
|
||||
{
|
||||
type: "TextBlock",
|
||||
text: getTestMessage(),
|
||||
weight: "Bolder",
|
||||
size: "Medium",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await got.post(notification.address, {
|
||||
json: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
const err = error as HTTPError;
|
||||
this.logger.warn({
|
||||
message: "Teams test alert failed",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendTestAlert",
|
||||
stack: err?.stack,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> {
|
||||
if (!notification.address) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const payload = this.wrapAdaptiveCard(this.buildAdaptiveCard(message));
|
||||
|
||||
try {
|
||||
await got.post(notification.address, {
|
||||
json: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
this.logger.info({
|
||||
message: "Teams notification sent via sendMessage",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendMessage",
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
const err = error as Error;
|
||||
this.logger.warn({
|
||||
message: "Teams alert failed via sendMessage",
|
||||
service: SERVICE_NAME,
|
||||
method: "sendMessage",
|
||||
stack: err?.stack,
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap an Adaptive Card in the Teams webhook envelope format
|
||||
*/
|
||||
private wrapAdaptiveCard(card: object): object {
|
||||
return {
|
||||
type: "message",
|
||||
attachments: [
|
||||
{
|
||||
contentType: "application/vnd.microsoft.card.adaptive",
|
||||
contentUrl: null,
|
||||
content: card,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an Adaptive Card from NotificationMessage
|
||||
*/
|
||||
private buildAdaptiveCard(message: NotificationMessage): object {
|
||||
const colorMap: Record<string, string> = {
|
||||
critical: "attention",
|
||||
warning: "warning",
|
||||
success: "good",
|
||||
info: "accent",
|
||||
};
|
||||
const color = colorMap[message.severity] || "default";
|
||||
|
||||
const body: any[] = [];
|
||||
|
||||
// Header with colored status indicator
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: message.content.title,
|
||||
weight: "Bolder",
|
||||
size: "Large",
|
||||
color,
|
||||
wrap: true,
|
||||
});
|
||||
|
||||
// Summary
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: message.content.summary,
|
||||
wrap: true,
|
||||
spacing: "Small",
|
||||
});
|
||||
|
||||
// Separator
|
||||
body.push({
|
||||
type: "ColumnSet",
|
||||
separator: true,
|
||||
spacing: "Medium",
|
||||
columns: [],
|
||||
});
|
||||
|
||||
// Monitor details as a FactSet
|
||||
body.push({
|
||||
type: "FactSet",
|
||||
facts: [
|
||||
{ title: "Name", value: message.monitor.name },
|
||||
{ title: "Type", value: message.monitor.type },
|
||||
{ title: "Status", value: message.monitor.status },
|
||||
{ title: "URL", value: message.monitor.url },
|
||||
],
|
||||
});
|
||||
|
||||
// Threshold breaches
|
||||
if (message.content.thresholds && message.content.thresholds.length > 0) {
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: "**Threshold Breaches**",
|
||||
weight: "Bolder",
|
||||
spacing: "Medium",
|
||||
wrap: true,
|
||||
});
|
||||
|
||||
for (const breach of message.content.thresholds) {
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: `• **${breach.metric.toUpperCase()}**: ${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`,
|
||||
wrap: true,
|
||||
spacing: "Small",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Additional details
|
||||
if (message.content.details && message.content.details.length > 0) {
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: "**Additional Information**",
|
||||
weight: "Bolder",
|
||||
spacing: "Medium",
|
||||
wrap: true,
|
||||
});
|
||||
|
||||
for (const detail of message.content.details) {
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: `• ${detail}`,
|
||||
wrap: true,
|
||||
spacing: "Small",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Timestamp footer
|
||||
body.push({
|
||||
type: "TextBlock",
|
||||
text: `Checkmate | ${new Date(message.content.timestamp).toUTCString()}`,
|
||||
size: "Small",
|
||||
isSubtle: true,
|
||||
spacing: "Medium",
|
||||
wrap: true,
|
||||
});
|
||||
|
||||
// Actions (incident link)
|
||||
const actions: any[] = [];
|
||||
if (message.content.incident) {
|
||||
actions.push({
|
||||
type: "Action.OpenUrl",
|
||||
title: "View Incident",
|
||||
url: `${message.clientHost}/infrastructure/${message.monitor.id}`,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
type: "AdaptiveCard",
|
||||
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
|
||||
version: "1.4",
|
||||
body,
|
||||
...(actions.length > 0 ? { actions } : {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ export class NotificationsService implements INotificationsService {
|
||||
private discordProvider: INotificationProvider;
|
||||
private pagerDutyProvider: INotificationProvider;
|
||||
private matrixProvider: INotificationProvider;
|
||||
private teamsProvider: INotificationProvider;
|
||||
private logger: ILogger;
|
||||
private settingsService: ISettingsService;
|
||||
private notificationMessageBuilder: INotificationMessageBuilder;
|
||||
@@ -45,6 +46,7 @@ export class NotificationsService implements INotificationsService {
|
||||
discordProvider: INotificationProvider,
|
||||
pagerDutyProvider: INotificationProvider,
|
||||
matrixProvider: INotificationProvider,
|
||||
teamsProvider: INotificationProvider,
|
||||
settingsService: ISettingsService,
|
||||
logger: ILogger,
|
||||
notificationMessageBuilder: INotificationMessageBuilder
|
||||
@@ -57,6 +59,7 @@ export class NotificationsService implements INotificationsService {
|
||||
this.discordProvider = discordProvider;
|
||||
this.pagerDutyProvider = pagerDutyProvider;
|
||||
this.matrixProvider = matrixProvider;
|
||||
this.teamsProvider = teamsProvider;
|
||||
this.settingsService = settingsService;
|
||||
this.logger = logger;
|
||||
this.notificationMessageBuilder = notificationMessageBuilder;
|
||||
@@ -92,6 +95,8 @@ export class NotificationsService implements INotificationsService {
|
||||
return await this.discordProvider.sendMessage!(notification, notificationMessage);
|
||||
case "email":
|
||||
return await this.emailProvider.sendMessage!(notification, notificationMessage);
|
||||
case "teams":
|
||||
return await this.teamsProvider.sendMessage!(notification, notificationMessage);
|
||||
default:
|
||||
this.logger.warn({
|
||||
message: `Unknown notification type: ${notification.type}`,
|
||||
@@ -150,6 +155,8 @@ export class NotificationsService implements INotificationsService {
|
||||
return await this.matrixProvider.sendTestAlert(notification);
|
||||
case "webhook":
|
||||
return await this.webhookProvider.sendTestAlert(notification);
|
||||
case "teams":
|
||||
return await this.teamsProvider.sendTestAlert(notification);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export const NotificationChannels = ["email", "slack", "discord", "webhook", "pager_duty", "matrix"] as const;
|
||||
export const NotificationChannels = ["email", "slack", "discord", "webhook", "pager_duty", "matrix", "teams"] as const;
|
||||
export type NotificationChannel = (typeof NotificationChannels)[number];
|
||||
|
||||
export interface Notification {
|
||||
|
||||
@@ -59,6 +59,15 @@ export const createNotificationBodyValidation = z.discriminatedUnion("type", [
|
||||
roomId: z.string().min(1, "Room ID is required"),
|
||||
accessToken: z.string().min(1, "Access Token is required"),
|
||||
}),
|
||||
// Teams notification
|
||||
z.object({
|
||||
notificationName: z.string().min(1, "Notification name is required"),
|
||||
type: z.literal("teams"),
|
||||
address: z.string().url("Please enter a valid Webhook URL"),
|
||||
homeserverUrl: z.union([z.string(), z.literal("")]).optional(),
|
||||
roomId: z.union([z.string(), z.literal("")]).optional(),
|
||||
accessToken: z.union([z.string(), z.literal("")]).optional(),
|
||||
}),
|
||||
]);
|
||||
|
||||
export const sendTestEmailBodyValidation = z.object({
|
||||
|
||||
Reference in new issue
Block a user