Merge pull request #2831 from mtelgkamp/mattermost-notification-improvements

Improve mattermost notifications
This commit is contained in:
Louis Lam 2023-04-09 15:42:28 +08:00 committed by GitHub
commit 2f3f929fbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,7 @@ class Mattermost extends NotificationProvider {
let okMsg = "Sent Successfully."; let okMsg = "Sent Successfully.";
try { try {
const mattermostUserName = notification.mattermostusername || "Uptime Kuma"; const mattermostUserName = notification.mattermostusername || "Uptime Kuma";
// If heartbeatJSON is null, assume we're testing. // If heartbeatJSON is null, assume non monitoring notification (Certificate warning) or testing.
if (heartbeatJSON == null) { if (heartbeatJSON == null) {
let mattermostTestData = { let mattermostTestData = {
username: mattermostUserName, username: mattermostUserName,
@ -27,86 +27,69 @@ class Mattermost extends NotificationProvider {
} }
const mattermostIconEmoji = notification.mattermosticonemo; const mattermostIconEmoji = notification.mattermosticonemo;
const mattermostIconUrl = notification.mattermosticonurl; let mattermostIconEmojiOnline = "";
let mattermostIconEmojiOffline = "";
if (heartbeatJSON["status"] === DOWN) { if (mattermostIconEmoji && typeof mattermostIconEmoji === "string") {
let mattermostdowndata = { const emojiArray = mattermostIconEmoji.split(" ");
username: mattermostUserName, if (emojiArray.length >= 2) {
text: "Uptime Kuma Alert", mattermostIconEmojiOnline = emojiArray[0];
channel: mattermostChannel, mattermostIconEmojiOffline = emojiArray[1];
icon_emoji: mattermostIconEmoji, }
icon_url: mattermostIconUrl, }
attachments: [ const mattermostIconUrl = notification.mattermosticonurl;
{ let iconEmoji = mattermostIconEmoji;
fallback: let statusField = {
"Your " +
monitorJSON["name"] +
" service went down.",
color: "#FF0000",
title:
"❌ " +
monitorJSON["name"] +
" service went down. ❌",
title_link: monitorJSON["url"],
fields: [
{
short: true,
title: "Service Name",
value: monitorJSON["name"],
},
{
short: true,
title: "Time (UTC)",
value: heartbeatJSON["time"],
},
{
short: false, short: false,
title: "Error", title: "Error",
value: heartbeatJSON["msg"], value: heartbeatJSON.msg,
},
],
},
],
}; };
await axios.post( let statusText = "unknown";
notification.mattermostWebhookUrl, let color = "#000000";
mattermostdowndata if (heartbeatJSON.status === DOWN) {
); iconEmoji = mattermostIconEmojiOffline || mattermostIconEmoji;
return okMsg; statusField = {
} else if (heartbeatJSON["status"] === UP) { short: false,
let mattermostupdata = { title: "Error",
username: mattermostUserName, value: heartbeatJSON.msg,
text: "Uptime Kuma Alert", };
statusText = "down.";
color = "#FF0000";
} else if (heartbeatJSON.status === UP) {
iconEmoji = mattermostIconEmojiOnline || mattermostIconEmoji;
statusField = {
short: false,
title: "Ping",
value: heartbeatJSON.ping + "ms",
};
statusText = "up!";
color = "#32CD32";
}
let mattermostdata = {
username: monitorJSON.name + " " + mattermostUserName,
channel: mattermostChannel, channel: mattermostChannel,
icon_emoji: mattermostIconEmoji, icon_emoji: iconEmoji,
icon_url: mattermostIconUrl, icon_url: mattermostIconUrl,
attachments: [ attachments: [
{ {
fallback: fallback:
"Your " + "Your " +
monitorJSON["name"] + monitorJSON.name +
" service went up!", " service went " +
color: "#32CD32", statusText,
color: color,
title: title:
"✅ " + monitorJSON.name +
monitorJSON["name"] + " service went " +
" service went up! ✅", statusText,
title_link: monitorJSON["url"], title_link: monitorJSON.url,
fields: [ fields: [
{ statusField,
short: true,
title: "Service Name",
value: monitorJSON["name"],
},
{ {
short: true, short: true,
title: "Time (UTC)", title: "Time (UTC)",
value: heartbeatJSON["time"], value: heartbeatJSON.time,
},
{
short: false,
title: "Ping",
value: heartbeatJSON["ping"] + "ms",
}, },
], ],
}, },
@ -114,10 +97,9 @@ class Mattermost extends NotificationProvider {
}; };
await axios.post( await axios.post(
notification.mattermostWebhookUrl, notification.mattermostWebhookUrl,
mattermostupdata mattermostdata
); );
return okMsg; return okMsg;
}
} catch (error) { } catch (error) {
this.throwGeneralAxiosError(error); this.throwGeneralAxiosError(error);
} }