2024-11-26 15:13:57 +08:00
|
|
|
import { BaseNotification, IsNotification, NotificationBody, NotificationInput } from '@certd/pipeline';
|
|
|
|
|
|
|
|
|
|
@IsNotification({
|
|
|
|
|
name: 'telegram',
|
|
|
|
|
title: 'Telegram通知',
|
|
|
|
|
desc: 'Telegram Bot推送通知',
|
2024-11-30 01:57:09 +08:00
|
|
|
needPlus: true,
|
2024-11-26 15:13:57 +08:00
|
|
|
})
|
|
|
|
|
export class TelegramNotification extends BaseNotification {
|
|
|
|
|
@NotificationInput({
|
|
|
|
|
title: 'Bot Token',
|
|
|
|
|
component: {
|
|
|
|
|
placeholder: '123456789:ABCdefGhijklmnopqrstUVWXyz',
|
|
|
|
|
},
|
|
|
|
|
helper: '[token获取](https://core.telegram.org/bots/features#botfather)',
|
|
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
botToken = '';
|
|
|
|
|
|
|
|
|
|
@NotificationInput({
|
|
|
|
|
title: '聊天ID',
|
|
|
|
|
component: {
|
2024-11-26 18:36:28 +08:00
|
|
|
placeholder: '聊天ID,例如 123456789 或 @channelusername',
|
2024-11-26 15:13:57 +08:00
|
|
|
},
|
2024-11-26 18:36:28 +08:00
|
|
|
helper: '用户ID(纯数字)或频道名称(@xxxx)',
|
2024-11-26 15:13:57 +08:00
|
|
|
required: true,
|
|
|
|
|
})
|
|
|
|
|
chatId = '';
|
|
|
|
|
|
|
|
|
|
async send(body: NotificationBody) {
|
|
|
|
|
if (!this.botToken || !this.chatId) {
|
|
|
|
|
throw new Error('Bot Token 和聊天ID不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建消息内容
|
|
|
|
|
const messageContent = `*${body.title}*\n\n${body.content}\n[查看详情](${body.url})`;
|
|
|
|
|
|
|
|
|
|
// Telegram API URL
|
|
|
|
|
const url = `https://api.telegram.org/bot${this.botToken}/sendMessage`;
|
|
|
|
|
|
|
|
|
|
// 发送 HTTP 请求
|
|
|
|
|
await this.http.request({
|
|
|
|
|
url: url,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
chat_id: this.chatId,
|
|
|
|
|
text: messageContent,
|
|
|
|
|
parse_mode: 'MarkdownV2', // 或使用 'HTML' 取决于需要的格式
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|