2021-10-23 09:20:24 -07: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";
|
|
|
|
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2022-04-13 09:30:32 -07:00
|
|
|
let barkEndpoint = notification.barkEndpoint;
|
2021-10-23 09:20:24 -07:00
|
|
|
|
2022-04-13 09:30:32 -07: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 09:20:24 -07:00
|
|
|
|
2022-04-17 00:43:03 -07:00
|
|
|
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) {
|
2022-04-13 09:30:32 -07:00
|
|
|
let title = "UptimeKuma Monitor Up";
|
|
|
|
return await this.postNotification(title, msg, barkEndpoint);
|
|
|
|
}
|
2021-10-23 09:20:24 -07:00
|
|
|
|
2022-04-17 00:43:03 -07:00
|
|
|
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) {
|
2022-04-13 09:30:32 -07:00
|
|
|
let title = "UptimeKuma Monitor Down";
|
|
|
|
return await this.postNotification(title, msg, barkEndpoint);
|
|
|
|
}
|
2021-10-23 09:20:24 -07:00
|
|
|
|
2022-04-13 09:30:32 -07:00
|
|
|
if (msg != null) {
|
|
|
|
let title = "UptimeKuma Message";
|
|
|
|
return await this.postNotification(title, msg, barkEndpoint);
|
2021-10-23 09:20:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:24:53 -07:00
|
|
|
/**
|
|
|
|
* Add additional parameter for better on device styles (iOS 15
|
|
|
|
* optimized)
|
|
|
|
* @param {string} postUrl URL to append parameters to
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-06-12 07:49:04 -07:00
|
|
|
appendAdditionalParameters(notification, postUrl) {
|
2021-10-23 09:20:24 -07:00
|
|
|
// grouping all our notifications
|
2022-06-12 07:30:42 -07:00
|
|
|
if (notification.barkGroup != null) {
|
|
|
|
postUrl += "&group=" + notification.barkGroup;
|
|
|
|
} else {
|
|
|
|
// default group
|
2022-06-13 02:06:05 -07:00
|
|
|
postUrl += "&group=" + "UptimeKuma";
|
2022-06-12 07:30:42 -07:00
|
|
|
}
|
2021-10-23 09:20:24 -07:00
|
|
|
// set icon to uptime kuma icon, 11kb should be fine
|
|
|
|
postUrl += "&icon=" + barkNotificationAvatar;
|
|
|
|
// picked a sound, this should follow system's mute status when arrival
|
2022-06-12 07:30:42 -07:00
|
|
|
if (notification.barkSound != null) {
|
|
|
|
postUrl += "&sound=" + notification.barkSound;
|
|
|
|
} else {
|
|
|
|
postUrl += "&sound=" + "telegraph";
|
|
|
|
// default sound
|
|
|
|
}
|
2021-10-23 09:20:24 -07:00
|
|
|
return postUrl;
|
|
|
|
}
|
|
|
|
|
2022-04-16 12:24:53 -07:00
|
|
|
/**
|
|
|
|
* Check if result is successful
|
|
|
|
* @param {Object} result Axios response object
|
|
|
|
* @throws {Error} The status code is not in range 2xx
|
|
|
|
*/
|
2021-10-23 09:20:24 -07: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 12:24:53 -07:00
|
|
|
/**
|
|
|
|
* Send the message
|
|
|
|
* @param {string} title Message title
|
|
|
|
* @param {string} subtitle Message
|
|
|
|
* @param {string} endpoint Endpoint to send request to
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-10-23 09:20:24 -07:00
|
|
|
async postNotification(title, subtitle, endpoint) {
|
|
|
|
// url encode title and subtitle
|
|
|
|
title = encodeURIComponent(title);
|
|
|
|
subtitle = encodeURIComponent(subtitle);
|
|
|
|
let postUrl = endpoint + "/" + title + "/" + subtitle;
|
|
|
|
postUrl = this.appendAdditionalParameters(postUrl);
|
|
|
|
let result = await axios.get(postUrl);
|
|
|
|
this.checkResult(result);
|
|
|
|
if (result.statusText != null) {
|
|
|
|
return "Bark notification succeed: " + result.statusText;
|
|
|
|
}
|
|
|
|
// because returned in range 200 ..< 300
|
|
|
|
return successMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Bark;
|