35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { Notifier, NotifyParams, NotifyResult, NotifierParam } from './notifier.types';
|
|
|
|
const params: NotifierParam[] = [
|
|
{ key: 'webhook_url', label: 'Webhook URL', type: 'url', required: true, placeholder: 'https://oapi.dingtalk.com/robot/send?access_token=xxx' },
|
|
|
|
];
|
|
|
|
export const dingtalkNotifier: Notifier = {
|
|
name: 'dingtalk',
|
|
label: '钉钉机器人',
|
|
params,
|
|
async notify(params) {
|
|
try {
|
|
const level = params.level || 'info';
|
|
const text = `## ${params.title || 'CloudSearch'}\n${params.content || ''}`;
|
|
const resp = await fetch(params.webhook_url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
msgtype: 'markdown',
|
|
markdown: { title: params.title || 'CloudSearch', text },
|
|
at: { isAtAll: false },
|
|
}),
|
|
});
|
|
const data: any = await resp.json();
|
|
if (data.errcode === 0)
|
|
return { success: true, message: '钉钉推送成功' };
|
|
return { success: false, message: data.errmsg || `HTTP ${resp.status}` };
|
|
}
|
|
catch (err: any) {
|
|
return { success: false, message: err.message };
|
|
}
|
|
},
|
|
};
|