|
| 1 | +const SERVICE_NAME = "PushoverProvider"; |
| 2 | +import type { Notification } from "@/types/index.js"; |
| 3 | +import { NotificationProvider } from "@/service/infrastructure/notificationProviders/INotificationProvider.js"; |
| 4 | +import type { NotificationMessage } from "@/types/notificationMessage.js"; |
| 5 | +import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; |
| 6 | +import got from "got"; |
| 7 | + |
| 8 | +export class PushoverProvider extends NotificationProvider { |
| 9 | + async sendTestAlert(notification: Partial<Notification>): Promise<boolean> { |
| 10 | + if (!notification.address || !notification.accessToken) { |
| 11 | + return false; |
| 12 | + } |
| 13 | + |
| 14 | + try { |
| 15 | + await got.post("https://api.pushover.net/1/messages.json", { |
| 16 | + form: { |
| 17 | + token: notification.accessToken, |
| 18 | + user: notification.address, |
| 19 | + message: getTestMessage(), |
| 20 | + title: "Checkmate Test Notification", |
| 21 | + }, |
| 22 | + ...this.gotRequestOptions(), |
| 23 | + }); |
| 24 | + return true; |
| 25 | + } catch (error) { |
| 26 | + const errMsg = error instanceof Error ? error.message : "unknown error"; |
| 27 | + const errStack = error instanceof Error ? error.stack : undefined; |
| 28 | + this.logger.warn({ |
| 29 | + message: "Pushover test alert failed", |
| 30 | + service: SERVICE_NAME, |
| 31 | + method: "sendTestAlert", |
| 32 | + stack: errStack, |
| 33 | + details: { error: errMsg }, |
| 34 | + }); |
| 35 | + return false; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + async sendMessage(notification: Notification, message: NotificationMessage): Promise<boolean> { |
| 40 | + if (!notification.address || !notification.accessToken) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + const text = this.buildPushoverText(message); |
| 45 | + |
| 46 | + try { |
| 47 | + await got.post("https://api.pushover.net/1/messages.json", { |
| 48 | + form: { |
| 49 | + token: notification.accessToken, |
| 50 | + user: notification.address, |
| 51 | + message: text, |
| 52 | + title: message.content.title, |
| 53 | + }, |
| 54 | + ...this.gotRequestOptions(), |
| 55 | + }); |
| 56 | + |
| 57 | + this.logger.info({ |
| 58 | + message: "Pushover notification sent", |
| 59 | + service: SERVICE_NAME, |
| 60 | + method: "sendMessage", |
| 61 | + }); |
| 62 | + return true; |
| 63 | + } catch (error) { |
| 64 | + const errMsg = error instanceof Error ? error.message : "unknown error"; |
| 65 | + const errStack = error instanceof Error ? error.stack : undefined; |
| 66 | + this.logger.warn({ |
| 67 | + message: "Pushover alert failed", |
| 68 | + service: SERVICE_NAME, |
| 69 | + method: "sendMessage", |
| 70 | + stack: errStack, |
| 71 | + details: { error: errMsg }, |
| 72 | + }); |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private buildPushoverText(message: NotificationMessage): string { |
| 78 | + const lines: string[] = []; |
| 79 | + |
| 80 | + lines.push(message.content.summary); |
| 81 | + lines.push(""); |
| 82 | + lines.push("Monitor Details:"); |
| 83 | + lines.push(`• Name: ${message.monitor.name}`); |
| 84 | + lines.push(`• URL: ${message.monitor.url}`); |
| 85 | + lines.push(`• Type: ${message.monitor.type}`); |
| 86 | + lines.push(`• Status: ${message.monitor.status}`); |
| 87 | + |
| 88 | + if (message.content.details && message.content.details.length > 0) { |
| 89 | + lines.push(""); |
| 90 | + lines.push("Additional Information:"); |
| 91 | + message.content.details.forEach((detail) => lines.push(`• ${detail}`)); |
| 92 | + } |
| 93 | + |
| 94 | + if (message.content.thresholds && message.content.thresholds.length > 0) { |
| 95 | + lines.push(""); |
| 96 | + lines.push("Threshold Breaches:"); |
| 97 | + message.content.thresholds.forEach((breach) => { |
| 98 | + lines.push(`• ${breach.metric.toUpperCase()}: ${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + if (message.content.incident) { |
| 103 | + lines.push(""); |
| 104 | + lines.push(`View Incident: ${message.clientHost}/incidents/${message.monitor.id}`); |
| 105 | + } |
| 106 | + |
| 107 | + return lines.join("\n"); |
| 108 | + } |
| 109 | +} |
0 commit comments