mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-08 13:51:32 +08:00
implement webhook
This commit is contained in:
@@ -109,7 +109,7 @@ class Monitor extends BeanModel {
|
||||
if (! previousBeat || previousBeat.status !== bean.status) {
|
||||
bean.important = true;
|
||||
|
||||
let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? `, [
|
||||
let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id `, [
|
||||
this.id
|
||||
])
|
||||
|
||||
@@ -125,7 +125,7 @@ class Monitor extends BeanModel {
|
||||
let msg = `[${this.name}] [${text}] ${bean.msg}`;
|
||||
|
||||
for(let notification of notificationList) {
|
||||
promiseList.push(Notification.send(JSON.parse(notification.config), msg));
|
||||
promiseList.push(Notification.send(JSON.parse(notification.config), msg, await this.toJSON(), bean.toJSON()));
|
||||
}
|
||||
|
||||
await Promise.all(promiseList);
|
||||
|
@@ -1,16 +1,56 @@
|
||||
const axios = require("axios");
|
||||
const {R} = require("redbean-node");
|
||||
const FormData = require('form-data');
|
||||
|
||||
class Notification {
|
||||
static async send(notification, msg) {
|
||||
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
||||
if (notification.type === "telegram") {
|
||||
let res = await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
||||
params: {
|
||||
chat_id: notification.telegramChatID,
|
||||
text: msg,
|
||||
try {
|
||||
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
||||
params: {
|
||||
chat_id: notification.telegramChatID,
|
||||
text: msg,
|
||||
}
|
||||
})
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (notification.type === "webhook") {
|
||||
try {
|
||||
|
||||
let data = {
|
||||
heartbeat: heartbeatJSON,
|
||||
monitor: monitorJSON,
|
||||
msg,
|
||||
};
|
||||
let finalData;
|
||||
let config = {};
|
||||
|
||||
if (notification.webhookContentType === "form-data") {
|
||||
finalData = new FormData();
|
||||
finalData.append('data', JSON.stringify(data));
|
||||
|
||||
config = {
|
||||
headers: finalData.getHeaders()
|
||||
}
|
||||
|
||||
} else {
|
||||
finalData = data;
|
||||
}
|
||||
})
|
||||
return true;
|
||||
|
||||
let res = await axios.post(notification.webhookURL, finalData, config)
|
||||
|
||||
console.log(res.data)
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new Error("Notification type is not supported")
|
||||
}
|
||||
|
@@ -12,6 +12,8 @@ const Monitor = require("./model/monitor");
|
||||
const {getSettings} = require("./util-server");
|
||||
const {Notification} = require("./notification")
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
let totalClient = 0;
|
||||
let jwtSecret = null;
|
||||
let monitorList = {};
|
||||
@@ -27,6 +29,13 @@ let monitorList = {};
|
||||
|
||||
app.use('/', express.static("dist"));
|
||||
|
||||
app.post('/test-webhook', function(request, response, next) {
|
||||
console.log("Test Webhook (application/json only)")
|
||||
console.log("Content-Type: " + request.header("Content-Type"))
|
||||
console.log(request.body)
|
||||
response.end();
|
||||
});
|
||||
|
||||
app.get('*', function(request, response, next) {
|
||||
response.sendFile(process.cwd() + '/dist/index.html');
|
||||
});
|
||||
|
Reference in New Issue
Block a user