(sock, message, args, context)
| 37 | description: 'Deep text analysis: reading level, sentiment, word stats (C++ powered)', |
| 38 | usage: '.analyze <text or reply to any message/file>', |
| 39 | async handler(sock, message, args, context) { |
| 40 | const { chatId, channelInfo } = context; |
| 41 | const binPath = getBin('analyze'); |
| 42 | if (!fs.existsSync(binPath)) { |
| 43 | return await sock.sendMessage(chatId, { |
| 44 | text: `❌ Analyze binary not available on this server (g++ not installed or not yet compiled).`, |
| 45 | ...channelInfo |
| 46 | }, { quoted: message }); |
| 47 | } |
| 48 | const quoted = getQuoted(message); |
| 49 | const quotedText = quoted?.conversation || quoted?.extendedTextMessage?.text || ''; |
| 50 | const hasDoc = !!quoted?.documentMessage; |
| 51 | const textInput = args.join(' ').trim() || quotedText; |
| 52 | if (!textInput && !hasDoc) { |
| 53 | return await sock.sendMessage(chatId, { |
| 54 | text: `📈 *Text Analyzer*\n\n` + |
| 55 | `*Usage:* \`.analyze <paste any text>\`\n\n` + |
| 56 | `*Or reply to:*\n` + |
| 57 | `• Any text message\n` + |
| 58 | `• A .txt or document file\n\n` + |
| 59 | `*Output includes:*\n` + |
| 60 | `📊 Word/sentence/paragraph count\n` + |
| 61 | `📖 Flesch Reading Ease score & level\n` + |
| 62 | `😊 Sentiment analysis (positive/negative/neutral)\n` + |
| 63 | `⏱️ Reading time estimate\n` + |
| 64 | `🏆 Top 20 keywords`, |
| 65 | ...channelInfo |
| 66 | }, { quoted: message }); |
| 67 | } |
| 68 | await sock.sendMessage(chatId, { text: '🔍 Analyzing...', ...channelInfo }, { quoted: message }); |
| 69 | const tempDir = path.join(process.cwd(), 'temp'); |
| 70 | fs.mkdirSync(tempDir, { recursive: true }); |
| 71 | const id = Date.now(); |
| 72 | try { |
| 73 | let stdout; |
| 74 | if (hasDoc && quoted) { |
| 75 | const msgObj = { message: { documentMessage: quoted.documentMessage } }; |
| 76 | const buf = await downloadMediaMessage(msgObj, 'buffer', {}); |
| 77 | const tmpFile = path.join(tempDir, `analyze_in_${id}.txt`); |
| 78 | fs.writeFileSync(tmpFile, buf); |
| 79 | const result = await execAsync(`"${binPath}" --file "${tmpFile}"`, { timeout: 30000 }); |
| 80 | stdout = result.stdout; |
| 81 | try { |
| 82 | fs.unlinkSync(tmpFile); |
| 83 | } |
| 84 | catch { } |
| 85 | } |
| 86 | else { |
| 87 | const tmpFile = path.join(tempDir, `analyze_in_${id}.txt`); |
| 88 | fs.writeFileSync(tmpFile, textInput); |
| 89 | const result = await execAsync(`"${binPath}" --file "${tmpFile}"`, { timeout: 30000 }); |
| 90 | stdout = result.stdout; |
| 91 | try { |
| 92 | fs.unlinkSync(tmpFile); |
| 93 | } |
| 94 | catch { } |
| 95 | } |
| 96 | const data = JSON.parse(stdout.trim()); |
nothing calls this directly
no test coverage detected