Skip to content

Commit

Permalink
feat: add feishu notification
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed May 2, 2024
1 parent cc910b7 commit f6fc210
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
@@ -0,0 +1,33 @@
import { Form, Input } from 'antd';
import React from 'react';
import { useTranslation } from '@i18next-toolkit/react';

export const NotificationFeishu: 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://open.feishu.cn/open-apis/bot/v2/hook/00000000-0000-0000-0000-000000000000'
)}
/>
</Form.Item>
<div className="text-sm opacity-80">
{t('Read more')}:{' '}
<a
href="https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot?lang=en-US"
target="_blank"
>
https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot?lang=en-US
</a>
</div>
</>
);
});
NotificationFeishu.displayName = 'NotificationFeishu';
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { NotificationSMTP } from './smtp';
import { NotificationTelegram } from './telegram';
import { NotificationApprise } from './apprise';
import { NotificationFeishu } from './feishu';

interface NotificationStrategy {
label: string;
Expand All @@ -25,4 +26,9 @@ export const notificationStrategies: NotificationStrategy[] = [
name: 'telegram',
form: NotificationTelegram,
},
{
label: 'Feishu',
name: 'feishu',
form: NotificationFeishu,
},
];
43 changes: 43 additions & 0 deletions src/server/model/notification/provider/feishu.ts
@@ -0,0 +1,43 @@
import { NotificationProvider } from './type';
import { baseContentTokenizer } from '../token';
import axios from 'axios';

interface FeishuPayload {
webhookUrl: string;
}

// Fork from https://github.com/louislam/uptime-kuma/blob/HEAD/server/notification-providers/smtp.js
export const feishu: NotificationProvider = {
send: async (notification, title, message) => {
const payload = notification.payload as unknown as FeishuPayload;
const webhookUrl = payload.webhookUrl;
if (
!webhookUrl.startsWith('https://open.feishu.cn/open-apis/bot/v2/hook')
) {
throw new Error('Is not a valid feishu webhook url');
}

const content = baseContentTokenizer.parse(message);

await axios.post(webhookUrl, {
msg_type: 'interactive',
card: {
elements: [
{
tag: 'div',
text: {
content,
tag: 'plain_text',
},
},
],
header: {
title: {
content: title,
tag: 'plain_text',
},
},
},
});
},
};
2 changes: 2 additions & 0 deletions src/server/model/notification/provider/index.ts
@@ -1,4 +1,5 @@
import { apprise } from './apprise';
import { feishu } from './feishu';
import { smtp } from './smtp';
import { telegram } from './telegram';
import type { NotificationProvider } from './type';
Expand All @@ -7,4 +8,5 @@ export const notificationProviders: Record<string, NotificationProvider> = {
smtp,
apprise,
telegram,
feishu,
};
4 changes: 2 additions & 2 deletions src/server/model/notification/token/tokenizer/base.ts
Expand Up @@ -18,11 +18,11 @@ export class BaseContentTokenizer {
}

parseTitle(token: TitleContentToken) {
return token.content;
return token.content + '\n';
}

parseParagraph(token: ParagraphContentToken) {
return token.content;
return token.content + '\n';
}

parseNewline(token: NewlineContentToken) {
Expand Down

0 comments on commit f6fc210

Please sign in to comment.