Improve mattermost notifications

This commit is contained in:
Michael Telgkamp 2023-02-23 17:40:39 +01:00
parent 7e3734af53
commit 186ca30508
No known key found for this signature in database
GPG key ID: D56A6B18219D5263

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);
} }