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,97 +27,79 @@ 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: [
{
fallback:
"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,
title: "Error",
value: heartbeatJSON["msg"],
},
],
},
],
};
await axios.post(
notification.mattermostWebhookUrl,
mattermostdowndata
);
return okMsg;
} else if (heartbeatJSON["status"] === UP) {
let mattermostupdata = {
username: mattermostUserName,
text: "Uptime Kuma Alert",
channel: mattermostChannel,
icon_emoji: mattermostIconEmoji,
icon_url: mattermostIconUrl,
attachments: [
{
fallback:
"Your " +
monitorJSON["name"] +
" service went up!",
color: "#32CD32",
title:
"✅ " +
monitorJSON["name"] +
" service went up! ✅",
title_link: monitorJSON["url"],
fields: [
{
short: true,
title: "Service Name",
value: monitorJSON["name"],
},
{
short: true,
title: "Time (UTC)",
value: heartbeatJSON["time"],
},
{
short: false,
title: "Ping",
value: heartbeatJSON["ping"] + "ms",
},
],
},
],
};
await axios.post(
notification.mattermostWebhookUrl,
mattermostupdata
);
return okMsg;
} }
const mattermostIconUrl = notification.mattermosticonurl;
let iconEmoji = mattermostIconEmoji;
let statusField = {
short: false,
title: "Error",
value: heartbeatJSON.msg,
};
let statusText = "unknown";
let color = "#000000";
if (heartbeatJSON.status === DOWN) {
iconEmoji = mattermostIconEmojiOffline || mattermostIconEmoji;
statusField = {
short: false,
title: "Error",
value: heartbeatJSON.msg,
};
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,
icon_emoji: iconEmoji,
icon_url: mattermostIconUrl,
attachments: [
{
fallback:
"Your " +
monitorJSON.name +
" service went " +
statusText,
color: color,
title:
monitorJSON.name +
" service went " +
statusText,
title_link: monitorJSON.url,
fields: [
statusField,
{
short: true,
title: "Time (UTC)",
value: heartbeatJSON.time,
},
],
},
],
};
await axios.post(
notification.mattermostWebhookUrl,
mattermostdata
);
return okMsg;
} catch (error) { } catch (error) {
this.throwGeneralAxiosError(error); this.throwGeneralAxiosError(error);
} }