81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
import TelegramBot from 'node-telegram-bot-api';
|
|
import dotenv from 'dotenv';
|
|
|
|
// Load environment variables
|
|
dotenv.config();
|
|
|
|
// Temporary script to get group chat ID and topic IDs
|
|
// Run this once to get the IDs you need for monitoring setup
|
|
|
|
const token = process.env.TELEGRAM_BOT_TOKEN;
|
|
if (!token) {
|
|
console.error('❌ TELEGRAM_BOT_TOKEN not found in environment variables');
|
|
process.exit(1);
|
|
}
|
|
|
|
const bot = new TelegramBot(token, { polling: true });
|
|
|
|
console.log('🤖 Bot started! Add me to your supergroup and I\'ll help you get the IDs...\n');
|
|
|
|
// Listen for messages to get chat ID
|
|
bot.on('message', (msg) => {
|
|
const chatId = msg.chat.id;
|
|
const chatType = msg.chat.type;
|
|
const chatTitle = msg.chat.title;
|
|
|
|
if (chatType === 'supergroup') {
|
|
console.log('📊 SUPERGROUP DETECTED:');
|
|
console.log(` Chat ID: ${chatId}`);
|
|
console.log(` Title: ${chatTitle}`);
|
|
console.log(` Type: ${chatType}\n`);
|
|
|
|
// Check if message is in a topic
|
|
if (msg.message_thread_id) {
|
|
console.log('📝 TOPIC MESSAGE DETECTED:');
|
|
console.log(` Topic ID: ${msg.message_thread_id}`);
|
|
console.log(` Message: ${msg.text || '[Non-text message]'}`);
|
|
console.log(` From: ${msg.from?.first_name || 'Unknown'}\n`);
|
|
}
|
|
|
|
// Send a test message to confirm bot can send messages
|
|
bot.sendMessage(chatId,
|
|
`✅ Bot connected successfully!\n\n` +
|
|
`📊 Group Info:\n` +
|
|
`• Chat ID: \`${chatId}\`\n` +
|
|
`• Title: ${chatTitle}\n` +
|
|
`• Type: ${chatType}\n\n` +
|
|
`📝 To get topic IDs:\n` +
|
|
`1. Create topics in this group\n` +
|
|
`2. Send a message in each topic\n` +
|
|
`3. Check the console output for topic IDs`,
|
|
{ parse_mode: 'Markdown' }
|
|
).catch(err => {
|
|
console.error('❌ Failed to send message to group:', err.message);
|
|
console.log('💡 Make sure the bot has permission to send messages in the group');
|
|
});
|
|
} else if (chatType === 'private') {
|
|
console.log(`💬 Private message from ${msg.from?.first_name}: ${msg.text}`);
|
|
} else {
|
|
console.log(`📱 Message from ${chatType}: ${chatTitle || 'Unknown'} (ID: ${chatId})`);
|
|
}
|
|
});
|
|
|
|
// Listen for errors
|
|
bot.on('error', (error) => {
|
|
console.error('❌ Bot error:', error.message);
|
|
});
|
|
|
|
// Graceful shutdown
|
|
process.on('SIGINT', () => {
|
|
console.log('\n👋 Shutting down bot...');
|
|
bot.stopPolling();
|
|
process.exit(0);
|
|
});
|
|
|
|
console.log('📋 Instructions:');
|
|
console.log('1. Add this bot to your supergroup as admin');
|
|
console.log('2. Enable topics in the group settings');
|
|
console.log('3. Create the monitoring topics');
|
|
console.log('4. Send a message in each topic');
|
|
console.log('5. Copy the Chat ID and Topic IDs from the output');
|
|
console.log('6. Press Ctrl+C to stop this script\n'); |