Last active
December 6, 2023 13:16
-
-
Save olaronymax/32cc07b169a28e2d34797b3df4a100d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function processarMensagemDeAudio(messageData) { | |
try { | |
console.log(messageData); | |
const messageId = messageData.data.key.id; | |
const owner = messageData.data.owner; | |
console.log("messageId:", messageId); | |
console.log("owner:", owner); | |
const apiKey = '7C8A9F4E-8BA3-4AD3-BFB5-30DADFBD140E'; | |
const markAsReadUrl = 'https://api-wpp.wpp.chat/chat/markMessageAsRead/contato'; | |
const markAsReadPayload = { | |
read_messages: [ | |
{ | |
remoteJid: messageData.data.key.remoteJid, | |
fromMe: false, | |
id: messageId | |
} | |
] | |
}; | |
const reader = await axios.put(markAsReadUrl, markAsReadPayload, { | |
headers: { | |
'Content-Type': 'application/json', | |
'apikey': apiKey, | |
}, | |
}); | |
console.log(reader); | |
const transcriptionUrl = 'https://api-wpp.wpp.chat/chat/getBase64FromMediaMessage/contato'; | |
await new Promise(resolve => setTimeout(resolve, 2000)); | |
const requestPayload = { | |
message: { | |
key: { | |
id: messageId, | |
}, | |
}, | |
convertToMp4: false, | |
}; | |
const requestHeaders = { | |
headers: { | |
'Content-Type': 'application/json', | |
'apikey': apiKey, | |
}, | |
}; | |
// Log do corpo e dos cabeçalhos da requisição | |
console.log("Enviando requisição para a API:"); | |
console.log("URL:", transcriptionUrl); | |
console.log("Corpo da requisição:", requestPayload); | |
console.log("Cabeçalhos da requisição:", requestHeaders); | |
const axiosInstance = axios.create({ | |
headers: requestHeaders.headers | |
}); | |
const response = await axiosInstance.post(transcriptionUrl, requestPayload); | |
console.log(response); | |
const audioBase64 = response.data.base64; | |
const downloadsPath = path.join(__dirname, 'downloads', owner); | |
if (!fs.existsSync(downloadsPath)) { | |
fs.mkdirSync(downloadsPath, { recursive: true }); | |
} | |
const fileName = `${messageId}.ogg`; | |
const filePath = path.join(downloadsPath, fileName); | |
fs.writeFileSync(filePath, audioBase64, 'base64'); | |
const publicAudioUrl = `https://isa-bot-apis.growby.ai/${filePath}`; | |
console.log(publicAudioUrl); | |
// const transcriptionApiUrl = 'https://isatranscreve.growby.ai/transcrever'; | |
// const transcribeResponse = await axios.post(transcriptionApiUrl, { | |
// cliente: owner, | |
// audioUrl: publicAudioUrl, | |
// }); | |
// const transcriçãoData = transcribeResponse.data; | |
// console.log(transcriçãoData); | |
// await salvarTranscricaoNoBanco(messageData.data.key.remoteJid, publicAudioUrl, messageData.data.message.audioMessage.seconds, transcriçãoData.textoTranscricao); | |
} catch (error) { | |
console.error('Erro ao processar mensagem de áudio:', error); | |
formatarErroAxios(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment