const SERVICE_NAME = "SlackProvider"; import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js"; import { INotificationProvider } from "@/service/index.js"; import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody, getTestMessage, } from "@/service/infrastructure/notificationProviders/utils.js"; import got, { HTTPError } from "got"; export class SlackProvider implements INotificationProvider { private logger: any; constructor(logger: any) { this.logger = logger; } private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse); const body = buildHardwareWebhookBody(alertsToSend, monitor); return body; }; private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { const body = buildWebhookBody(monitor, monitorStatusResponse); return body; }; async sendAlert(notification: Notification, monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): Promise { let body; if (monitor.type === "hardware") { body = this.getHardwareContent(monitor, monitorStatusResponse); } else { body = this.getContent(monitor, monitorStatusResponse); } if (!notification.address) { return false; } try { await got.post(notification.address, { json: { text: body }, headers: { "Content-Type": "application/json", }, }); return true; } catch (error) { const err = error as Error; this.logger.warn({ message: "Slack alert failed", service: SERVICE_NAME, method: "sendAlert", stack: err?.stack, }); return false; } } async sendTestAlert(notification: Notification): Promise { if (!notification.address) { return false; } try { await got.post(notification.address, { json: { text: getTestMessage() }, headers: { "Content-Type": "application/json", }, }); return true; } catch (error) { const err = error as HTTPError; this.logger.warn({ message: "Slack test alert failed", service: SERVICE_NAME, method: "sendTestAlert", stack: err?.stack, }); return false; } } }