require('dotenv').config();

// 添加 .agent/node_modules 到搜索路径
const path = require('path');
const agentNodeModules = path.join(process.cwd(), '.agent', 'node_modules');
if (!module.paths.includes(agentNodeModules)) {
  module.paths.unshift(agentNodeModules);
}

const TelegramBot = require('node-telegram-bot-api');

const token = process.env.TELEGRAM_BOT_TOKEN;

console.log('Token:', token ? token.substring(0, 10) + '...' : 'NOT SET');

if (!token) {
  console.log('ERROR: No token!');
  process.exit(1);
}

const bot = new TelegramBot(token, { polling: true });

console.log('Bot created, starting polling...');

bot.on('polling_error', (err) => {
  console.log('Polling error:', err.message);
});

bot.on('error', (err) => {
  console.log('Error:', err.message);
});

bot.on('message', (msg) => {
  console.log('\n=== MESSAGE RECEIVED ===');
  console.log('Chat ID:', msg.chat.id);
  console.log('From:', msg.from.username);
  console.log('Text:', msg.text);
  console.log('========================\n');

  // 回复测试
  bot
    .sendMessage(msg.chat.id, 'Bot is working! Received: ' + msg.text)
    .then(() => console.log('Reply sent'))
    .catch((err) => console.log('Reply error:', err.message));
});

console.log('Waiting for messages...');
