MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MD / handler

Function handler

plugins/wordcloud.js:38–113  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

36 description: 'Analyze text and show top 20 most used words with stats',
37 usage: '.wordcloud <text or reply to any message/file>',
38 async handler(sock, message, args, context) {
39 const { chatId, channelInfo } = context;
40 const scriptPath = path.join(process.cwd(), 'lib', 'wordcloud.py');
41 if (!fs.existsSync(scriptPath)) {
42 return await sock.sendMessage(chatId, {
43 text: `❌ wordcloud.py not found in lib/. Please check installation.`,
44 ...channelInfo
45 }, { quoted: message });
46 }
47 const quoted = getQuoted(message);
48 const quotedText = quoted?.conversation || quoted?.extendedTextMessage?.text || '';
49 const hasDoc = !!quoted?.documentMessage;
50 const textInput = args.join(' ').trim() || quotedText;
51 if (!textInput && !hasDoc) {
52 return await sock.sendMessage(chatId, {
53 text: `📝 *Word Cloud Analyzer*\n\n` +
54 `*Usage:*\n` +
55 `\`.wordcloud <paste any text here>\`\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 count, unique words, sentences\n` +
61 `🏆 Top 20 most used words\n` +
62 `⏱️ Reading time estimate\n` +
63 `📖 Lexical diversity score`,
64 ...channelInfo
65 }, { quoted: message });
66 }
67 await sock.sendMessage(chatId, { text: '🔍 Analyzing text...', ...channelInfo }, { quoted: message });
68 const tempDir = path.join(process.cwd(), 'temp');
69 fs.mkdirSync(tempDir, { recursive: true });
70 const id = Date.now();
71 try {
72 let cmd;
73 if (hasDoc && quoted) {
74 // Download document
75 const msgObj = { message: { documentMessage: quoted.documentMessage } };
76 const buf = await downloadMediaMessage(msgObj, 'buffer', {});
77 const tmpFile = path.join(tempDir, `wc_in_${id}.txt`);
78 fs.writeFileSync(tmpFile, buf);
79 cmd = `python3 "${scriptPath}" --file "${tmpFile}"`;
80 const { stdout } = await execAsync(cmd, { timeout: 30000 });
81 try {
82 fs.unlinkSync(tmpFile);
83 }
84 catch { }
85 const data = JSON.parse(stdout.trim());
86 if (data.error) {
87 return await sock.sendMessage(chatId, { text: `❌ ${data.error}`, ...channelInfo }, { quoted: message });
88 }
89 await sendResult(sock, chatId, channelInfo, message, formatResult(data), `wordcloud_${id}.txt`);
90 }
91 else {
92 const tmpFile = path.join(tempDir, `wc_in_${id}.txt`);
93 fs.writeFileSync(tmpFile, textInput);
94 cmd = `python3 "${scriptPath}" --file "${tmpFile}"`;
95 const { stdout } = await execAsync(cmd, { timeout: 30000 });

Callers

nothing calls this directly

Calls 4

formatResultFunction · 0.85
getQuotedFunction · 0.70
downloadMediaMessageFunction · 0.70
sendResultFunction · 0.70

Tested by

no test coverage detected