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

92 lines
2.3 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { BaseNotification, IsNotification, NotificationBody, NotificationInput } from "@certd/pipeline";
@IsNotification({
2026-05-31 01:41:33 +08:00
name: "telegram",
title: "Telegram通知",
desc: "Telegram Bot推送通知",
2024-11-30 01:57:09 +08:00
needPlus: true,
})
export class TelegramNotification extends BaseNotification {
2024-12-02 14:06:55 +08:00
@NotificationInput({
2026-05-31 01:41:33 +08:00
title: "URL",
value: "https://api.telegram.org",
2024-12-02 14:06:55 +08:00
component: {
2026-05-31 01:41:33 +08:00
placeholder: "https://api.telegram.org",
2024-12-02 14:06:55 +08:00
},
required: true,
})
2026-05-31 01:41:33 +08:00
endpoint = "https://api.telegram.org";
2024-12-02 14:06:55 +08:00
@NotificationInput({
2026-05-31 01:41:33 +08:00
title: "Bot Token",
component: {
2026-05-31 01:41:33 +08:00
placeholder: "123456789:ABCdefGhijklmnopqrstUVWXyz",
},
2026-05-31 01:41:33 +08:00
helper: "[token获取](https://core.telegram.org/bots/features#botfather)",
required: true,
})
2026-05-31 01:41:33 +08:00
botToken = "";
@NotificationInput({
2026-05-31 01:41:33 +08:00
title: "聊天ID",
component: {
2026-05-31 01:41:33 +08:00
placeholder: "聊天ID,例如 123456789 或 @channelusername",
},
2026-05-31 01:41:33 +08:00
helper: "用户ID(纯数字)或频道名称(@xxxx)",
required: true,
})
2026-05-31 01:41:33 +08:00
chatId = "";
2024-12-02 14:06:55 +08:00
@NotificationInput({
2026-05-31 01:41:33 +08:00
title: "代理",
2024-12-02 14:06:55 +08:00
component: {
2026-05-31 01:41:33 +08:00
placeholder: "http://xxxxx:xx",
2024-12-02 14:06:55 +08:00
},
2026-05-31 01:41:33 +08:00
helper: "使用https_proxy",
2024-12-02 14:06:55 +08:00
required: false,
})
2026-05-31 01:41:33 +08:00
httpsProxy = "";
2024-12-02 14:06:55 +08:00
@NotificationInput({
2026-05-31 01:41:33 +08:00
title: "忽略证书校验",
2024-12-02 14:06:55 +08:00
value: false,
component: {
2026-05-31 01:41:33 +08:00
name: "a-switch",
vModel: "checked",
2024-12-02 14:06:55 +08:00
},
required: false,
})
skipSslVerify: boolean;
replaceText(text: string) {
2025-06-13 00:25:08 +08:00
// .*()<> 等都需要用\\进行替换
2026-07-02 22:54:24 +08:00
return text.replace(/[_*[\]()~`>#+\-=|{}.!\\]/g, "\\$&");
2025-12-15 22:21:43 +08:00
// .replace(/([\\_*`|!.[\](){}>+#=~-])/gm, '\\$1')
// return text.replace(/[\\.*()<>]/g, '\\$&');
}
async send(body: NotificationBody) {
if (!this.botToken || !this.chatId) {
2026-05-31 01:41:33 +08:00
throw new Error("Bot Token 和聊天ID不能为空");
}
// 构建消息内容
const messageContent = `${this.replaceText(body.title)}\n\n${this.replaceText(body.content)}\n\n[查看详情](${body.url})`;
// Telegram API URL
const url = `https://api.telegram.org/bot${this.botToken}/sendMessage`;
// 发送 HTTP 请求
await this.http.request({
url: url,
2026-05-31 01:41:33 +08:00
method: "POST",
data: {
chat_id: this.chatId,
text: messageContent,
2026-05-31 01:41:33 +08:00
parse_mode: "MarkdownV2", // 或使用 'HTML' 取决于需要的格式
},
2024-12-02 14:06:55 +08:00
httpProxy: this.httpsProxy,
skipSslVerify: this.skipSslVerify,
});
}
}