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

Improve mattermost notifications
pull/2867/head^2
Louis Lam 2 years ago committed by GitHub
commit 2f3f929fbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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: [ const mattermostIconUrl = notification.mattermosticonurl;
{ let iconEmoji = mattermostIconEmoji;
fallback: let statusField = {
"Your " + short: false,
monitorJSON["name"] + title: "Error",
" service went down.", value: heartbeatJSON.msg,
color: "#FF0000", };
title: let statusText = "unknown";
"❌ " + let color = "#000000";
monitorJSON["name"] + if (heartbeatJSON.status === DOWN) {
" service went down. ❌", iconEmoji = mattermostIconEmojiOffline || mattermostIconEmoji;
title_link: monitorJSON["url"], statusField = {
fields: [ short: false,
{ title: "Error",
short: true, value: heartbeatJSON.msg,
title: "Service Name",
value: monitorJSON["name"],
},
{
short: true,
title: "Time (UTC)",
value: heartbeatJSON["time"],
},
{
short: false,
title: "Error",
value: heartbeatJSON["msg"],
},
],
},
],
}; };
await axios.post( statusText = "down.";
notification.mattermostWebhookUrl, color = "#FF0000";
mattermostdowndata } else if (heartbeatJSON.status === UP) {
); iconEmoji = mattermostIconEmojiOnline || mattermostIconEmoji;
return okMsg; statusField = {
} else if (heartbeatJSON["status"] === UP) { short: false,
let mattermostupdata = { title: "Ping",
username: mattermostUserName, value: heartbeatJSON.ping + "ms",
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( statusText = "up!";
notification.mattermostWebhookUrl, color = "#32CD32";
mattermostupdata
);
return okMsg;
} }
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);
} }

Loading…
Cancel
Save