Skip to content

Commit

Permalink
Create convertidor-tts3.js
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoSobrino authored May 1, 2024
1 parent 0dd9199 commit 006e30c
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions plugins/convertidor-tts3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fetch from 'node-fetch';

const handler = async (m, { conn, usedPrefix, command, text }) => {
const match = text.match(/^(\w+)\s*\|\s*(.+)/i);
if (!match) {
const voices = await getVoices();
const voiceNames = voices.voices.map(voice => voice.name).join('\nβ—‰ ');
return m.reply(`*[❗] Formato de uso erroneo, voz o texto faltante.*\n\n*β€”β—‰ Ejemplo:*\nβ—‰ ${usedPrefix + command} nombre_voz | texto\n\n*β€”β—‰ Ejemplo de uso:*\nβ—‰ ${usedPrefix + command} ${voices.voices[0].name} | este es un texto de ejemplo\n\n*β€”β—‰ Lista de voces disponibles:*\nβ—‰ ${voiceNames}`
);
}
const [, voiceName, inputText] = match;
const voices = await getVoices();
const voice = voices.voices.find(voice => voice.name.toLowerCase() === voiceName.toLowerCase());
if (!voice) {
const voiceNames = voices.voices.map(voice => voice.name).join('\nβ—‰ ');
return m.reply(
`[❗] No se encontrΓ³ ninguna voz con el nombre "${voiceName}".\n\nβ€”β—‰ Lista de voces disponibles:\nβ—‰ ${voiceNames}`
);
}
const audio = await convertTextToSpeech(inputText, voice.voice_id);
if (audio) {
conn.sendMessage(m.chat, { audio: audio.audio, fileName: `error.mp3`, mimetype: 'audio/mpeg', ptt: true }, { quoted: m });
}
};

handler.command = /^(tts3)$/i;
export default handler;

const apiKey = 'a0e2c6022f1aeb28b5020b1dd0faf6ee';
const getVoices = async () => {
const url = 'https://api.elevenlabs.io/v1/voices';
const options = { method: 'GET', headers: { 'Content-Type': 'application/json', 'xi-api-key': apiKey }};
try {
const response = await fetch(url, options);
const voices = await response.json();
return voices;
} catch (error) {
console.error('Error al obtener las voces:', error);
return [];
}
};

const convertTextToSpeech = async (text, voiceId) => {
const url = `https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`;
const options = { method: 'POST', headers: { 'Content-Type': 'application/json', 'xi-api-key': apiKey }, body: JSON.stringify({ text: text, model_id: 'eleven_monolingual_v1', voice_settings: { stability: 0.5, similarity_boost: 0.5 }})};
try {
const response = await fetch(url, options);
const audioBuffer = await response.buffer();
return { audio: audioBuffer };
} catch (error) {
console.error('Error al generar el audio:', error);
return [];
}
};

0 comments on commit 006e30c

Please sign in to comment.