test-telegram.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env node
  2. require('dotenv').config();
  3. const TelegramBot = require('node-telegram-bot-api');
  4. const logger = require('./src/utils/logger');
  5. async function testTelegram() {
  6. console.log('🤖 测试Telegram机器人...\n');
  7. const botToken = process.env.TELEGRAM_BOT_TOKEN;
  8. const chatId = process.env.TELEGRAM_CHAT_ID;
  9. if (!botToken) {
  10. console.error('❌ 未配置TELEGRAM_BOT_TOKEN');
  11. process.exit(1);
  12. }
  13. if (!chatId) {
  14. console.error('❌ 未配置TELEGRAM_CHAT_ID');
  15. console.log('\n📋 如何获取Chat ID:');
  16. console.log(' 1. 将机器人添加到群组或频道');
  17. console.log(' 2. 在群组中发送 /start');
  18. console.log(' 3. 访问: https://api.telegram.org/bot' + botToken + '/getUpdates');
  19. console.log(' 4. 查找 "chat" 对象中的 "id" 字段');
  20. process.exit(1);
  21. }
  22. try {
  23. console.log('📋 机器人配置信息:');
  24. console.log(` Bot Token: ${botToken.substring(0, 10)}...${botToken.substring(botToken.length - 10)}`);
  25. console.log(` Chat ID: ${chatId}\n`);
  26. // 创建机器人实例
  27. const bot = new TelegramBot(botToken, { polling: false });
  28. console.log('🔄 测试机器人连接...');
  29. const me = await bot.getMe();
  30. console.log('✅ 机器人连接成功!');
  31. console.log(` 机器人名称: ${me.first_name}`);
  32. console.log(` 机器人用户名: @${me.username}`);
  33. console.log(` 机器人ID: ${me.id}\n`);
  34. console.log('🔄 测试发送消息...');
  35. const testMessage = `🧪 测试消息
  36. 时间: ${new Date().toLocaleString('zh-CN')}
  37. 状态: 机器人连接正常
  38. 功能: Clash节点测速工具
  39. 如果您收到此消息,说明Telegram通知功能配置成功!`;
  40. const sentMessage = await bot.sendMessage(chatId, testMessage, {
  41. parse_mode: 'Markdown',
  42. disable_web_page_preview: true
  43. });
  44. console.log('✅ 消息发送成功!');
  45. console.log(` 消息ID: ${sentMessage.message_id}`);
  46. console.log(` 发送时间: ${new Date(sentMessage.date * 1000).toLocaleString('zh-CN')}\n`);
  47. console.log('🔄 测试格式化消息...');
  48. const formattedMessage = `🚨 *节点故障通知*
  49. *节点名称:* 测试节点
  50. *节点类型:* ss
  51. *服务器:* 1.2.3.4:8388
  52. *分组:* 测试分组
  53. *连续失败次数:* 3
  54. *最后测试时间:* ${new Date().toLocaleString('zh-CN')}
  55. *错误信息:* 连接超时
  56. *测试耗时:* 5000ms`;
  57. const formattedSentMessage = await bot.sendMessage(chatId, formattedMessage, {
  58. parse_mode: 'Markdown',
  59. disable_web_page_preview: true
  60. });
  61. console.log('✅ 格式化消息发送成功!');
  62. console.log(` 消息ID: ${formattedSentMessage.message_id}\n`);
  63. console.log('🎉 Telegram机器人测试完成!');
  64. console.log('\n📝 测试结果:');
  65. console.log(' ✅ 机器人连接正常');
  66. console.log(' ✅ 消息发送功能正常');
  67. console.log(' ✅ 格式化消息功能正常');
  68. console.log(' ✅ 通知功能已就绪');
  69. } catch (error) {
  70. console.error('❌ Telegram测试失败:', error.message);
  71. if (error.response) {
  72. console.error(' 错误详情:', error.response.data);
  73. }
  74. console.log('\n🔧 故障排除建议:');
  75. console.log(' 1. 检查Bot Token是否正确');
  76. console.log(' 2. 检查Chat ID是否正确');
  77. console.log(' 3. 确保机器人已添加到目标群组/频道');
  78. console.log(' 4. 确保机器人有发送消息权限');
  79. process.exit(1);
  80. }
  81. }
  82. // 如果直接运行此文件
  83. if (require.main === module) {
  84. testTelegram();
  85. }
  86. module.exports = { testTelegram };