123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #!/usr/bin/env node
- require('dotenv').config();
- const TelegramBot = require('node-telegram-bot-api');
- const logger = require('./src/utils/logger');
- async function testTelegram() {
- console.log('🤖 测试Telegram机器人...\n');
- const botToken = process.env.TELEGRAM_BOT_TOKEN;
- const chatId = process.env.TELEGRAM_CHAT_ID;
- if (!botToken) {
- console.error('❌ 未配置TELEGRAM_BOT_TOKEN');
- process.exit(1);
- }
- if (!chatId) {
- console.error('❌ 未配置TELEGRAM_CHAT_ID');
- console.log('\n📋 如何获取Chat ID:');
- console.log(' 1. 将机器人添加到群组或频道');
- console.log(' 2. 在群组中发送 /start');
- console.log(' 3. 访问: https://api.telegram.org/bot' + botToken + '/getUpdates');
- console.log(' 4. 查找 "chat" 对象中的 "id" 字段');
- process.exit(1);
- }
- try {
- console.log('📋 机器人配置信息:');
- console.log(` Bot Token: ${botToken.substring(0, 10)}...${botToken.substring(botToken.length - 10)}`);
- console.log(` Chat ID: ${chatId}\n`);
- // 创建机器人实例
- const bot = new TelegramBot(botToken, { polling: false });
- console.log('🔄 测试机器人连接...');
- const me = await bot.getMe();
- console.log('✅ 机器人连接成功!');
- console.log(` 机器人名称: ${me.first_name}`);
- console.log(` 机器人用户名: @${me.username}`);
- console.log(` 机器人ID: ${me.id}\n`);
- console.log('🔄 测试发送消息...');
- const testMessage = `🧪 测试消息
- 时间: ${new Date().toLocaleString('zh-CN')}
- 状态: 机器人连接正常
- 功能: Clash节点测速工具
- 如果您收到此消息,说明Telegram通知功能配置成功!`;
- const sentMessage = await bot.sendMessage(chatId, testMessage, {
- parse_mode: 'Markdown',
- disable_web_page_preview: true
- });
- console.log('✅ 消息发送成功!');
- console.log(` 消息ID: ${sentMessage.message_id}`);
- console.log(` 发送时间: ${new Date(sentMessage.date * 1000).toLocaleString('zh-CN')}\n`);
- console.log('🔄 测试格式化消息...');
- const formattedMessage = `🚨 *节点故障通知*
- *节点名称:* 测试节点
- *节点类型:* ss
- *服务器:* 1.2.3.4:8388
- *分组:* 测试分组
- *连续失败次数:* 3
- *最后测试时间:* ${new Date().toLocaleString('zh-CN')}
- *错误信息:* 连接超时
- *测试耗时:* 5000ms`;
- const formattedSentMessage = await bot.sendMessage(chatId, formattedMessage, {
- parse_mode: 'Markdown',
- disable_web_page_preview: true
- });
- console.log('✅ 格式化消息发送成功!');
- console.log(` 消息ID: ${formattedSentMessage.message_id}\n`);
- console.log('🎉 Telegram机器人测试完成!');
- console.log('\n📝 测试结果:');
- console.log(' ✅ 机器人连接正常');
- console.log(' ✅ 消息发送功能正常');
- console.log(' ✅ 格式化消息功能正常');
- console.log(' ✅ 通知功能已就绪');
- } catch (error) {
- console.error('❌ Telegram测试失败:', error.message);
-
- if (error.response) {
- console.error(' 错误详情:', error.response.data);
- }
-
- console.log('\n🔧 故障排除建议:');
- console.log(' 1. 检查Bot Token是否正确');
- console.log(' 2. 检查Chat ID是否正确');
- console.log(' 3. 确保机器人已添加到目标群组/频道');
- console.log(' 4. 确保机器人有发送消息权限');
-
- process.exit(1);
- }
- }
- // 如果直接运行此文件
- if (require.main === module) {
- testTelegram();
- }
- module.exports = { testTelegram };
|