feat: add webhook notification

This commit is contained in:
moonrailgun 2024-06-18 22:35:13 +08:00
parent ee16e6cd76
commit 90df8e8e36
4 changed files with 51 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import { NotificationSMTP } from './smtp';
import { NotificationTelegram } from './telegram';
import { NotificationApprise } from './apprise';
import { NotificationFeishu } from './feishu';
import { NotificationWebhook } from './webhook';
interface NotificationStrategy {
label: string;
@ -31,4 +32,9 @@ export const notificationStrategies: NotificationStrategy[] = [
name: 'feishu',
form: NotificationFeishu,
},
{
label: 'Webhook',
name: 'webhook',
form: NotificationWebhook,
},
];

View File

@ -0,0 +1,20 @@
import { Form, Input } from 'antd';
import React from 'react';
import { useTranslation } from '@i18next-toolkit/react';
export const NotificationWebhook: React.FC = React.memo(() => {
const { t } = useTranslation();
return (
<>
<Form.Item
label={t('Webhook URL')}
name={['payload', 'webhookUrl']}
rules={[{ required: true }]}
>
<Input placeholder={t('For example: https://example.com/callback')} />
</Form.Item>
</>
);
});
NotificationWebhook.displayName = 'NotificationWebhook';

View File

@ -2,6 +2,7 @@ import { apprise } from './apprise';
import { feishu } from './feishu';
import { smtp } from './smtp';
import { telegram } from './telegram';
import { webhook } from './webhook';
import type { NotificationProvider } from './type';
export const notificationProviders: Record<string, NotificationProvider> = {
@ -9,4 +10,5 @@ export const notificationProviders: Record<string, NotificationProvider> = {
apprise,
telegram,
feishu,
webhook,
};

View File

@ -0,0 +1,23 @@
import { NotificationProvider } from './type';
import { baseContentTokenizer } from '../token';
import axios from 'axios';
interface WebhookPayload {
webhookUrl: string;
}
// Fork from https://github.com/louislam/uptime-kuma/blob/HEAD/server/notification-providers/smtp.js
export const webhook: NotificationProvider = {
send: async (notification, title, message) => {
const payload = notification.payload as unknown as WebhookPayload;
const webhookUrl = payload.webhookUrl;
const content = baseContentTokenizer.parse(message);
await axios.post(webhookUrl, {
notification,
content,
raw: message,
});
},
};