2021-09-07 10:42:46 -04:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
|
|
|
|
|
|
|
class Mattermost extends NotificationProvider {
|
|
|
|
name = "mattermost";
|
|
|
|
|
2023-08-11 03:46:41 -04:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-07 10:42:46 -04:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 09:21:15 -04:00
|
|
|
const okMsg = "Sent Successfully.";
|
|
|
|
|
2021-09-07 10:42:46 -04:00
|
|
|
try {
|
|
|
|
const mattermostUserName = notification.mattermostusername || "Uptime Kuma";
|
2023-02-23 11:40:39 -05:00
|
|
|
// If heartbeatJSON is null, assume non monitoring notification (Certificate warning) or testing.
|
2021-09-07 10:42:46 -04:00
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
let mattermostTestData = {
|
|
|
|
username: mattermostUserName,
|
|
|
|
text: msg,
|
2022-04-09 03:44:50 -04:00
|
|
|
};
|
|
|
|
await axios.post(notification.mattermostWebhookUrl, mattermostTestData);
|
2021-09-07 10:42:46 -04:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
2022-04-09 03:44:50 -04:00
|
|
|
let mattermostChannel;
|
|
|
|
|
|
|
|
if (typeof notification.mattermostchannel === "string") {
|
|
|
|
mattermostChannel = notification.mattermostchannel.toLowerCase();
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:42:46 -04:00
|
|
|
const mattermostIconEmoji = notification.mattermosticonemo;
|
2023-02-23 11:40:39 -05:00
|
|
|
let mattermostIconEmojiOnline = "";
|
|
|
|
let mattermostIconEmojiOffline = "";
|
2021-09-07 10:42:46 -04:00
|
|
|
|
2023-02-23 11:40:39 -05:00
|
|
|
if (mattermostIconEmoji && typeof mattermostIconEmoji === "string") {
|
|
|
|
const emojiArray = mattermostIconEmoji.split(" ");
|
|
|
|
if (emojiArray.length >= 2) {
|
|
|
|
mattermostIconEmojiOnline = emojiArray[0];
|
|
|
|
mattermostIconEmojiOffline = emojiArray[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2021-09-07 10:42:46 -04:00
|
|
|
};
|
2023-02-23 11:40:39 -05:00
|
|
|
statusText = "down.";
|
|
|
|
color = "#FF0000";
|
|
|
|
} else if (heartbeatJSON.status === UP) {
|
|
|
|
iconEmoji = mattermostIconEmojiOnline || mattermostIconEmoji;
|
|
|
|
statusField = {
|
|
|
|
short: false,
|
|
|
|
title: "Ping",
|
|
|
|
value: heartbeatJSON.ping + "ms",
|
2021-09-07 10:42:46 -04:00
|
|
|
};
|
2023-02-23 11:40:39 -05:00
|
|
|
statusText = "up!";
|
|
|
|
color = "#32CD32";
|
2021-09-07 10:42:46 -04:00
|
|
|
}
|
2023-02-23 11:40:39 -05:00
|
|
|
|
|
|
|
let mattermostdata = {
|
|
|
|
username: monitorJSON.name + " " + mattermostUserName,
|
|
|
|
channel: mattermostChannel,
|
|
|
|
icon_emoji: iconEmoji,
|
|
|
|
icon_url: mattermostIconUrl,
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
fallback:
|
|
|
|
"Your " +
|
2023-11-30 02:58:01 -05:00
|
|
|
monitorJSON.pathName +
|
2023-02-23 11:40:39 -05:00
|
|
|
" service went " +
|
|
|
|
statusText,
|
|
|
|
color: color,
|
|
|
|
title:
|
2023-11-30 02:58:01 -05:00
|
|
|
monitorJSON.pathName +
|
2023-02-23 11:40:39 -05:00
|
|
|
" service went " +
|
|
|
|
statusText,
|
|
|
|
title_link: monitorJSON.url,
|
|
|
|
fields: [
|
|
|
|
statusField,
|
|
|
|
{
|
|
|
|
short: true,
|
2023-05-29 13:24:40 -04:00
|
|
|
title: `Time (${heartbeatJSON["timezone"]})`,
|
|
|
|
value: heartbeatJSON.localDateTime,
|
2023-02-23 11:40:39 -05:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2024-03-14 09:21:15 -04:00
|
|
|
await axios.post(notification.mattermostWebhookUrl, mattermostdata);
|
2023-02-23 11:40:39 -05:00
|
|
|
return okMsg;
|
2021-09-07 10:42:46 -04:00
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Mattermost;
|