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

Function handler

plugins/tts.js:10–48  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

8 description: 'Convert text to speech and send as an audio message.',
9 usage: '.tts <text> [language code]',
10 async handler(sock, message, args, context) {
11 const chatId = context.chatId || message.key.remoteJid;
12 if (!args.length) {
13 return sock.sendMessage(chatId, { text: '*Please provide text for TTS.*\nExample: `.tts Hello world`\nWith language: `.tts Hola mundo es`' }, { quoted: message });
14 }
15 let language = 'en';
16 if (args.length > 1 && /^[a-z]{2}$/.test(args[args.length - 1])) {
17 language = args.pop();
18 }
19 const text = args.join(' ').trim();
20 const filePath = path.join(process.cwd(), 'tmp', `tts-${Date.now()}.mp3`);
21 const tempDir = path.dirname(filePath);
22 if (!fs.existsSync(tempDir))
23 fs.mkdirSync(tempDir, { recursive: true });
24 try {
25 await new Promise((resolve, reject) => {
26 const tts = new gSpeak(text, language);
27 tts.save(filePath, (err) => {
28 if (err)
29 reject(err);
30 else
31 resolve();
32 });
33 });
34 await sock.sendMessage(chatId, {
35 audio: { url: filePath },
36 mimetype: 'audio/mpeg',
37 fileName: 'tts.mp3'
38 }, { quoted: message });
39 }
40 catch (err) {
41 console.error('TTS error:', err.message);
42 await sock.sendMessage(chatId, { text: `❌ Failed to generate TTS audio.\nReason: ${err.message}` }, { quoted: message });
43 }
44 finally {
45 if (fs.existsSync(filePath))
46 fs.unlinkSync(filePath);
47 }
48 }
49};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected