修复自定义通知

This commit is contained in:
whyour
2022-09-20 17:34:36 +08:00
parent 5168b044a3
commit 1ebcfd8001
5 changed files with 125 additions and 27 deletions
+1
View File
@@ -95,6 +95,7 @@ export class WebhookNotification extends NotificationBaseInfo {
public webhookBody: any = {};
public webhookUrl: string = '';
public webhookMethod: 'GET' | 'POST' | 'PUT' = 'GET';
public webhookContentType: 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded' = 'application/json';
}
export interface NotificationInfo
+23 -9
View File
@@ -34,7 +34,7 @@ export default class NotificationService {
private content = '';
private params!: Omit<NotificationInfo, 'type'>;
constructor(@Inject('logger') private logger: winston.Logger) {}
constructor(@Inject('logger') private logger: winston.Logger) { }
public async notify(
title: string,
@@ -181,9 +181,8 @@ export default class NotificationService {
telegramBotUserId,
} = this.params;
const authStr = telegramBotProxyAuth ? `${telegramBotProxyAuth}@` : '';
const url = `https://${
telegramBotApiHost ? telegramBotApiHost : 'api.telegram.org'
}/bot${telegramBotToken}/sendMessage`;
const url = `https://${telegramBotApiHost ? telegramBotApiHost : 'api.telegram.org'
}/bot${telegramBotToken}/sendMessage`;
let agent;
if (telegramBotProxyHost && telegramBotProxyPort) {
const options: any = {
@@ -386,17 +385,32 @@ export default class NotificationService {
}
private async webhook() {
const { webhookUrl, webhookBody, webhookHeaders, webhookMethod } =
const { webhookUrl, webhookBody, webhookHeaders, webhookMethod, webhookContentType } =
this.params;
const { statusCode } = await got(webhookUrl, {
const bodyParam = this.formatBody(webhookContentType, webhookBody);
const options = {
method: webhookMethod,
headers: webhookHeaders,
body: webhookBody,
timeout: this.timeout,
retry: 0,
});
allowGetBody: true,
...bodyParam
}
const res = await got(webhookUrl, options);
return String(res.statusCode).startsWith('20');
}
return String(statusCode).includes('20');
private formatBody(contentType: string, body: any): object {
if (!body) return {};
switch (contentType) {
case 'application/json':
return { json: body };
case 'multipart/form-data':
return { form: body };
case 'application/x-www-form-urlencoded':
return { body };
}
return {};
}
}