Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can I capture voice audios #39

Open
aledc7 opened this issue Aug 22, 2024 · 2 comments
Open

how can I capture voice audios #39

aledc7 opened this issue Aug 22, 2024 · 2 comments

Comments

@aledc7
Copy link

aledc7 commented Aug 22, 2024

How can I capture the voice audio of users?

In the docs we have: but nothing about capture audio.

// Send Voice Note

const filename = "myaudio.mp3";
const audio = fs.readFileSync(filename); // return Buffer
const send = await whatsapp.sendVoiceNote({
  sessionId: "session1",
  to: "6281234567890",
  media: audio,
});

how can we archive that?

Thank you

@Reda-l
Copy link

Reda-l commented Jan 26, 2025

Hi! Did you manage to figure this out?

If not, I was able to achieve it using whiskeysockets/baileys. Here's a snippet of my implementation that captures and processes voice audio from users and saves it locally:

`import { downloadMediaMessage } from '@whiskeysockets/baileys';
import * as fs from 'fs';
import * as path from 'path';

private async handleAudioMessage(msg: any, sessionId: string): Promise {
try {
const audioDir = path.join('media_downloads', sessionId, 'audio');
fs.mkdirSync(audioDir, { recursive: true });

const mediaId = msg.message.audioMessage.mediaKey.toString('hex');
const filePath = path.join(audioDir, `${mediaId}.mp3`);

console.log('📥 Downloading audio message...');
const buffer = await downloadMediaMessage(msg, 'buffer', {});
fs.writeFileSync(filePath, buffer);

console.log('✅ Audio saved to:', filePath);
return filePath;

} catch (error) {
console.error('❌ Error handling audio message:', error);
throw new Error(Failed to process audio message: ${error.message});
}
}`

Good luck! ✌️

@aledc7
Copy link
Author

aledc7 commented Jan 26, 2025

Hi! Did you manage to figure this out?

If not, I was able to achieve it using whiskeysockets/baileys. Here's a snippet of my implementation that captures and processes voice audio from users and saves it locally:

`import { downloadMediaMessage } from '@whiskeysockets/baileys'; import * as fs from 'fs'; import * as path from 'path';

private async handleAudioMessage(msg: any, sessionId: string): Promise { try { const audioDir = path.join('media_downloads', sessionId, 'audio'); fs.mkdirSync(audioDir, { recursive: true });

const mediaId = msg.message.audioMessage.mediaKey.toString('hex');
const filePath = path.join(audioDir, `${mediaId}.mp3`);

console.log('📥 Downloading audio message...');
const buffer = await downloadMediaMessage(msg, 'buffer', {});
fs.writeFileSync(filePath, buffer);

console.log('✅ Audio saved to:', filePath);
return filePath;

} catch (error) { console.error('❌ Error handling audio message:', error); throw new Error(Failed to process audio message: ${error.message}); } }`

Good luck! ✌️

Thank you very much, just like you, I have managed to send audios by switching to the Baileys library. Greetings!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants