diff --git a/back/data/notify.ts b/back/data/notify.ts index 271e6348..6a3ccc7c 100644 --- a/back/data/notify.ts +++ b/back/data/notify.ts @@ -95,6 +95,7 @@ export class WebhookNotification extends NotificationBaseInfo { public webhookBody: any = {}; public webhookUrl: string = ''; public webhookMethod: 'GET' | 'POST' | 'PUT' = 'GET'; + public webhookContentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded' = 'application/json'; } export interface NotificationInfo diff --git a/back/services/notify.ts b/back/services/notify.ts index daa7d4c6..538bd19b 100644 --- a/back/services/notify.ts +++ b/back/services/notify.ts @@ -34,7 +34,7 @@ export default class NotificationService { private content = ''; private params!: Omit; - constructor(@Inject('logger') private logger: winston.Logger) {} + constructor(@Inject('logger') private logger: winston.Logger) { } public async notify( title: string, @@ -181,9 +181,8 @@ export default class NotificationService { telegramBotUserId, } = this.params; const authStr = telegramBotProxyAuth ? `${telegramBotProxyAuth}@` : ''; - const url = `https://${ - telegramBotApiHost ? telegramBotApiHost : 'api.telegram.org' - }/bot${telegramBotToken}/sendMessage`; + const url = `https://${telegramBotApiHost ? telegramBotApiHost : 'api.telegram.org' + }/bot${telegramBotToken}/sendMessage`; let agent; if (telegramBotProxyHost && telegramBotProxyPort) { const options: any = { @@ -386,17 +385,32 @@ export default class NotificationService { } private async webhook() { - const { webhookUrl, webhookBody, webhookHeaders, webhookMethod } = + const { webhookUrl, webhookBody, webhookHeaders, webhookMethod, webhookContentType } = this.params; - const { statusCode } = await got(webhookUrl, { + const bodyParam = this.formatBody(webhookContentType, webhookBody); + const options = { method: webhookMethod, headers: webhookHeaders, - body: webhookBody, timeout: this.timeout, retry: 0, - }); + allowGetBody: true, + ...bodyParam + } + const res = await got(webhookUrl, options); + return String(res.statusCode).startsWith('20'); + } - return String(statusCode).includes('20'); + private formatBody(contentType: string, body: any): object { + if (!body) return {}; + switch (contentType) { + case 'application/json': + return { json: body }; + case 'multipart/form-data': + return { form: body }; + case 'application/x-www-form-urlencoded': + return { body }; + } + return {}; } } diff --git a/src/pages/setting/notification.tsx b/src/pages/setting/notification.tsx index f98536c6..885ffbc0 100644 --- a/src/pages/setting/notification.tsx +++ b/src/pages/setting/notification.tsx @@ -2,6 +2,7 @@ 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; @@ -12,9 +13,15 @@ const NotificationSetting = ({ data }: any) => { const [form] = Form.useForm(); const handleOk = (values: any) => { - if (values.type == 'closed') { + const { type, webhookBody, webhookContentType } = values; + if (type == 'closed') { values.type = ''; } + + if (type === 'webhook') { + values.webhookHeaders = { ...parseHeaders(values.webhookHeaders) }; + values.webhookBody = parseBody(webhookBody, webhookContentType); + } request .put(`${config.apiPrefix}user/notification`, { data: { @@ -73,10 +80,22 @@ const NotificationSetting = ({ data }: any) => { rules={[{ required: x.required }]} style={{ maxWidth: 400 }} > - + { + x.items ? ( + + ) : ( + + ) + } ))}