From 4c79cf1186ab82c0e5158b0c3c5a4dffc808bec1 Mon Sep 17 00:00:00 2001 From: michel_929 Date: Sun, 10 Nov 2024 20:30:27 +0100 Subject: [PATCH 1/4] Create Webhook Option Completly Custom An option that allows you to send two different post requests depending on the status (UP and DOWN) --- server/notification-providers/webhook.js | 42 +++++++++++++- src/components/notifications/Webhook.vue | 73 ++++++++++++++++++++++-- src/lang/en.json | 9 ++- 3 files changed, 116 insertions(+), 8 deletions(-) diff --git a/server/notification-providers/webhook.js b/server/notification-providers/webhook.js index 986986d44..d32e45b6b 100644 --- a/server/notification-providers/webhook.js +++ b/server/notification-providers/webhook.js @@ -21,6 +21,7 @@ class Webhook extends NotificationProvider { let config = { headers: {} }; + let url = notification.webhookURL; if (notification.webhookContentType === "form-data") { const formData = new FormData(); @@ -28,6 +29,8 @@ class Webhook extends NotificationProvider { config.headers = formData.getHeaders(); data = formData; } else if (notification.webhookContentType === "custom") { + + console.log(msg); // Initialize LiquidJS and parse the custom Body Template const engine = new Liquid(); const tpl = engine.parse(notification.webhookCustomBody); @@ -39,9 +42,44 @@ class Webhook extends NotificationProvider { heartbeatJSON, monitorJSON }); + }else if(notification.webhookContentType === "CompletlyCustom"){ + + if(msg.includes("Down")){ + const tpl = JSON.parse(notification.webhookCustomBodyDown); + // Insert templated values into Body + data = tpl; + url = notification.webhookURLDown; + + if (notification.webhookAdditionalHeaders) { + try { + config.headers = { + ...config.headers, + ...JSON.parse(notification.webhookAdditionalHeadersDown) + }; + } catch (err) { + throw "Additional Headers is not a valid JSON"; + } + } + }else { + const tpl = JSON.parse(notification.webhookCustomBodyUp); + // Insert templated values into Body + data = tpl; + url = notification.webhookURLUp; + + if (notification.webhookAdditionalHeaders) { + try { + config.headers = { + ...config.headers, + ...JSON.parse(notification.webhookAdditionalHeadersUp) + }; + } catch (err) { + throw "Additional Headers is not a valid JSON"; + } + } + } } - if (notification.webhookAdditionalHeaders) { + if (notification.webhookAdditionalHeaders && notification.webhookContentType != "CompletlyCustom") { try { config.headers = { ...config.headers, @@ -52,7 +90,7 @@ class Webhook extends NotificationProvider { } } - await axios.post(notification.webhookURL, data, config); + await axios.post(url, data, config); return okMsg; } catch (error) { diff --git a/src/components/notifications/Webhook.vue b/src/components/notifications/Webhook.vue index 8c67a2745..a54668d60 100644 --- a/src/components/notifications/Webhook.vue +++ b/src/components/notifications/Webhook.vue @@ -1,16 +1,37 @@ + + +
@@ -56,13 +100,32 @@
{{ $t("webhookAdditionalHeadersDesc") }}
+ +
+ + + + +
diff --git a/src/lang/en.json b/src/lang/en.json index e215f1031..170eca7f1 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1051,5 +1051,12 @@ "RabbitMQ Password": "RabbitMQ Password", "rabbitmqHelpText": "To use the monitor, you will need to enable the Management Plugin in your RabbitMQ setup. For more information, please consult the {rabitmq_documentation}.", "SendGrid API Key": "SendGrid API Key", - "Separate multiple email addresses with commas": "Separate multiple email addresses with commas" + "Separate multiple email addresses with commas": "Separate multiple email addresses with commas", + "Post URL DOWN": "Post URL Status = Down", + "Post URL UP": "Post URL Status = UP", + "Body UP": "Body Status = UP", + "Body DOWN": "Body Status = DOWN", + "Header UP": "Header Status = UP", + "Header DOWN": "Header Status = Down", + "Completly Custom": "Completly Custom Post Request" } From ae0d07737ee58d65b8cff93643b41547a2f31fb6 Mon Sep 17 00:00:00 2001 From: michel_929 Date: Sun, 10 Nov 2024 20:41:19 +0100 Subject: [PATCH 2/4] Edit webhook.js and Webhook.vue Check linters --- server/notification-providers/webhook.js | 8 ++++---- src/components/notifications/Webhook.vue | 25 ++++++++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/server/notification-providers/webhook.js b/server/notification-providers/webhook.js index d32e45b6b..156c40cdb 100644 --- a/server/notification-providers/webhook.js +++ b/server/notification-providers/webhook.js @@ -42,9 +42,9 @@ class Webhook extends NotificationProvider { heartbeatJSON, monitorJSON }); - }else if(notification.webhookContentType === "CompletlyCustom"){ + } else if (notification.webhookContentType === "CompletlyCustom") { - if(msg.includes("Down")){ + if (msg.includes("Down")) { const tpl = JSON.parse(notification.webhookCustomBodyDown); // Insert templated values into Body data = tpl; @@ -60,7 +60,7 @@ class Webhook extends NotificationProvider { throw "Additional Headers is not a valid JSON"; } } - }else { + } else { const tpl = JSON.parse(notification.webhookCustomBodyUp); // Insert templated values into Body data = tpl; @@ -79,7 +79,7 @@ class Webhook extends NotificationProvider { } } - if (notification.webhookAdditionalHeaders && notification.webhookContentType != "CompletlyCustom") { + if (notification.webhookAdditionalHeaders && notification.webhookContentType !== "CompletlyCustom") { try { config.headers = { ...config.headers, diff --git a/src/components/notifications/Webhook.vue b/src/components/notifications/Webhook.vue index a54668d60..28bcaccae 100644 --- a/src/components/notifications/Webhook.vue +++ b/src/components/notifications/Webhook.vue @@ -1,16 +1,16 @@ -