#!/usr/bin/env node /** * Tongge Rest Mode - 休息时间自动回复 * * 23:00-07:00 自动回复,模拟人类休息 * 紧急关键词可以穿透(如"紧急"、"急事") */ const https = require('https'); const BOT_TOKEN = '8719964249:AAGy4GEqZ1mMOhTKYt5iPD1FcYtpuIDUdCk'; const REST_START = 23; // 23:00 const REST_END = 7; // 07:00 // 休息时间回复语料 const REST_REPLIES = [ "桐哥在睡觉呢~ 明天再聊吧 😴", "夜深了,桐哥去休息啦,有话明天说~", "桐哥已经睡了,留言明天会回复的 🌙", "现在是桐哥的休息时间,明天找你聊哦~", "桐哥充电中🔋,明天满血复活再聊!", ]; // 可以穿透休息模式的关键词 const URGENCY_KEYWORDS = ['紧急', '急事', '救命', 'help', 'emergency']; // 获取香港时区的小时 (UTC+8) function getHKHour() { const now = new Date(); const utcHour = now.getUTCHours(); const hkHour = (utcHour + 8) % 24; return hkHour; } // 检查是否在休息时间(香港时区) function isRestTime() { const hour = getHKHour(); return hour >= REST_START || hour < REST_END; } // 检查消息是否紧急 function isUrgent(message) { return URGENCY_KEYWORDS.some(keyword => message.toLowerCase().includes(keyword.toLowerCase()) ); } // 获取随机休息回复 function getRestReply() { return REST_REPLIES[Math.floor(Math.random() * REST_REPLIES.length)]; } // 发送 Telegram 消息 function sendMessage(chatId, text) { return new Promise((resolve, reject) => { const data = JSON.stringify({ chat_id: chatId, text: text, }); const options = { hostname: 'api.telegram.org', port: 443, path: `/bot${BOT_TOKEN}/sendMessage`, method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length, }, }; const req = https.request(options, (res) => { let body = ''; res.on('data', (chunk) => body += chunk); res.on('end', () => { resolve(JSON.parse(body)); }); }); req.on('error', reject); req.write(data); req.end(); }); } // Webhook 处理器(如果部署为 webhook) async function handleWebhook(req, res) { let body = ''; req.on('data', chunk => body += chunk); req.on('end', async () => { try { const update = JSON.parse(body); const message = update.message; if (!message) { res.writeHead(200); res.end('OK'); return; } const chatId = message.chat.id; const text = message.text || ''; // 检查是否在休息时间 if (isRestTime()) { // 检查是否紧急 if (isUrgent(text)) { console.log(`[URGENT] Message from ${chatId}: ${text}`); // 紧急消息,正常转发给桐哥(不拦截) res.writeHead(200); res.end('OK'); return; } // 发送休息回复 const reply = getRestReply(); await sendMessage(chatId, reply); console.log(`[REST] Replied to ${chatId}: ${reply}`); } res.writeHead(200); res.end('OK'); } catch (error) { console.error('[ERROR]', error); res.writeHead(500); res.end('Error'); } }); } // 命令行模式:检查配置 if (require.main === module) { console.log('Tongge Rest Mode Configuration:'); console.log(` Rest Start: ${REST_START}:00`); console.log(` Rest End: ${REST_END}:00`); console.log(` Current Time: ${new Date().toTimeString().slice(0, 5)}`); console.log(` Is Rest Time: ${isRestTime() ? 'YES' : 'NO'}`); console.log(` Urgency Keywords: ${URGENCY_KEYWORDS.join(', ')}`); if (isRestTime()) { console.log(`\nSample Reply: ${getRestReply()}`); } } module.exports = { isRestTime, isUrgent, getRestReply, sendMessage, handleWebhook };