From 063d64eec8d9aba488f4cd8f72f46ab327d06ed3 Mon Sep 17 00:00:00 2001 From: Willian Rodrigues Barbosa Date: Sun, 12 Sep 2021 14:46:59 -0300 Subject: [PATCH 1/4] feat: add microsoft teams notification provider --- server/notification-providers/teams.js | 118 +++++++++++++++++++++++++ server/notification.js | 2 + src/components/NotificationDialog.vue | 5 ++ src/components/notifications/Teams.vue | 29 ++++++ 4 files changed, 154 insertions(+) create mode 100644 server/notification-providers/teams.js create mode 100644 src/components/notifications/Teams.vue diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js new file mode 100644 index 000000000..2cd5a36c5 --- /dev/null +++ b/server/notification-providers/teams.js @@ -0,0 +1,118 @@ +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 test"; + }; + + _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 + ); + 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: [ + { + name: "Monitor", + value: monitorName, + }, + { + name: "URL", + value: monitorUrl, + }, + ], + }, + ], + }; + }; + + _sendNotification = async (webhookUrl, payload) => { + await axios.post(webhookUrl, payload); + }; + + _handleTestNotification = (webhookUrl) => { + const payload = this._notificationPayloadFactory({ + monitorMessage: "Just a test", + monitorName: "Test Notification", + monitorUrl: "http://localhost:3000", + }); + + return this._sendNotification(webhookUrl, payload); + }; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully. "; + + try { + if (heartbeatJSON == null) { + await this._handleTestNotification(notification.webhookUrl); + 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; diff --git a/server/notification.js b/server/notification.js index 83dabc537..134472410 100644 --- a/server/notification.js +++ b/server/notification.js @@ -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(), diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index ffb7ba71d..a83fa81c0 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -17,6 +17,7 @@ + @@ -395,6 +396,8 @@ + +
@@ -444,6 +447,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 { @@ -451,6 +455,7 @@ export default { Confirm, HiddenInput, Telegram, + Teams, SMTP, }, props: {}, diff --git a/src/components/notifications/Teams.vue b/src/components/notifications/Teams.vue new file mode 100644 index 000000000..e28db2444 --- /dev/null +++ b/src/components/notifications/Teams.vue @@ -0,0 +1,29 @@ + + + From f351b423c56d294364f5fbf122f2a5bed954e6fe Mon Sep 17 00:00:00 2001 From: LouisLam Date: Mon, 13 Sep 2021 20:25:41 +0800 Subject: [PATCH 2/4] link to the english version of microsoft link --- src/components/notifications/Teams.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/notifications/Teams.vue b/src/components/notifications/Teams.vue index e28db2444..748bf7ad6 100644 --- a/src/components/notifications/Teams.vue +++ b/src/components/notifications/Teams.vue @@ -11,7 +11,7 @@
You can learn how to create a webhook url here.
From ccb8736b3d143f123e81acfce0a498f058e4d9ad Mon Sep 17 00:00:00 2001 From: Willian Rodrigues Barbosa Date: Mon, 13 Sep 2021 14:02:52 -0300 Subject: [PATCH 3/4] fix: send msg if heartbeat message is not set --- server/notification-providers/teams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index 2cd5a36c5..6021883ee 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -101,7 +101,7 @@ class Teams extends NotificationProvider { } const payload = this._notificationPayloadFactory({ - monitorMessage: heartbeatJSON.msg, + monitorMessage: heartbeatJSON.msg || msg, monitorName: monitorJSON.name, monitorUrl: url, status: heartbeatJSON.status, From 1693873f4a1b6deefde60fc98a5733b92054a6d9 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Wed, 15 Sep 2021 16:38:28 +0800 Subject: [PATCH 4/4] [Teams] change handleTestNotification to GeneralNotification --- server/notification-providers/teams.js | 40 +++++++++++++++----------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index 6021883ee..72409ffc8 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -11,7 +11,7 @@ class Teams extends NotificationProvider { } else if (status === UP) { return `✅ Application [${monitorName}] is back online`; } - return "Notification test"; + return "Notification"; }; _getThemeColor = (status) => { @@ -34,6 +34,23 @@ class Teams extends NotificationProvider { 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", @@ -51,16 +68,7 @@ class Teams extends NotificationProvider { { activityTitle: "**Description**", text: monitorMessage, - facts: [ - { - name: "Monitor", - value: monitorName, - }, - { - name: "URL", - value: monitorUrl, - }, - ], + facts, }, ], }; @@ -70,11 +78,9 @@ class Teams extends NotificationProvider { await axios.post(webhookUrl, payload); }; - _handleTestNotification = (webhookUrl) => { + _handleGeneralNotification = (webhookUrl, msg) => { const payload = this._notificationPayloadFactory({ - monitorMessage: "Just a test", - monitorName: "Test Notification", - monitorUrl: "http://localhost:3000", + monitorMessage: msg }); return this._sendNotification(webhookUrl, payload); @@ -85,7 +91,7 @@ class Teams extends NotificationProvider { try { if (heartbeatJSON == null) { - await this._handleTestNotification(notification.webhookUrl); + await this._handleGeneralNotification(notification.webhookUrl, msg); return okMsg; } @@ -101,7 +107,7 @@ class Teams extends NotificationProvider { } const payload = this._notificationPayloadFactory({ - monitorMessage: heartbeatJSON.msg || msg, + monitorMessage: heartbeatJSON.msg, monitorName: monitorJSON.name, monitorUrl: url, status: heartbeatJSON.status,