/****************************************************************************** * 1) MONKEY-PATCH console.log TO HIDE NOISY LOGS *****************************************************************************/ const originalLog = console.log; console.log = function (...args) { // Combine all arguments into one string for easy matching const msg = args.map(a => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' '); // Hide lines containing any of these substrings if ( msg.includes('[sendMessage] Success') || msg.includes('stream-room-messages') || msg.includes('Calling (async)') || msg.includes('received message in room') || msg.includes('received] Message in room') || msg.includes('(caching)') || msg.includes('Success: "GENERAL"') || msg.includes('getRoomIdByNameOrId') || msg.includes('"bot"') || msg.includes('"_id"') || msg.includes('Success:') ) { return; // Skip printing this line } // Otherwise, print as normal originalLog(...args); }; /****************************************************************************** * 2) SETUP: REQUIRE DEPENDENCIES & DEFINE HELPER FUNCTIONS *****************************************************************************/ const readline = require('readline'); const { driver } = require('@rocket.chat/sdk'); const SERVER = 'http://ubu1.ethan.net:3000'; const ROOM = 'general'; // Helper to prompt for user input function askQuestion(rl, question) { return new Promise((resolve) => { rl.question(question, (answer) => { resolve(answer.trim()); }); }); } /****************************************************************************** * 3) MAIN LOGIC: PROMPT CREDENTIALS, CONNECT, CHAT *****************************************************************************/ async function run() { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, prompt: '> ' }); try { const username = await askQuestion(rl, 'Username: '); const password = await askQuestion(rl, 'Password: '); await driver.connect({ host: SERVER }); await driver.login({ username, password }); const userId = driver.userId; await driver.joinRooms([ROOM]); await driver.subscribeToMessages(); rl.setPrompt(`${username} > `); driver.reactToMessages((err, message) => { if (err) { console.error('Error receiving messages:', err); return; } if (message.u && message.u._id === userId) { return; } console.log(`\n[${message.u.username}]: ${message.msg}`); rl.prompt(); }); console.log(`\nConnected to "${ROOM}" on ${SERVER} as "${username}".`); console.log('Type a message (or "exit" to quit).'); rl.prompt(); rl.on('line', async (input) => { if (input.toLowerCase() === 'exit') { console.log('Exiting...'); await driver.disconnect(); rl.close(); return; } await driver.sendToRoom(input, ROOM); rl.prompt(); }); } catch (error) { console.error('Error:', error); process.exit(1); } } run();