inject logger
This commit is contained in:
1 parent
7c9410f9d7
commit
bebb5d6942
9 files changed
+125
-18
No files matched your search
@@ -1,9 +1,15 @@
|
||||
import got from "got";
|
||||
import type { INotificationProvider } from "@/service/index.js";
|
||||
import type { Notification, Monitor, MonitorStatusResponse } from "@/types/index.js";
|
||||
import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
||||
import {
|
||||
buildHardwareAlerts,
|
||||
buildHardwareWebhookBody,
|
||||
buildWebhookBody,
|
||||
getTestMessage,
|
||||
} from "@/service/infrastructure/notificationProviders/utils.js";
|
||||
|
||||
export class MatrixProvider implements INotificationProvider {
|
||||
constructor(private logger: any) {}
|
||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
||||
@@ -37,6 +43,7 @@ export class MatrixProvider implements INotificationProvider {
|
||||
formatted_body: formattedMessage,
|
||||
};
|
||||
try {
|
||||
this.logger?.debug?.("Sending Matrix alert", { room: roomId });
|
||||
await got.post(url, {
|
||||
json: body,
|
||||
headers: {
|
||||
@@ -46,11 +53,34 @@ export class MatrixProvider implements INotificationProvider {
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.logger?.error?.("Matrix alert failed", { error });
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
sendTestAlert = async (notification: Notification) => {
|
||||
return false;
|
||||
const { homeserverUrl, accessToken, roomId } = notification;
|
||||
if (!homeserverUrl || !accessToken || !roomId) {
|
||||
return false;
|
||||
}
|
||||
const url = `${homeserverUrl}/_matrix/client/v3/rooms/${roomId}/send/m.room.message?access_token=${accessToken}`;
|
||||
const body = {
|
||||
msgtype: "m.text",
|
||||
body: getTestMessage(),
|
||||
};
|
||||
try {
|
||||
this.logger?.debug?.("Sending Matrix test alert", { room: roomId });
|
||||
await got.post(url, {
|
||||
json: body,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
responseType: "json",
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.logger?.error?.("Matrix test alert failed", { error });
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user