mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-06-16 17:36:47 +02:00
override slack message
This commit is contained in:
parent
8ccfa7bcdf
commit
01a3caf0c9
2 changed files with 27 additions and 4 deletions
|
@ -14,6 +14,7 @@ const { Notification } = require('../notification')
|
||||||
const { demoMode } = require('../config')
|
const { demoMode } = require('../config')
|
||||||
const version = require('../../package.json').version
|
const version = require('../../package.json').version
|
||||||
const apicache = require('../modules/apicache')
|
const apicache = require('../modules/apicache')
|
||||||
|
const moment = require('moment')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* status:
|
* status:
|
||||||
|
@ -664,17 +665,24 @@ class Monitor extends BeanModel {
|
||||||
const notificationList = await Monitor.getNotificationList(monitor)
|
const notificationList = await Monitor.getNotificationList(monitor)
|
||||||
|
|
||||||
let text
|
let text
|
||||||
|
let message
|
||||||
if (bean.status === UP) {
|
if (bean.status === UP) {
|
||||||
text = '✅ Up'
|
text = '✅ Up'
|
||||||
|
const heartbeat = await Monitor.getPreviousHeartbeatByStatus(monitor, DOWN)
|
||||||
|
message = `${text}: ${monitor.name} ( ${monitor.url} ).`
|
||||||
|
if (heartbeat) {
|
||||||
|
const diff = moment(bean.time).diff(moment(heartbeat.time))
|
||||||
|
const duration = moment.utc(diff).format('HH:mm:ss.SSS')
|
||||||
|
message += ` It was down for ${duration}.`
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
text = '🔴 Down'
|
text = '🔴 Down'
|
||||||
|
message = `${text}: ${monitor.name} ( ${monitor.url} ). Reason: ${bean.msg}.`
|
||||||
}
|
}
|
||||||
|
|
||||||
const msg = `[${monitor.name}] [${text}] ${bean.msg}`
|
|
||||||
|
|
||||||
for (const notification of notificationList) {
|
for (const notification of notificationList) {
|
||||||
try {
|
try {
|
||||||
await Notification.send(JSON.parse(notification.config), msg, await monitor.toJSON(), bean.toJSON())
|
await Notification.send(JSON.parse(notification.config), message, await monitor.toJSON(), bean.toJSON())
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Cannot send notification to ' + notification.name)
|
console.error('Cannot send notification to ' + notification.name)
|
||||||
console.log(e)
|
console.log(e)
|
||||||
|
@ -754,6 +762,15 @@ class Monitor extends BeanModel {
|
||||||
monitorID
|
monitorID
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async getPreviousHeartbeatByStatus (monitorID, status = 0) {
|
||||||
|
return await R.getRow(`
|
||||||
|
SELECT status, time FROM heartbeat
|
||||||
|
WHERE id = (select MAX(id) from heartbeat where monitor_id = ? and status = ?)
|
||||||
|
`, [
|
||||||
|
monitorID, status
|
||||||
|
])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Monitor
|
module.exports = Monitor
|
||||||
|
|
|
@ -86,7 +86,13 @@ class Slack extends NotificationProvider {
|
||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// overide slack message
|
||||||
|
data = {
|
||||||
|
"channel": notification.slackchannel,
|
||||||
|
"icon_emoji": notification.slackiconemo,
|
||||||
|
"username": notification.slackusername,
|
||||||
|
text: msg
|
||||||
|
}
|
||||||
await axios.post(notification.slackwebhookURL, data);
|
await axios.post(notification.slackwebhookURL, data);
|
||||||
return okMsg;
|
return okMsg;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue