-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtelegram-notify.js
More file actions
311 lines (268 loc) · 9.48 KB
/
telegram-notify.js
File metadata and controls
311 lines (268 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Telegram通知脚本
* 通过Telegram Bot API发送消息通知
*/
const https = require('https');
const http = require('http');
const { URL } = require('url');
const fs = require('fs');
const path = require('path');
const { envConfig } = require('./env-config');
/**
* Telegram通知类
*/
class TelegramNotifier {
/**
* 构造函数
*/
constructor() {
// 使用统一的环境变量配置
const telegramConfig = envConfig.getTelegramConfig();
this.botToken = telegramConfig.bot_token;
this.chatId = telegramConfig.chat_id;
this.proxyUrl = telegramConfig.proxy_url;
this.enabled = this._loadConfig();
if (this.enabled) {
if (!this.botToken || !this.chatId) {
console.log('⚠️ Telegram通知已启用但配置不完整,请检查TELEGRAM_BOT_TOKEN和TELEGRAM_CHAT_ID');
this.enabled = false;
} else {
this.baseUrl = `https://api.telegram.org/bot${this.botToken}`;
if (this.proxyUrl) {
console.log(`✅ Telegram通知已启用(使用代理: ${this.proxyUrl})`);
} else {
console.log('✅ Telegram通知已启用');
}
}
} else {
console.log('ℹ️ Telegram通知未启用');
}
}
/**
* 从config.json加载配置
* @returns {boolean} 是否启用
*/
_loadConfig() {
try {
const configPath = path.join(__dirname, 'config.json');
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
return config.notification?.telegram?.enabled || false;
}
} catch (error) {
console.error('⚠️ 读取config.json失败:', error.message);
}
return false;
}
/**
* 发送消息到Telegram
* @param {string} message - 消息内容
* @param {string} parseMode - 消息解析模式 ("HTML" 或 "Markdown")
* @returns {Promise<boolean>} 发送是否成功
*/
async sendMessage(message, parseMode = 'HTML') {
if (!this.enabled) {
console.log('ℹ️ Telegram通知未启用,跳过发送');
return false;
}
const payload = {
chat_id: this.chatId,
text: message,
parse_mode: parseMode,
disable_web_page_preview: false
};
return this._sendPayload('/sendMessage', payload);
}
/**
* 发送HTTP请求到Telegram API
* @param {string} endpoint - API端点
* @param {Object} payload - 请求载荷
* @returns {Promise<boolean>} 发送是否成功
*/
_sendPayload(endpoint, payload) {
return new Promise((resolve) => {
const data = JSON.stringify(payload);
const apiUrl = new URL(this.baseUrl + endpoint);
if (this.proxyUrl) {
// 使用代理 - 通过CONNECT建立隧道
this._sendViaProxy(apiUrl, data, resolve);
} else {
// 直连
this._sendDirect(apiUrl, data, resolve);
}
});
}
/**
* 直连发送请求
*/
_sendDirect(apiUrl, data, resolve) {
const options = {
hostname: apiUrl.hostname,
path: apiUrl.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
const req = https.request(options, (res) => {
this._handleResponse(res, resolve);
});
req.on('error', (error) => {
console.error('❌ 发送Telegram请求失败:', error.message);
resolve(false);
});
req.write(data);
req.end();
}
/**
* 通过代理发送请求
*/
_sendViaProxy(apiUrl, data, resolve) {
const proxy = new URL(this.proxyUrl);
// 步骤1: 发送CONNECT请求建立隧道
const connectOptions = {
hostname: proxy.hostname,
port: proxy.port || (proxy.protocol === 'https:' ? 443 : 80),
method: 'CONNECT',
path: `${apiUrl.hostname}:443`,
headers: {}
};
// 如果代理有认证信息
if (proxy.username && proxy.password) {
const auth = Buffer.from(`${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`).toString('base64');
connectOptions.headers['Proxy-Authorization'] = `Basic ${auth}`;
}
const proxyProtocol = proxy.protocol === 'https:' ? https : http;
const connectReq = proxyProtocol.request(connectOptions);
connectReq.on('connect', (res, socket) => {
if (res.statusCode === 200) {
// 隧道建立成功,通过隧道发送HTTPS请求
const tlsOptions = {
socket: socket,
servername: apiUrl.hostname
};
const httpsReq = https.request({
...tlsOptions,
method: 'POST',
path: apiUrl.pathname,
headers: {
'Host': apiUrl.hostname,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
}, (response) => {
this._handleResponse(response, resolve);
});
httpsReq.on('error', (error) => {
console.error('❌ 通过代理发送请求失败:', error.message);
resolve(false);
});
httpsReq.write(data);
httpsReq.end();
} else {
console.error(`❌ 代理连接失败: HTTP ${res.statusCode}`);
resolve(false);
}
});
connectReq.on('error', (error) => {
console.error('❌ 连接代理服务器失败:', error.message);
resolve(false);
});
connectReq.end();
}
/**
* 处理响应
*/
_handleResponse(res, resolve) {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
try {
const result = JSON.parse(responseData);
if (result.ok) {
console.log('✅ Telegram消息发送成功');
resolve(true);
} else {
console.error('❌ Telegram消息发送失败:', result.description);
resolve(false);
}
} catch (error) {
console.error('❌ 解析Telegram响应失败:', error.message);
console.error('响应内容:', responseData.substring(0, 200));
resolve(false);
}
});
}
}
/**
* 任务完成通知函数
* @param {string} taskInfo - 任务信息
* @param {string} projectName - 项目名称
* @returns {Promise<boolean>} 发送是否成功
*/
async function notifyTaskCompletion(taskInfo = "Claude Code任务已完成", projectName = "") {
const notifier = new TelegramNotifier();
if (!notifier.enabled) {
console.log('⚠️ 请先配置Telegram通知');
console.log('📝 配置方法:');
console.log('1. 与 @BotFather 对话创建机器人,获取 token');
console.log('2. 将机器人添加到频道/群组,或直接与机器人对话');
console.log('3. 获取 chat_id');
console.log('4. 在 .env 文件中设置 TELEGRAM_BOT_TOKEN 和 TELEGRAM_CHAT_ID');
console.log('5. 在 config.json 中将 notification.telegram.enabled 设为 true');
return false;
}
// 构造通知内容
const timestamp = new Date().toLocaleString('zh-CN', {
timeZone: 'Asia/Shanghai',
hour12: false
});
// 项目名放在最前面,适配显示
const title = projectName ? `${projectName}: ${taskInfo}` : taskInfo;
const message = `🤖 <b>${title}</b>
⏰ 完成时间:${timestamp}
💡 可以查看执行结果了!`;
try {
const success = await notifier.sendMessage(message);
if (success) {
console.log('🎉 任务完成通知已发送到Telegram!');
} else {
console.log('❌ Telegram通知发送失败,请检查配置');
}
return success;
} catch (error) {
console.error('❌ 发送Telegram通知时发生错误:', error.message);
return false;
}
}
/**
* 获取命令行参数
*/
function getCommandLineArgs() {
const args = process.argv.slice(2);
const options = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg.startsWith('--')) {
const key = arg.slice(2);
const value = args[i + 1] && !args[i + 1].startsWith('--') ? args[i + 1] : true;
options[key] = value;
if (value !== true) i++; // 跳过下一个参数
}
}
return options;
}
// 如果直接运行此脚本
if (require.main === module) {
const options = getCommandLineArgs();
const taskInfo = options.message || options.task || "Claude Code任务已完成";
console.log('🚀 开始发送Telegram通知...');
notifyTaskCompletion(taskInfo);
}
module.exports = {
TelegramNotifier,
notifyTaskCompletion
};