mirror of https://github.com/louislam/uptime-kuma
				
				
				
			Merge pull request #395 from WillianRod/feat/add-microsoft-teams-notification
Add microsoft teams notification providerpull/410/head
						commit
						76db55b657
					
				| 
						 | 
				
			
			@ -0,0 +1,124 @@
 | 
			
		|||
const NotificationProvider = require("./notification-provider");
 | 
			
		||||
const axios = require("axios");
 | 
			
		||||
const { DOWN, UP } = require("../../src/util");
 | 
			
		||||
 | 
			
		||||
class Teams extends NotificationProvider {
 | 
			
		||||
    name = "teams";
 | 
			
		||||
 | 
			
		||||
    _statusMessageFactory = (status, monitorName) => {
 | 
			
		||||
        if (status === DOWN) {
 | 
			
		||||
            return `🔴 Application [${monitorName}] went down`;
 | 
			
		||||
        } else if (status === UP) {
 | 
			
		||||
            return `✅ Application [${monitorName}] is back online`;
 | 
			
		||||
        }
 | 
			
		||||
        return "Notification";
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _getThemeColor = (status) => {
 | 
			
		||||
        if (status === DOWN) {
 | 
			
		||||
            return "ff0000";
 | 
			
		||||
        }
 | 
			
		||||
        if (status === UP) {
 | 
			
		||||
            return "00e804";
 | 
			
		||||
        }
 | 
			
		||||
        return "008cff";
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _notificationPayloadFactory = ({
 | 
			
		||||
        status,
 | 
			
		||||
        monitorMessage,
 | 
			
		||||
        monitorName,
 | 
			
		||||
        monitorUrl,
 | 
			
		||||
    }) => {
 | 
			
		||||
        const notificationMessage = this._statusMessageFactory(
 | 
			
		||||
            status,
 | 
			
		||||
            monitorName
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        const facts = [];
 | 
			
		||||
 | 
			
		||||
        if (monitorName) {
 | 
			
		||||
            facts.push({
 | 
			
		||||
                name: "Monitor",
 | 
			
		||||
                value: monitorName,
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (monitorUrl) {
 | 
			
		||||
            facts.push({
 | 
			
		||||
                name: "URL",
 | 
			
		||||
                value: monitorUrl,
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return {
 | 
			
		||||
            "@context": "https://schema.org/extensions",
 | 
			
		||||
            "@type": "MessageCard",
 | 
			
		||||
            themeColor: this._getThemeColor(status),
 | 
			
		||||
            summary: notificationMessage,
 | 
			
		||||
            sections: [
 | 
			
		||||
                {
 | 
			
		||||
                    activityImage:
 | 
			
		||||
                        "https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png",
 | 
			
		||||
                    activityTitle: "**Uptime Kuma**",
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                    activityTitle: notificationMessage,
 | 
			
		||||
                },
 | 
			
		||||
                {
 | 
			
		||||
                    activityTitle: "**Description**",
 | 
			
		||||
                    text: monitorMessage,
 | 
			
		||||
                    facts,
 | 
			
		||||
                },
 | 
			
		||||
            ],
 | 
			
		||||
        };
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _sendNotification = async (webhookUrl, payload) => {
 | 
			
		||||
        await axios.post(webhookUrl, payload);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    _handleGeneralNotification = (webhookUrl, msg) => {
 | 
			
		||||
        const payload = this._notificationPayloadFactory({
 | 
			
		||||
            monitorMessage: msg
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return this._sendNotification(webhookUrl, payload);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
 | 
			
		||||
        let okMsg = "Sent Successfully. ";
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            if (heartbeatJSON == null) {
 | 
			
		||||
                await this._handleGeneralNotification(notification.webhookUrl, msg);
 | 
			
		||||
                return okMsg;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let url;
 | 
			
		||||
 | 
			
		||||
            if (monitorJSON["type"] === "port") {
 | 
			
		||||
                url = monitorJSON["hostname"];
 | 
			
		||||
                if (monitorJSON["port"]) {
 | 
			
		||||
                    url += ":" + monitorJSON["port"];
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                url = monitorJSON["url"];
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            const payload = this._notificationPayloadFactory({
 | 
			
		||||
                monitorMessage: heartbeatJSON.msg,
 | 
			
		||||
                monitorName: monitorJSON.name,
 | 
			
		||||
                monitorUrl: url,
 | 
			
		||||
                status: heartbeatJSON.status,
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            await this._sendNotification(notification.webhookUrl, payload);
 | 
			
		||||
            return okMsg;
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            this.throwGeneralAxiosError(error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module.exports = Teams;
 | 
			
		||||
| 
						 | 
				
			
			@ -13,6 +13,7 @@ const RocketChat = require("./notification-providers/rocket-chat");
 | 
			
		|||
const Signal = require("./notification-providers/signal");
 | 
			
		||||
const Slack = require("./notification-providers/slack");
 | 
			
		||||
const SMTP = require("./notification-providers/smtp");
 | 
			
		||||
const Teams = require("./notification-providers/teams");
 | 
			
		||||
const Telegram = require("./notification-providers/telegram");
 | 
			
		||||
const Webhook = require("./notification-providers/webhook");
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -28,6 +29,7 @@ class Notification {
 | 
			
		|||
        const list = [
 | 
			
		||||
            new Apprise(),
 | 
			
		||||
            new Discord(),
 | 
			
		||||
            new Teams(),
 | 
			
		||||
            new Gotify(),
 | 
			
		||||
            new Line(),
 | 
			
		||||
            new LunaSea(),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,6 +17,7 @@
 | 
			
		|||
                                <option value="webhook">Webhook</option>
 | 
			
		||||
                                <option value="smtp">{{ $t("Email") }} (SMTP)</option>
 | 
			
		||||
                                <option value="discord">Discord</option>
 | 
			
		||||
                                <option value="teams">Microsoft Teams</option>
 | 
			
		||||
                                <option value="signal">Signal</option>
 | 
			
		||||
                                <option value="gotify">Gotify</option>
 | 
			
		||||
                                <option value="slack">Slack</option>
 | 
			
		||||
| 
						 | 
				
			
			@ -400,6 +401,8 @@
 | 
			
		|||
 | 
			
		||||
                        <!-- DEPRECATED! Please create vue component in "./src/components/notifications/{notification name}.vue" -->
 | 
			
		||||
 | 
			
		||||
                        <Teams v-if="notification.type === 'teams'" />
 | 
			
		||||
 | 
			
		||||
                        <div class="mb-3 mt-4">
 | 
			
		||||
                            <hr class="dropdown-divider mb-4">
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -449,6 +452,7 @@ import { ucfirst } from "../util.ts"
 | 
			
		|||
import Confirm from "./Confirm.vue";
 | 
			
		||||
import HiddenInput from "./HiddenInput.vue";
 | 
			
		||||
import Telegram from "./notifications/Telegram.vue";
 | 
			
		||||
import Teams from "./notifications/Teams.vue";
 | 
			
		||||
import SMTP from "./notifications/SMTP.vue";
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
| 
						 | 
				
			
			@ -456,6 +460,7 @@ export default {
 | 
			
		|||
        Confirm,
 | 
			
		||||
        HiddenInput,
 | 
			
		||||
        Telegram,
 | 
			
		||||
        Teams,
 | 
			
		||||
        SMTP,
 | 
			
		||||
    },
 | 
			
		||||
    props: {},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
<template>
 | 
			
		||||
    <div class="mb-3">
 | 
			
		||||
        <label for="teams-webhookurl" class="form-label">Webhook URL</label>
 | 
			
		||||
        <input
 | 
			
		||||
            id="teams-webhookurl"
 | 
			
		||||
            v-model="$parent.notification.webhookUrl"
 | 
			
		||||
            type="text"
 | 
			
		||||
            class="form-control"
 | 
			
		||||
            required
 | 
			
		||||
        />
 | 
			
		||||
        <div class="form-text">
 | 
			
		||||
            You can learn how to create a webhook url
 | 
			
		||||
            <a
 | 
			
		||||
                href="https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook"
 | 
			
		||||
                target="_blank"
 | 
			
		||||
            >here</a>.
 | 
			
		||||
        </div>
 | 
			
		||||
    </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
export default {
 | 
			
		||||
    data() {
 | 
			
		||||
        return {
 | 
			
		||||
            name: "teams",
 | 
			
		||||
        };
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
</script>
 | 
			
		||||
		Loading…
	
		Reference in New Issue