From fe8b0f667fd2a2f834089f05cf0d25c26081469b Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 9 Sep 2024 23:15:35 +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 | 47 +++++++++++++++++++++++++---------------------- sample/notify.py | 9 ++++++--- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/sample/notify.js b/sample/notify.js index 5899f83e775..30de9e58dfa 100644 --- a/sample/notify.js +++ b/sample/notify.js @@ -1143,14 +1143,21 @@ 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 _text = text?.replaceAll('\n', '\\n'); + const _desp = desp?.replaceAll('\n', '\\n'); const body = parseBody(WEBHOOK_BODY, WEBHOOK_CONTENT_TYPE, (v) => - v?.replaceAll('$title', text)?.replaceAll('$content', desp), + v?.replaceAll('$title', _text)?.replaceAll('$content', _desp), ); const bodyParam = formatBodyFun(WEBHOOK_CONTENT_TYPE, body); const options = { @@ -1162,27 +1169,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 4c87cd9bafe..9a71336a5b5 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -849,14 +849,17 @@ def custom_notify(title: str, content: str) -> None: return headers = parse_headers(WEBHOOK_HEADERS) + _title = title.replace('\n', '\\n') if text else None + _content = content.replace('\n', '\\n') if desp else 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("$content", _content), ) formatted_url = WEBHOOK_URL.replace( - "$title", urllib.parse.quote_plus(title) - ).replace("$content", urllib.parse.quote_plus(content)) + "$title", urllib.parse.quote_plus(_title) + ).replace("$content", urllib.parse.quote_plus(_content)) response = requests.request( method=WEBHOOK_METHOD, url=formatted_url, headers=headers, timeout=15, data=body )