mirror of
https://github.com/whyour/qinglong.git
synced 2026-07-01 04:40:38 +08:00
修改自定义通知 body 解析逻辑
This commit is contained in:
+8
-3
@@ -360,7 +360,10 @@ export function parseHeaders(headers: string) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parseString(input: string): Record<string, string> {
|
||||
function parseString(
|
||||
input: string,
|
||||
valueFormatFn?: (v: string) => string,
|
||||
): Record<string, string> {
|
||||
const regex = /(\w+):\s*((?:(?!\n\w+:).)*)/g;
|
||||
const matches: Record<string, string> = {};
|
||||
|
||||
@@ -372,9 +375,10 @@ function parseString(input: string): Record<string, string> {
|
||||
continue;
|
||||
}
|
||||
|
||||
const _value = value.trim();
|
||||
let _value = value.trim();
|
||||
|
||||
try {
|
||||
_value = valueFormatFn ? valueFormatFn(_value) : _value;
|
||||
const jsonValue = JSON.parse(_value);
|
||||
matches[_key] = jsonValue;
|
||||
} catch (error) {
|
||||
@@ -392,12 +396,13 @@ export function parseBody(
|
||||
| 'multipart/form-data'
|
||||
| 'application/x-www-form-urlencoded'
|
||||
| 'text/plain',
|
||||
valueFormatFn?: (v: string) => string,
|
||||
) {
|
||||
if (contentType === 'text/plain' || !body) {
|
||||
return body;
|
||||
}
|
||||
|
||||
const parsed = parseString(body);
|
||||
const parsed = parseString(body, valueFormatFn);
|
||||
|
||||
switch (contentType) {
|
||||
case 'multipart/form-data':
|
||||
|
||||
+7
-22
@@ -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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user