Files
CloudSearch/packages/backend/src/cloud/notifiers/bark.notifier.ts

45 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Notifier, NotifyParams, NotifyResult, NotifierParam } from './notifier.types';
const params: NotifierParam[] = [
{ key: 'key', label: 'Bark Key', type: 'text', required: true, placeholder: 'xxxxxxxxxxxxxxxxx' },
{ key: 'server', label: '服务器', type: 'url', default: 'https://api.day.app', required: false, placeholder: 'https://api.day.app' },
{ key: 'title', label: '标题', type: 'text', default: 'CloudSearch 通知', required: false },
{ key: 'content', label: '内容', type: 'text', required: true },
{ key: 'level', label: '级别', type: 'text', default: 'info', required: false },
];
export const barkNotifier: Notifier = {
name: 'bark',
label: 'Bark',
params,
async notify(params: NotifyParams): Promise<NotifyResult> {
try {
const key = params.key;
const server = (params.server || 'https://api.day.app').replace(/\/+$/, '');
const title = params.title || 'CloudSearch';
const content = params.content || '';
const level: string = params.level || 'info';
const icon = level === 'error' ? '⚠️' : level === 'warn' ? '🔔' : '';
const resp = await fetch(`${server}/${key}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: `${icon} ${title}`,
body: content,
group: 'CloudSearch',
level: level === 'error' ? 'timeSensitive' : 'active',
icon: '',
}),
});
if (!resp.ok) {
const text = await resp.text();
return { success: false, message: `HTTP ${resp.status}: ${text.slice(0, 100)}` };
}
return { success: true, message: 'Bark 推送成功' };
} catch (err: any) {
return { success: false, message: err.message };
}
},
};