From 926ad067c1c0abeb3b2e789c0d67cb3f9542cfd3 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 9 Sep 2024 23:19:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=9A=E7=9F=A5=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=8D=A2=E8=A1=8C=E7=AC=A6=E8=A7=A3=E6=9E=90=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/notify.js | 46 ++++++++++++++++++++++++---------------------- sample/notify.py | 4 +++- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/sample/notify.js b/sample/notify.js index 5899f83e..b879162a 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -1143,14 +1143,20 @@ function webhookNotify(text, desp) { WEBHOOK_CONTENT_TYPE, WEBHOOK_METHOD, } = push_config; - if (!WEBHOOK_URL.includes('$title') && !WEBHOOK_BODY.includes('$title')) { + if ( + !WEBHOOK_METHOD || + !WEBHOOK_URL || + (!WEBHOOK_URL.includes('$title') && !WEBHOOK_BODY.includes('$title')) + ) { resolve(); return; } const headers = parseHeaders(WEBHOOK_HEADERS); const body = parseBody(WEBHOOK_BODY, WEBHOOK_CONTENT_TYPE, (v) => - v?.replaceAll('$title', text)?.replaceAll('$content', desp), + v + ?.replaceAll('$title', text?.replaceAll('\n', '\\n')) + ?.replaceAll('$content', desp?.replaceAll('\n', '\\n')), ); const bodyParam = formatBodyFun(WEBHOOK_CONTENT_TYPE, body); const options = { @@ -1162,27 +1168,23 @@ function webhookNotify(text, desp) { retry: 1, }; - if (WEBHOOK_METHOD) { - const formatUrl = WEBHOOK_URL.replaceAll( - '$title', - encodeURIComponent(text), - ).replaceAll('$content', encodeURIComponent(desp)); - got(formatUrl, options).then((resp) => { - try { - if (resp.statusCode !== 200) { - console.log(`自定义发送通知消息失败😞 ${resp.body}\n`); - } else { - console.log(`自定义发送通知消息成功🎉 ${resp.body}\n`); - } - } catch (e) { - $.logErr(e, resp); - } finally { - resolve(resp.body); + const formatUrl = WEBHOOK_URL.replaceAll( + '$title', + encodeURIComponent(text), + ).replaceAll('$content', encodeURIComponent(desp)); + got(formatUrl, options).then((resp) => { + try { + if (resp.statusCode !== 200) { + console.log(`自定义发送通知消息失败😞 ${resp.body}\n`); + } else { + console.log(`自定义发送通知消息成功🎉 ${resp.body}\n`); } - }); - } else { - resolve(); - } + } catch (e) { + $.logErr(e, resp); + } finally { + resolve(resp.body); + } + }); }); } diff --git a/sample/notify.py b/sample/notify.py index 4c87cd9b..3d88f597 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -852,7 +852,9 @@ def custom_notify(title: str, content: str) -> None: body = parse_body( WEBHOOK_BODY, WEBHOOK_CONTENT_TYPE, - lambda v: v.replace("$title", title).replace("$content", content), + lambda v: v.replace("$title", title.replace("\n", "\\n")).replace( + "$content", content.replace("\n", "\\n") + ), ) formatted_url = WEBHOOK_URL.replace( "$title", urllib.parse.quote_plus(title)