Files
certd/packages/ui/certd-server/src/plugins/plugin-notification/webhook/index.ts

101 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
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 { BaseNotification, IsNotification, NotificationBody, NotificationInput } from '@certd/pipeline';
@IsNotification({
name: 'webhook',
title: '自定义webhook',
desc: '根据模版自定义http请求',
})
export class WebhookNotification extends BaseNotification {
@NotificationInput({
title: 'webhook地址',
component: {
placeholder: '',
},
required: true,
})
webhook = '';
@NotificationInput({
title: '请求方式',
value: 'post',
component: {
name: 'a-select',
placeholder: 'post/put',
options: [
{ value: 'post', label: 'post' },
{ value: 'put', label: 'put' },
],
},
required: true,
})
method = '';
@NotificationInput({
title: 'ContentType',
value: 'json',
component: {
name: 'a-select',
options: [
{ value: 'application/json', label: 'json' },
{ value: 'application/x-www-form-urlencoded', label: 'x-www-form-urlencoded' },
],
},
required: true,
})
contentType = '';
@NotificationInput({
title: 'Headers',
component: {
name: 'a-textarea',
rows: 3,
},
helper: '一行一个格式为key:value',
required: true,
})
headers = '';
@NotificationInput({
title: '消息body模版',
value: `{
"text":"{title}",
"desp":"{content}\\n[查看详情]({url})"
}`,
component: {
name: 'a-textarea',
rows: 4,
},
col: {
span: 24,
},
helper: `根据实际的webhook接口构建一个json对象作为参数支持变量{title}、{content}、{url},变量用{}包裹,字符串需要双引号`,
required: true,
})
template = '';
async send(body: NotificationBody) {
if (!this.template) {
throw new Error('模版不能为空');
}
const bodyStr = this.template.replaceAll('{title}', body.title).replaceAll('{content}', body.content).replaceAll('{url}', body.url);
const data = JSON.parse(bodyStr);
const headers: any = {};
this.headers.split('\n').forEach(item => {
const [key, value] = item.trim().split(':');
headers[key] = value;
});
await this.http.request({
url: this.webhook,
method: this.method,
headers: {
'Content-Type': `${this.contentType}; charset=UTF-8`,
...headers,
},
data: data,
});
}
}