| 325 | |
| 326 | // 发送到 Telegram |
| 327 | function sendToTelegram(text) { |
| 328 | const data = querystring.stringify({ |
| 329 | chat_id: CHAT_ID, |
| 330 | text: text, |
| 331 | disable_web_page_preview: true |
| 332 | }); |
| 333 | |
| 334 | const options = { |
| 335 | hostname: 'api.telegram.org', |
| 336 | port: 443, |
| 337 | path: `/bot${BOT_TOKEN}/sendMessage`, |
| 338 | method: 'POST', |
| 339 | headers: { |
| 340 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 341 | 'Content-Length': Buffer.byteLength(data) |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | const req = https.request(options, (res) => { |
| 346 | let responseData = ''; |
| 347 | |
| 348 | res.on('data', (chunk) => { |
| 349 | responseData += chunk; |
| 350 | }); |
| 351 | |
| 352 | res.on('end', () => { |
| 353 | try { |
| 354 | const response = JSON.parse(responseData); |
| 355 | if (response.ok) { |
| 356 | console.log('✅ 消息已成功发送到 Telegram'); |
| 357 | } else { |
| 358 | console.error('❌ Telegram API 错误:', response.description); |
| 359 | process.exit(1); |
| 360 | } |
| 361 | } catch (error) { |
| 362 | console.error('❌ 解析响应失败:', error.message); |
| 363 | console.error('响应内容:', responseData); |
| 364 | process.exit(1); |
| 365 | } |
| 366 | }); |
| 367 | }); |
| 368 | |
| 369 | req.on('error', (error) => { |
| 370 | console.error('❌ 发送请求失败:', error.message); |
| 371 | process.exit(1); |
| 372 | }); |
| 373 | |
| 374 | req.write(data); |
| 375 | req.end(); |
| 376 | } |
| 377 | |
| 378 | // 主函数 |
| 379 | async function main() { |