2021-10-23 12:20:24 -04:00
|
|
|
//
|
|
|
|
// bark.js
|
|
|
|
// UptimeKuma
|
|
|
|
//
|
|
|
|
// Created by Lakr Aream on 2021/10/24.
|
|
|
|
// Copyright © 2021 Lakr Aream. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
|
|
|
const { default: axios } = require("axios");
|
|
|
|
|
|
|
|
// bark is an APN bridge that sends notifications to Apple devices.
|
|
|
|
|
|
|
|
const barkNotificationAvatar = "https://github.com/louislam/uptime-kuma/raw/master/public/icon.png";
|
|
|
|
const successMessage = "Successes!";
|
|
|
|
|
|
|
|
class Bark extends NotificationProvider {
|
|
|
|
name = "Bark";
|
|
|
|
|
2023-08-11 03:46:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-10-23 12:20:24 -04:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2022-04-13 12:30:32 -04:00
|
|
|
let barkEndpoint = notification.barkEndpoint;
|
2021-10-23 12:20:24 -04:00
|
|
|
|
2022-04-13 12:30:32 -04:00
|
|
|
// check if the endpoint has a "/" suffix, if so, delete it first
|
|
|
|
if (barkEndpoint.endsWith("/")) {
|
|
|
|
barkEndpoint = barkEndpoint.substring(0, barkEndpoint.length - 1);
|
|
|
|
}
|
2021-10-23 12:20:24 -04:00
|
|
|
|
2022-04-17 03:43:03 -04:00
|
|
|
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) {
|
2022-04-13 12:30:32 -04:00
|
|
|
let title = "UptimeKuma Monitor Up";
|
2022-09-16 02:21:22 -04:00
|
|
|
return await this.postNotification(notification, title, msg, barkEndpoint);
|
2022-04-13 12:30:32 -04:00
|
|
|
}
|
2021-10-23 12:20:24 -04:00
|
|
|
|
2022-04-17 03:43:03 -04:00
|
|
|
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) {
|
2022-04-13 12:30:32 -04:00
|
|
|
let title = "UptimeKuma Monitor Down";
|
2022-09-16 02:21:22 -04:00
|
|
|
return await this.postNotification(notification, title, msg, barkEndpoint);
|
2022-04-13 12:30:32 -04:00
|
|
|
}
|
2021-10-23 12:20:24 -04:00
|
|
|
|
2022-04-13 12:30:32 -04:00
|
|
|
if (msg != null) {
|
|
|
|
let title = "UptimeKuma Message";
|
2022-09-16 02:21:22 -04:00
|
|
|
return await this.postNotification(notification, title, msg, barkEndpoint);
|
2021-10-23 12:20:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:24:53 -04:00
|
|
|
/**
|
2024-03-14 09:21:15 -04:00
|
|
|
* Add additional parameter for Bark v1 endpoints.
|
|
|
|
* Leads to better on device styles (iOS 15 optimized)
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {BeanModel} notification Notification to send
|
|
|
|
* @returns {string} Additional URL parameters
|
2022-04-16 15:24:53 -04:00
|
|
|
*/
|
2024-03-14 09:21:15 -04:00
|
|
|
additionalParameters(notification) {
|
2022-06-13 09:44:10 -04:00
|
|
|
// set icon to uptime kuma icon, 11kb should be fine
|
2024-03-14 09:21:15 -04:00
|
|
|
let params = "?icon=" + barkNotificationAvatar;
|
2021-10-23 12:20:24 -04:00
|
|
|
// grouping all our notifications
|
2022-06-12 10:30:42 -04:00
|
|
|
if (notification.barkGroup != null) {
|
2024-03-14 09:21:15 -04:00
|
|
|
params += "&group=" + notification.barkGroup;
|
2022-06-12 10:30:42 -04:00
|
|
|
} else {
|
2022-06-13 09:44:10 -04:00
|
|
|
// default name
|
2024-03-14 09:21:15 -04:00
|
|
|
params += "&group=" + "UptimeKuma";
|
2022-06-12 10:30:42 -04:00
|
|
|
}
|
2021-10-23 12:20:24 -04:00
|
|
|
// picked a sound, this should follow system's mute status when arrival
|
2022-06-12 10:30:42 -04:00
|
|
|
if (notification.barkSound != null) {
|
2024-03-14 09:21:15 -04:00
|
|
|
params += "&sound=" + notification.barkSound;
|
2022-06-12 10:30:42 -04:00
|
|
|
} else {
|
2022-06-13 09:44:10 -04:00
|
|
|
// default sound
|
2024-03-14 09:21:15 -04:00
|
|
|
params += "&sound=" + "telegraph";
|
2022-06-12 10:30:42 -04:00
|
|
|
}
|
2024-03-14 09:21:15 -04:00
|
|
|
return params;
|
2021-10-23 12:20:24 -04:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:24:53 -04:00
|
|
|
/**
|
|
|
|
* Check if result is successful
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {object} result Axios response object
|
|
|
|
* @returns {void}
|
2022-04-16 15:24:53 -04:00
|
|
|
* @throws {Error} The status code is not in range 2xx
|
|
|
|
*/
|
2021-10-23 12:20:24 -04:00
|
|
|
checkResult(result) {
|
|
|
|
if (result.status == null) {
|
|
|
|
throw new Error("Bark notification failed with invalid response!");
|
|
|
|
}
|
|
|
|
if (result.status < 200 || result.status >= 300) {
|
|
|
|
throw new Error("Bark notification failed with status code " + result.status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:24:53 -04:00
|
|
|
/**
|
|
|
|
* Send the message
|
2023-08-11 03:46:41 -04:00
|
|
|
* @param {BeanModel} notification Notification to send
|
2022-04-16 15:24:53 -04:00
|
|
|
* @param {string} title Message title
|
|
|
|
* @param {string} subtitle Message
|
|
|
|
* @param {string} endpoint Endpoint to send request to
|
2024-03-15 10:02:55 -04:00
|
|
|
* @returns {Promise<string>} Success message
|
2022-04-16 15:24:53 -04:00
|
|
|
*/
|
2022-09-16 02:21:22 -04:00
|
|
|
async postNotification(notification, title, subtitle, endpoint) {
|
2023-09-21 09:08:04 -04:00
|
|
|
let result;
|
|
|
|
if (notification.apiVersion === "v1" || notification.apiVersion == null) {
|
|
|
|
// url encode title and subtitle
|
|
|
|
title = encodeURIComponent(title);
|
|
|
|
subtitle = encodeURIComponent(subtitle);
|
2024-03-14 09:21:15 -04:00
|
|
|
const params = this.additionalParameters(notification);
|
|
|
|
result = await axios.get(`${endpoint}/${title}/${subtitle}${params}`);
|
2023-09-21 09:08:04 -04:00
|
|
|
} else {
|
|
|
|
result = await axios.post(`${endpoint}/push`, {
|
|
|
|
title,
|
|
|
|
body: subtitle,
|
|
|
|
icon: barkNotificationAvatar,
|
|
|
|
sound: notification.barkSound || "telegraph", // default sound is telegraph
|
|
|
|
group: notification.barkGroup || "UptimeKuma", // default group is UptimeKuma
|
|
|
|
});
|
|
|
|
}
|
2021-10-23 12:20:24 -04:00
|
|
|
this.checkResult(result);
|
|
|
|
if (result.statusText != null) {
|
|
|
|
return "Bark notification succeed: " + result.statusText;
|
|
|
|
}
|
|
|
|
// because returned in range 200 ..< 300
|
|
|
|
return successMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Bark;
|