fix(whatsapp.gblib): Fixed audio with IBM Watson.

This commit is contained in:
Rodrigo Rodriguez 2025-02-02 17:06:43 -03:00
parent a654b9a35c
commit 54b2b6a56e
2 changed files with 64 additions and 11 deletions

View file

@ -1606,7 +1606,7 @@ export class GBMinService {
// Removes unwanted chars in input text.
step.context.activity['originalText'] = context.activity.text;
const text = await GBConversationalService.handleText(min, user, step, context.activity.text);
const text = context.activity.text;
step.context.activity['originalText'];
step.context.activity['text'] = text;

View file

@ -1169,8 +1169,23 @@ private async sendButtonList(to: string, buttons: string[]) {
user = await sec.updateUserLocale(user.userId, locale);
}
}
if (req.type === 'ptt') {
if (process.env.AUDIO_DISABLED !== 'true') {
if (provider ==='meta'){
const buf = await this.downloadAudio(req, min);
text = await GBConversationalService.getTextFromAudioBuffer(
this.min.instance.speechKey,
this.min.instance.cloudLocation,
buf,
user.locale
);
}else if (req.type === 'ptt') {
const media = await req.downloadMedia();
const buf = Buffer.from(media.data, 'base64');
@ -1182,13 +1197,9 @@ private async sendButtonList(to: string, buttons: string[]) {
);
req.body = text;
} else {
await this.sendToDevice(
user.userSystemId,
`No momento estou apenas conseguindo ler mensagens de texto.`,
null
);
}
}
let activeMin;
@ -1488,4 +1499,46 @@ private async sendButtonList(to: string, buttons: string[]) {
return result;
}
public async downloadAudio(req, min) {
// Extract the audio ID from the request body
const audioId = req.body.entry[0].changes[0].value.messages[0].audio.id;
// Meta WhatsApp Business API endpoint for downloading media
const metaApiUrl = `https://graph.facebook.com/v16.0/${audioId}`;
// User access token from min.whatsappServiceKey
const userAccessToken = min.whatsappServiceKey;
// Fetch the media URL using the audio ID
const mediaUrlResponse = await fetch(metaApiUrl, {
headers: {
Authorization: `Bearer ${userAccessToken}`,
},
});
if (!mediaUrlResponse.ok) {
throw new Error(`Failed to fetch media URL: ${mediaUrlResponse.statusText}`);
}
const mediaUrlData = await mediaUrlResponse.json();
const mediaUrl = mediaUrlData.url;
if (!mediaUrl) {
throw new Error('Media URL not found in the response');
}
// Download the audio file
const audioResponse = await fetch(mediaUrl, {
headers: {
Authorization: `Bearer ${userAccessToken}`,
},
});
if (!audioResponse.ok) {
throw new Error(`Failed to download audio: ${audioResponse.statusText}`);
}
return audioResponse.body;
}
}