From 20e7f47b5e648d73a729d57949e29f4ff2ad839f Mon Sep 17 00:00:00 2001 From: lyc8503 Date: Fri, 17 Oct 2025 01:46:19 +0800 Subject: [PATCH] fix #148: handle nested webhook payload --- types/config.ts | 2 +- worker/src/util.ts | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/types/config.ts b/types/config.ts index b512642..6ba8c2d 100644 --- a/types/config.ts +++ b/types/config.ts @@ -65,7 +65,7 @@ export type WebhookConfig = { method?: 'GET' | 'POST' | 'PUT' | 'PATCH' headers?: { [key: string]: string | number } payloadType: 'param' | 'json' | 'x-www-form-urlencoded' - payload: { [key: string]: string | number } + payload: any timeout?: number } diff --git a/worker/src/util.ts b/worker/src/util.ts index 4094118..f9b33c3 100644 --- a/worker/src/util.ts +++ b/worker/src/util.ts @@ -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) { console.log( 'Sending webhook notification: ' + JSON.stringify(message) + ' to webhook ' + webhook.url @@ -75,11 +87,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) { let method = webhook.method let headers = new Headers(webhook.headers as any) let payloadTemplated: { [key: string]: string | number } = JSON.parse(JSON.stringify(webhook.payload)) - Object.keys(payloadTemplated).forEach((k) => { - if (payloadTemplated[k] === '$MSG') { - payloadTemplated[k] = message - } - }) + templateWebhookPlayload(payloadTemplated, message) let body = undefined switch (webhook.payloadType) {