From 90df8e8e36c618868110c3b9a0119b1b69184546 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Tue, 18 Jun 2024 22:35:13 +0800 Subject: [PATCH] feat: add webhook notification --- .../NotificationInfo/strategies/index.ts | 6 +++++ .../NotificationInfo/strategies/webhook.tsx | 20 ++++++++++++++++ .../model/notification/provider/index.ts | 2 ++ .../model/notification/provider/webhook.ts | 23 +++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/client/components/modals/NotificationInfo/strategies/webhook.tsx create mode 100644 src/server/model/notification/provider/webhook.ts diff --git a/src/client/components/modals/NotificationInfo/strategies/index.ts b/src/client/components/modals/NotificationInfo/strategies/index.ts index 3deac36..bea0ba8 100644 --- a/src/client/components/modals/NotificationInfo/strategies/index.ts +++ b/src/client/components/modals/NotificationInfo/strategies/index.ts @@ -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, + }, ]; diff --git a/src/client/components/modals/NotificationInfo/strategies/webhook.tsx b/src/client/components/modals/NotificationInfo/strategies/webhook.tsx new file mode 100644 index 0000000..466a513 --- /dev/null +++ b/src/client/components/modals/NotificationInfo/strategies/webhook.tsx @@ -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 ( + <> + + + + + ); +}); +NotificationWebhook.displayName = 'NotificationWebhook'; diff --git a/src/server/model/notification/provider/index.ts b/src/server/model/notification/provider/index.ts index 41a1160..90ee93c 100644 --- a/src/server/model/notification/provider/index.ts +++ b/src/server/model/notification/provider/index.ts @@ -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 = { @@ -9,4 +10,5 @@ export const notificationProviders: Record = { apprise, telegram, feishu, + webhook, }; diff --git a/src/server/model/notification/provider/webhook.ts b/src/server/model/notification/provider/webhook.ts new file mode 100644 index 0000000..5e21bd9 --- /dev/null +++ b/src/server/model/notification/provider/webhook.ts @@ -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, + }); + }, +};