修复系统通知错误提示,gotifyPriority 配置参数

This commit is contained in:
whyour 2023-05-03 18:18:28 +08:00
parent 77dc7817fb
commit 9db0095e29
3 changed files with 246 additions and 169 deletions

View File

@ -36,7 +36,7 @@ export default class NotificationService {
private content = ''; private content = '';
private params!: Omit<NotificationInfo, 'type'>; private params!: Omit<NotificationInfo, 'type'>;
private gotOption = { private gotOption = {
timeout: 30000, timeout: 10000,
retry: 1, retry: 1,
}; };
@ -78,33 +78,41 @@ export default class NotificationService {
} }
private async gotify() { private async gotify() {
const { gotifyUrl, gotifyToken, gotifyPriority } = this.params; const { gotifyUrl, gotifyToken, gotifyPriority = 1 } = this.params;
const res: any = await got try {
.post(`${gotifyUrl}/message?token=${gotifyToken}`, { const res: any = await got
...this.gotOption, .post(`${gotifyUrl}/message?token=${gotifyToken}`, {
body: `title=${encodeURIComponent( ...this.gotOption,
this.title, body: `title=${encodeURIComponent(
)}&message=${encodeURIComponent( this.title,
this.content, )}&message=${encodeURIComponent(
)}&priority=${gotifyPriority}`, this.content,
headers: { )}&priority=${gotifyPriority}`,
'Content-Type': 'application/x-www-form-urlencoded', headers: {
}, 'Content-Type': 'application/x-www-form-urlencoded',
}) },
.json(); })
return typeof res.id === 'number'; .json();
return typeof res.id === 'number';
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async goCqHttpBot() { private async goCqHttpBot() {
const { goCqHttpBotQq, goCqHttpBotToken, goCqHttpBotUrl } = this.params; const { goCqHttpBotQq, goCqHttpBotToken, goCqHttpBotUrl } = this.params;
const res: any = await got try {
.post(`${goCqHttpBotUrl}?${goCqHttpBotQq}`, { const res: any = await got
...this.gotOption, .post(`${goCqHttpBotUrl}?${goCqHttpBotQq}`, {
json: { message: `${this.title}\n${this.content}` }, ...this.gotOption,
headers: { Authorization: 'Bearer ' + goCqHttpBotToken }, json: { message: `${this.title}\n${this.content}` },
}) headers: { Authorization: 'Bearer ' + goCqHttpBotToken },
.json(); })
return res.retcode === 0; .json();
return res.retcode === 0;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async serverChan() { private async serverChan() {
@ -112,49 +120,61 @@ export default class NotificationService {
const url = serverChanKey.startsWith('SCT') const url = serverChanKey.startsWith('SCT')
? `https://sctapi.ftqq.com/${serverChanKey}.send` ? `https://sctapi.ftqq.com/${serverChanKey}.send`
: `https://sc.ftqq.com/${serverChanKey}.send`; : `https://sc.ftqq.com/${serverChanKey}.send`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
body: `title=${this.title}&desp=${this.content}`, ...this.gotOption,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `title=${this.title}&desp=${this.content}`,
}) headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
.json(); })
return res.errno === 0 || res.data.errno === 0; .json();
return res.errno === 0 || res.data.errno === 0;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async pushDeer() { private async pushDeer() {
const { pushDeerKey, pushDeerUrl } = this.params; const { pushDeerKey, pushDeerUrl } = this.params;
const url = pushDeerUrl || `https://api2.pushdeer.com/message/push`; const url = pushDeerUrl || `https://api2.pushdeer.com/message/push`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
body: `pushkey=${pushDeerKey}&text=${encodeURIComponent( ...this.gotOption,
this.title, body: `pushkey=${pushDeerKey}&text=${encodeURIComponent(
)}&desp=${encodeURIComponent(this.content)}&type=markdown`, this.title,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, )}&desp=${encodeURIComponent(this.content)}&type=markdown`,
}) headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
.json(); })
return ( .json();
res.content.result.length !== undefined && res.content.result.length > 0 return (
); res.content.result.length !== undefined && res.content.result.length > 0
);
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async chat() { private async chat() {
const { chatUrl, chatToken } = this.params; const { chatUrl, chatToken } = this.params;
const url = `${chatUrl}${chatToken}`; const url = `${chatUrl}${chatToken}`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
body: `payload={"text":"${this.title}\n${this.content}"}`, ...this.gotOption,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `payload={"text":"${this.title}\n${this.content}"}`,
}) headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
.json(); })
return res.success; .json();
return res.success;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async bark() { private async bark() {
let { barkPush, barkIcon, barkSound, barkGroup } = this.params; let { barkPush, barkIcon, barkSound, barkGroup } = this.params;
if (!barkPush.startsWith('http') && !barkPush.startsWith('https')) { if (!barkPush.startsWith('http')) {
barkPush = `https://api.day.app/${barkPush}`; barkPush = `https://api.day.app/${barkPush}`;
} }
const url = `${barkPush}/${encodeURIComponent( const url = `${barkPush}/${encodeURIComponent(
@ -162,13 +182,18 @@ export default class NotificationService {
)}/${encodeURIComponent( )}/${encodeURIComponent(
this.content, this.content,
)}?icon=${barkIcon}&sound=${barkSound}&group=${barkGroup}`; )}?icon=${barkIcon}&sound=${barkSound}&group=${barkGroup}`;
const res: any = await got
.get(url, { try {
...this.gotOption, const res: any = await got
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, .get(url, {
}) ...this.gotOption,
.json(); headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
return res.code === 200; })
.json();
return res.code === 200;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async telegramBot() { private async telegramBot() {
@ -200,15 +225,19 @@ export default class NotificationService {
https: httpsAgent, https: httpsAgent,
}; };
} }
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
body: `chat_id=${telegramBotUserId}&text=${this.title}\n\n${this.content}&disable_web_page_preview=true`, ...this.gotOption,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `chat_id=${telegramBotUserId}&text=${this.title}\n\n${this.content}&disable_web_page_preview=true`,
agent, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
}) agent,
.json(); })
return !!res.ok; .json();
return !!res.ok;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async dingtalkBot() { private async dingtalkBot() {
@ -222,35 +251,43 @@ export default class NotificationService {
secretParam = `&timestamp=${dateNow}&sign=${result}`; secretParam = `&timestamp=${dateNow}&sign=${result}`;
} }
const url = `https://oapi.dingtalk.com/robot/send?access_token=${dingtalkBotToken}${secretParam}`; const url = `https://oapi.dingtalk.com/robot/send?access_token=${dingtalkBotToken}${secretParam}`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
json: { ...this.gotOption,
msgtype: 'text', json: {
text: { msgtype: 'text',
content: ` ${this.title}\n\n${this.content}`, text: {
content: ` ${this.title}\n\n${this.content}`,
},
}, },
}, })
}) .json();
.json(); return res.errcode === 0;
return res.errcode === 0; } catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async weWorkBot() { private async weWorkBot() {
const { weWorkBotKey } = this.params; const { weWorkBotKey } = this.params;
const url = `https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${weWorkBotKey}`; const url = `https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${weWorkBotKey}`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
json: { ...this.gotOption,
msgtype: 'text', json: {
text: { msgtype: 'text',
content: ` ${this.title}\n\n${this.content}`, text: {
content: ` ${this.title}\n\n${this.content}`,
},
}, },
}, })
}) .json();
.json(); return res.errcode === 0;
return res.errcode === 0; } catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async weWorkApp() { private async weWorkApp() {
@ -306,22 +343,26 @@ export default class NotificationService {
break; break;
} }
const res: any = await got try {
.post( const res: any = await got
`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${tokenRes.access_token}`, .post(
{ `https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${tokenRes.access_token}`,
...this.gotOption, {
json: { ...this.gotOption,
touser, json: {
agentid, touser,
safe: '0', agentid,
...options, safe: '0',
...options,
},
}, },
}, )
) .json();
.json();
return res.errcode === 0; return res.errcode === 0;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async aibotk() { private async aibotk() {
@ -353,85 +394,111 @@ export default class NotificationService {
break; break;
} }
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
json: { ...this.gotOption,
...json, json: {
}, ...json,
}) },
.json(); })
.json();
return res.code === 0; return res.code === 0;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async iGot() { private async iGot() {
const { iGotPushKey } = this.params; const { iGotPushKey } = this.params;
const url = `https://push.hellyw.com/${iGotPushKey.toLowerCase()}`; const url = `https://push.hellyw.com/${iGotPushKey.toLowerCase()}`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
body: `title=${this.title}&content=${this.content}`, ...this.gotOption,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `title=${this.title}&content=${this.content}`,
}) headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
.json(); })
.json();
return res.ret === 0; return res.ret === 0;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async pushPlus() { private async pushPlus() {
const { pushPlusToken, pushPlusUser } = this.params; const { pushPlusToken, pushPlusUser } = this.params;
const url = `https://www.pushplus.plus/send`; const url = `https://www.pushplus.plus/send`;
const res: any = await got try {
.post(url, { const res: any = await got
...this.gotOption, .post(url, {
json: { ...this.gotOption,
token: `${pushPlusToken}`, json: {
title: `${this.title}`, token: `${pushPlusToken}`,
content: `${this.content.replace(/[\n\r]/g, '<br>')}`, title: `${this.title}`,
topic: `${pushPlusUser || ''}`, content: `${this.content.replace(/[\n\r]/g, '<br>')}`,
}, topic: `${pushPlusUser || ''}`,
}) },
.json(); })
.json();
return res.code === 200; return res.code === 200;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async lark() { private async lark() {
const { larkKey } = this.params; let { larkKey } = this.params;
const res: any = await got
.post(`https://open.feishu.cn/open-apis/bot/v2/hook/${larkKey}`, { if (!larkKey.startsWith('http')) {
...this.gotOption, larkKey = `https://open.feishu.cn/open-apis/bot/v2/hook/${larkKey}`;
json: { }
msg_type: 'text',
content: { text: `${this.title}\n\n${this.content}` }, try {
}, const res: any = await got
headers: { 'Content-Type': 'application/json' }, .post(larkKey, {
}) ...this.gotOption,
.json(); json: {
return res.StatusCode === 0; msg_type: 'text',
content: { text: `${this.title}\n\n${this.content}` },
},
headers: { 'Content-Type': 'application/json' },
})
.json();
return res.StatusCode === 0;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async email() { private async email() {
const { emailPass, emailService, emailUser } = this.params; const { emailPass, emailService, emailUser } = this.params;
const transporter = nodemailer.createTransport({
service: emailService,
auth: {
user: emailUser,
pass: emailPass,
},
});
const info = await transporter.sendMail({ try {
from: `"青龙快讯" <${emailUser}>`, const transporter = nodemailer.createTransport({
to: `${emailUser}`, service: emailService,
subject: `${this.title}`, auth: {
html: `${this.content.replace(/\n/g, '<br/>')}`, user: emailUser,
}); pass: emailPass,
},
});
transporter.close(); const info = await transporter.sendMail({
from: `"青龙快讯" <${emailUser}>`,
to: `${emailUser}`,
subject: `${this.title}`,
html: `${this.content.replace(/\n/g, '<br/>')}`,
});
return !!info.messageId; transporter.close();
return !!info.messageId;
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private async webhook() { private async webhook() {
@ -460,8 +527,12 @@ export default class NotificationService {
allowGetBody: true, allowGetBody: true,
...bodyParam, ...bodyParam,
}; };
const res = await got(formatUrl, options); try {
return String(res.statusCode).startsWith('20'); const res = await got(formatUrl, options);
return String(res.statusCode).startsWith('20');
} catch (error: any) {
throw new Error(error.response ? error.response.body : error);
}
} }
private formatBody(contentType: string, body: any): object { private formatBody(contentType: string, body: any): object {

View File

@ -449,7 +449,7 @@ patch_version() {
fi fi
if [[ $NpmMirror ]]; then if [[ $NpmMirror ]]; then
cd && pnpm config set registry $NpmMirror cd && pnpm config set registry $NpmMirror
pnpm install -g --prefer-offline --force pnpm install -g --force
fi fi
git config --global pull.rebase false git config --global pull.rebase false

View File

@ -12,6 +12,7 @@ const NotificationSetting = ({ data }: any) => {
const [form] = Form.useForm(); const [form] = Form.useForm();
const handleOk = (values: any) => { const handleOk = (values: any) => {
setLoading(true);
const { type } = values; const { type } = values;
if (type == 'closed') { if (type == 'closed') {
values.type = ''; values.type = '';
@ -30,7 +31,8 @@ const NotificationSetting = ({ data }: any) => {
}) })
.catch((error: any) => { .catch((error: any) => {
console.log(error); console.log(error);
}); })
.finally(() => setLoading(false));
}; };
const notificationModeChange = (value: string) => { const notificationModeChange = (value: string) => {
@ -56,7 +58,7 @@ const NotificationSetting = ({ data }: any) => {
style={{ maxWidth: 400 }} style={{ maxWidth: 400 }}
initialValue={notificationMode} initialValue={notificationMode}
> >
<Select onChange={notificationModeChange}> <Select onChange={notificationModeChange} disabled={loading}>
{config.notificationModes.map((x) => ( {config.notificationModes.map((x) => (
<Option key={x.value} value={x.value}> <Option key={x.value} value={x.value}>
{x.label} {x.label}
@ -74,7 +76,10 @@ const NotificationSetting = ({ data }: any) => {
style={{ maxWidth: 400 }} style={{ maxWidth: 400 }}
> >
{x.items ? ( {x.items ? (
<Select placeholder={x.placeholder || `请选择${x.label}`}> <Select
placeholder={x.placeholder || `请选择${x.label}`}
disabled={loading}
>
{x.items.map((y) => ( {x.items.map((y) => (
<Option key={y.value} value={y.value}> <Option key={y.value} value={y.value}>
{y.label || y.value} {y.label || y.value}
@ -83,14 +88,15 @@ const NotificationSetting = ({ data }: any) => {
</Select> </Select>
) : ( ) : (
<Input.TextArea <Input.TextArea
disabled={loading}
autoSize={true} autoSize={true}
placeholder={x.placeholder || `请输入${x.label}`} placeholder={x.placeholder || `请输入${x.label}`}
/> />
)} )}
</Form.Item> </Form.Item>
))} ))}
<Button type="primary" htmlType="submit"> <Button type="primary" htmlType="submit" disabled={loading}>
{loading ? '测试中...' : '保存'}
</Button> </Button>
</Form> </Form>
</div> </div>