123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #!/usr/bin/env node
- require('dotenv').config();
- const TelegramBot = require('node-telegram-bot-api');
- async function getChatId() {
- console.log('🔍 获取Telegram Chat ID...\n');
- const botToken = process.env.TELEGRAM_BOT_TOKEN;
- if (!botToken) {
- console.error('❌ 未配置TELEGRAM_BOT_TOKEN');
- process.exit(1);
- }
- try {
- console.log('📋 机器人配置信息:');
- console.log(` Bot Token: ${botToken.substring(0, 10)}...${botToken.substring(botToken.length - 10)}\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}\n`);
- console.log('📋 获取最近的更新...');
- const updates = await bot.getUpdates();
-
- if (updates.length === 0) {
- console.log('⚠️ 没有找到任何更新记录');
- console.log('\n📝 请按以下步骤操作:');
- console.log(' 1. 将机器人添加到群组或频道');
- console.log(' 2. 在群组中发送 /start 或任何消息');
- console.log(' 3. 再次运行此脚本');
- return;
- }
- console.log(`✅ 找到 ${updates.length} 条更新记录\n`);
- console.log('📊 Chat ID 列表:');
- const chatIds = new Set();
-
- updates.forEach((update, index) => {
- if (update.message) {
- const chat = update.message.chat;
- chatIds.add(chat.id);
- console.log(` ${index + 1}. Chat ID: ${chat.id}`);
- console.log(` 类型: ${chat.type}`);
- console.log(` 标题: ${chat.title || chat.first_name || '个人聊天'}`);
- console.log(` 用户名: ${chat.username ? '@' + chat.username : '无'}`);
- console.log('');
- }
- });
- if (chatIds.size > 0) {
- console.log('💡 建议使用的Chat ID:');
- chatIds.forEach((chatId, index) => {
- console.log(` ${index + 1}. ${chatId}`);
- });
-
- console.log('\n📝 配置方法:');
- console.log(' 在.env文件中设置:');
- console.log(` TELEGRAM_CHAT_ID=${Array.from(chatIds)[0]}`);
- }
- } catch (error) {
- console.error('❌ 获取Chat ID失败:', error.message);
-
- if (error.response) {
- console.error(' 错误详情:', error.response.data);
- }
-
- console.log('\n🔧 故障排除建议:');
- console.log(' 1. 检查Bot Token是否正确');
- console.log(' 2. 确保机器人已添加到群组/频道');
- console.log(' 3. 在群组中发送消息激活机器人');
-
- process.exit(1);
- }
- }
- // 如果直接运行此文件
- if (require.main === module) {
- getChatId();
- }
- module.exports = { getChatId };
|