Skip to content

Commit

Permalink
fix(whatsapp.gblib): Fixed audio with IBM Watson.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Feb 2, 2025
1 parent a654b9a commit 54b2b6a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
71 changes: 62 additions & 9 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (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');

Expand All @@ -1182,15 +1197,11 @@ 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;

// Processes group behaviour.
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 54b2b6a

Please sign in to comment.