Skip to content

Commit

Permalink
修复通知文件换行符解析错误
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Sep 9, 2024
1 parent ff98c3a commit fe8b0f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
47 changes: 25 additions & 22 deletions sample/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
}
});
});
}

Expand Down
9 changes: 6 additions & 3 deletions sample/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit fe8b0f6

Please sign in to comment.