uptime-kuma/server/notification-providers/Webpush.js

53 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-12-15 23:01:34 -05:00
const NotificationProvider = require("./notification-provider");
const { UP } = require("../../src/util");
2024-12-16 14:21:39 -05:00
const webpush = require("web-push");
2024-12-15 23:01:34 -05:00
const { setting } = require("../util-server");
class Webpush extends NotificationProvider {
name = "Webpush";
/**
* @inheritDoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
try {
// Get VAPID keys from settings
const publicVapidKey = await setting("webpushPublicVapidKey");
const privateVapidKey = await setting("webpushPrivateVapidKey");
// Set Vapid keys in web-push helper lib
2024-12-16 14:21:39 -05:00
webpush.setVapidDetails("https://github.com/louislam/uptime-kuma", publicVapidKey, privateVapidKey);
2024-12-15 23:01:34 -05:00
if (heartbeatJSON === null && monitorJSON === null) {
// Test message
const data = JSON.stringify({
2024-12-16 14:21:39 -05:00
title: "TEST",
2024-12-15 23:01:34 -05:00
body: "Test Alert - " + msg
});
//send push notification using web-push lib
2024-12-16 14:21:39 -05:00
await webpush.sendNotification(notification.subscription, data);
2024-12-15 23:01:34 -05:00
return okMsg;
}
const title = `Monitor ${heartbeatJSON["status"] === UP ? "UP" : "DOWN"}`;
const down = "❌ " + monitorJSON["name"] + " is DOWN ❌";
2024-12-16 14:21:39 -05:00
const up = "✅ " + monitorJSON["name"] + " is UP ✅";
2024-12-15 23:01:34 -05:00
2024-12-16 14:21:39 -05:00
const data = JSON.stringify({
title: title,
2024-12-15 23:01:34 -05:00
body: `${heartbeatJSON["status"] === UP ? up : down}`
});
//send push notification using web-push lib
2024-12-16 14:21:39 -05:00
await webpush.sendNotification(notification.subscription, data);
2024-12-15 23:01:34 -05:00
return okMsg;
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
}
2024-12-16 14:21:39 -05:00
module.exports = Webpush;