Repository: louislam/uptime-kuma
File: server/notification.js (JavaScript)

Code before:
const axios = require("axios");
const { R } = require("redbean-node");
const FormData = require("form-data");
const nodemailer = require("nodemailer");
const child_process = require("child_process");

const { UP, DOWN } = require("../src/util");

class Notification {

    /**
     *
     * @param notification
     * @param msg
     * @param monitorJSON
     * @param heartbeatJSON
     * @returns {Promise<string>} Successful msg
     * Throw Error with fail msg
     */
    static async send(notification, monitorName = null, monitorJSON = null, heartbeatJSON = null) {
        let okMsg = "Sent Successfully. ";

        let msg = `[${notification.name}] Testing`;
        // heartbeatJSON is only defined if we're not testing
        if(heartbeatJSON) {
            let text;
            if (heartbeatJSON["status"] === UP) {
                text = "✅ Up"
            } else {
                text = "🔴 Down"
            }
            msg = `[${monitorName}] [${text}] ${heartbeatJSON["msg"]}`;
        }

        if (notification.type === "telegram") {
            try {
                await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
                    params: {
                        chat_id: notification.telegramChatID,
                        text: msg,
                    },
                })
                return okMsg;

            } catch (error) {

Review comment (suggestion):
The function is not just for up/down.
I prefer to keep it as `msg`, because I will implement cert expiration notification.
And it possibly breaks the "Test" button.
 

Code after:
const axios = require("axios");
const { R } = require("redbean-node");
const FormData = require("form-data");
const nodemailer = require("nodemailer");
const child_process = require("child_process");

const { UP, DOWN } = require("../src/util");

class Notification {

    /**
     *
     * @param notification
     * @param msg
     * @param monitorJSON
     * @param heartbeatJSON
     * @returns {Promise<string>} Successful msg
     * Throw Error with fail msg
     */
    static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        let okMsg = "Sent Successfully. ";

        if (notification.type === "telegram") {
            try {
                await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
                    params: {
                        chat_id: notification.telegramChatID,
                        text: msg,
                    },
                })
                return okMsg;

            } catch (error) {
                let msg = (error.response.data.description) ? error.response.data.description : "Error without description"
                throw new Error(msg)
            }

        } else if (notification.type === "gotify") {
            try {
                if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
                    notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
                }
                await axios.post(`${notification.gotifyserverurl}/message?token=${notification.gotifyapplicationToken}`, {
                    "message": msg,
                    "priority": notification.gotifyPriority || 8,