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

Function handler

plugins/vnote.ts:29–74  ·  view source on GitHub ↗
(sock: any, message: any, _args: any, _context: BotContext)

Source from the content-addressed store, hash-verified

27 usage: 'Reply to an audio file with .vnote',
28
29 async handler(sock: any, message: any, _args: any, _context: BotContext) {
30 const chatId = message.key.remoteJid;
31
32 const quoted = message.message?.extendedTextMessage?.contextInfo?.quotedMessage;
33 if (!quoted?.audioMessage) {
34 return sock.sendMessage(chatId, {
35 text: '❌ Please reply to an *audio file* to convert it to a voice note.'
36 }, { quoted: message });
37 }
38
39 const tmpDir = path.join(process.cwd(), 'tmp');
40 if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir, { recursive: true });
41
42 const tmpIn = path.join(tmpDir, `vnote_in_${Date.now()}`);
43 const tmpOut = path.join(tmpDir, `vnote_out_${Date.now()}.ogg`);
44
45 try {
46 const stream = await downloadContentFromMessage(quoted.audioMessage, 'audio');
47 let buffer = Buffer.from([]);
48 for await (const chunk of stream) buffer = Buffer.concat([buffer, chunk]);
49
50 console.log('[VNOTE] original size:', buffer.length, 'mimetype:', quoted.audioMessage.mimetype);
51
52 fs.writeFileSync(tmpIn, buffer);
53
54 await execAsync(`ffmpeg -y -i "${tmpIn}" -c:a libopus -b:a 64k -ar 48000 -ac 1 "${tmpOut}"`);
55
56 const opusBuffer = fs.readFileSync(tmpOut);
57 console.log('[VNOTE] converted size:', opusBuffer.length);
58
59 await sock.sendMessage(chatId, {
60 audio: opusBuffer,
61 ptt: true,
62 mimetype: 'audio/ogg; codecs=opus'
63 }, { quoted: message });
64
65 } catch(error: any) {
66 console.error('[VNOTE] Error:', error.message);
67 await sock.sendMessage(chatId, {
68 text: '❌ Failed to convert audio to voice note.'
69 }, { quoted: message });
70 } finally {
71 try { fs.unlinkSync(tmpIn); } catch {}
72 try { fs.unlinkSync(tmpOut); } catch {}
73 }
74 }
75};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected