修改自定义通知 body 解析逻辑

This commit is contained in:
whyour
2024-03-12 22:22:10 +08:00
parent 68ad01e0e8
commit 2ac4581d54
4 changed files with 132 additions and 112 deletions
+7 -22
View File
@@ -656,17 +656,14 @@ export default class NotificationService {
webhookContentType,
} = this.params;
const { formatBody, formatUrl } = this.formatNotifyContent(
webhookUrl,
webhookBody,
);
if (!formatUrl && !formatBody) {
if (!webhookUrl.includes('$title') && !webhookBody.includes('$title')) {
throw new Error('Url 或者 Body 中必须包含 $title');
}
const headers = parseHeaders(webhookHeaders);
const body = parseBody(formatBody, webhookContentType);
const body = parseBody(webhookBody, webhookContentType, (v) =>
v?.replaceAll('$title', this.title)?.replaceAll('$content', this.content),
);
const bodyParam = this.formatBody(webhookContentType, body);
const options = {
method: webhookMethod,
@@ -676,6 +673,9 @@ export default class NotificationService {
...bodyParam,
};
try {
const formatUrl = webhookUrl
?.replaceAll('$title', encodeURIComponent(this.title))
?.replaceAll('$content', encodeURIComponent(this.content));
const res = await got(formatUrl, options);
if (String(res.statusCode).startsWith('20')) {
return true;
@@ -700,19 +700,4 @@ export default class NotificationService {
}
return {};
}
private formatNotifyContent(url: string, body: string) {
if (!url.includes('$title') && !body.includes('$title')) {
return {};
}
return {
formatUrl: url
?.replaceAll('$title', encodeURIComponent(this.title))
?.replaceAll('$content', encodeURIComponent(this.content)),
formatBody: body
?.replaceAll('$title', this.title)
?.replaceAll('$content', this.content),
};
}
}