Merge branch 'main' into globalping

pull/147/head
lyc8503 2025-10-29 20:48:38 +08:00 committed by GitHub
commit f17ef9bad2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 9 deletions

View File

@ -65,7 +65,7 @@ export type WebhookConfig = {
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' method?: 'GET' | 'POST' | 'PUT' | 'PATCH'
headers?: { [key: string]: string | number } headers?: { [key: string]: string | number }
payloadType: 'param' | 'json' | 'x-www-form-urlencoded' payloadType: 'param' | 'json' | 'x-www-form-urlencoded'
payload: { [key: string]: string | number } payload: any
timeout?: number timeout?: number
} }

View File

@ -66,6 +66,18 @@ function formatStatusChangeNotification(
} }
} }
function templateWebhookPlayload(payload: any, message: string) {
for (const key in payload) {
if (Object.prototype.hasOwnProperty.call(payload, key)) {
if (payload[key] === '$MSG') {
payload[key] = message
} else if (typeof payload[key] === 'object' && payload[key] !== null) {
templateWebhookPlayload(payload[key], message)
}
}
}
}
async function webhookNotify(webhook: WebhookConfig, message: string) { async function webhookNotify(webhook: WebhookConfig, message: string) {
console.log( console.log(
'Sending webhook notification: ' + JSON.stringify(message) + ' to webhook ' + webhook.url 'Sending webhook notification: ' + JSON.stringify(message) + ' to webhook ' + webhook.url
@ -74,14 +86,8 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
let url = webhook.url let url = webhook.url
let method = webhook.method let method = webhook.method
let headers = new Headers(webhook.headers as any) let headers = new Headers(webhook.headers as any)
let payloadTemplated: { [key: string]: string | number } = JSON.parse( let payloadTemplated: { [key: string]: string | number } = JSON.parse(JSON.stringify(webhook.payload))
JSON.stringify(webhook.payload) templateWebhookPlayload(payloadTemplated, message)
)
Object.keys(payloadTemplated).forEach((k) => {
if (payloadTemplated[k] === '$MSG') {
payloadTemplated[k] = message
}
})
let body = undefined let body = undefined
switch (webhook.payloadType) { switch (webhook.payloadType) {