修复自定义通知

This commit is contained in:
whyour 2022-09-20 19:13:11 +08:00
parent e08f0cd308
commit f23e7a9e49
3 changed files with 22 additions and 8 deletions

View File

@ -389,8 +389,12 @@ export default class NotificationService {
const { webhookUrl, webhookBody, webhookHeaders, webhookMethod, webhookContentType } =
this.params;
const { formatBody, formatUrl } = this.formatNotifyContent(webhookUrl, webhookBody);
if (!formatUrl && !formatBody) {
return false;
}
const headers = parseHeaders(webhookHeaders);
const body = parseBody(webhookBody, webhookContentType);
const body = parseBody(formatBody, webhookContentType);
const bodyParam = this.formatBody(webhookContentType, body);
const options = {
method: webhookMethod,
@ -400,7 +404,7 @@ export default class NotificationService {
allowGetBody: true,
...bodyParam
}
const res = await got(webhookUrl, options);
const res = await got(formatUrl, options);
return String(res.statusCode).startsWith('20');
}
@ -416,4 +420,15 @@ 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),
}
}
}

View File

@ -2,7 +2,6 @@ import React, { useEffect, useState } from 'react';
import { Typography, Input, Form, Button, Select, message } from 'antd';
import { request } from '@/utils/http';
import config from '@/utils/config';
import { parseBody, parseHeaders } from '@/utils';
const { Option } = Select;
@ -28,7 +27,7 @@ const NotificationSetting = ({ data }: any) => {
if (_data && _data.code === 200) {
message.success(values.type ? '通知发送成功' : '通知关闭成功');
} else {
message.error(_data.data);
message.error(_data.message);
}
})
.catch((error: any) => {

View File

@ -238,9 +238,9 @@ export default {
},
{
label: 'webhookUrl',
tip: '请求链接以http或者https开头',
tip: '请求链接以http或者https开头。url或者body中必须包含$title$content可选对应api内容的位置',
required: true,
placeholder: 'https://xxx.cn/api?query=xxx\n',
placeholder: 'https://xxx.cn/api?content=$title\n',
},
{
label: 'webhookHeaders',
@ -249,8 +249,8 @@ export default {
},
{
label: 'webhookBody',
tip: '请求体格式key1: value1多个换行分割',
placeholder: 'key1: value1\nkey2: value2',
tip: '请求体格式key1: value1多个换行分割。url或者body中必须包含$title$content可选对应api内容的位置',
placeholder: 'key1: $title\nkey2: $content',
},
],
},