diff --git a/package-lock.json b/package-lock.json index cdae49c8e..d0a518585 100644 --- a/package-lock.json +++ b/package-lock.json @@ -278,8 +278,7 @@ "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "optional": true + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "atob": { "version": "2.1.2", @@ -575,7 +574,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "optional": true, "requires": { "delayed-stream": "~1.0.0" } @@ -740,8 +738,7 @@ "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "optional": true + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, "delegates": { "version": "1.0.0", @@ -1170,13 +1167,12 @@ "optional": true }, "form-data": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz", - "integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==", - "optional": true, + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", "requires": { "asynckit": "^0.4.0", - "combined-stream": "^1.0.6", + "combined-stream": "^1.0.8", "mime-types": "^2.1.12" } }, @@ -2760,6 +2756,17 @@ "uuid": "^3.3.2" }, "dependencies": { + "form-data": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-2.3.3.tgz", + "integrity": "sha512-1lLKB2Mu3aGP1Q/2eCOx0fNbRMe7XdwktwOruhfqqd0rIJWwN4Dh+E3hrPSlDCXnSR7UtZ1N38rVXm+6+MEhJQ==", + "optional": true, + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.6", + "mime-types": "^2.1.12" + } + }, "qs": { "version": "6.5.2", "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", diff --git a/package.json b/package.json index 359b2fe25..a1e706665 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "bootstrap": "^5.0.0", "dayjs": "^1.10.4", "express": "^4.17.1", + "form-data": "^4.0.0", "jsonwebtoken": "^8.5.1", "password-hash": "^1.2.2", "redbean-node": "0.0.20", diff --git a/server/model/monitor.js b/server/model/monitor.js index f92e43e7e..97797e09b 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -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); diff --git a/server/notification.js b/server/notification.js index de9c30b9f..d5adb5d48 100644 --- a/server/notification.js +++ b/server/notification.js @@ -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") } diff --git a/server/server.js b/server/server.js index 27a4951d3..a958a247a 100644 --- a/server/server.js +++ b/server/server.js @@ -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'); }); diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 2dc58eb39..bdc537359 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -10,7 +10,6 @@
+ You can get your chat id by sending message to the bot and go to this url to view the chat_id: +
+ ++ + + {{ telegramGetUpdatesURL }} + + + + {{ telegramGetUpdatesURL }} + +
+- You can get your chat id by sending message to the bot and go to this url to view the chat_id: -
- -- - - {{ telegramGetUpdatesURL }} - - - - {{ telegramGetUpdatesURL }} - -
+"application/json" is good for any modern http servers such as express.js
+"multipart/form-data" is good for PHP, you just need to parse the json by json_decode($_POST['data'])