From 71bb9d9a7ca1e617b56c232fa175777169e2fc99 Mon Sep 17 00:00:00 2001 From: SrDouglax <69745104+SrDouglax@users.noreply.github.com> Date: Wed, 18 Sep 2024 14:04:18 -0300 Subject: [PATCH] show how to send media from Buffer --- .../creating-your-bot/handling-attachments.md | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/guide/creating-your-bot/handling-attachments.md b/src/guide/creating-your-bot/handling-attachments.md index b9654f8..62c67c0 100644 --- a/src/guide/creating-your-bot/handling-attachments.md +++ b/src/guide/creating-your-bot/handling-attachments.md @@ -85,6 +85,24 @@ client.on('message', async (msg) => { }); ``` +### Sending Files from a Buffer + +You can also manipulate data from other formats and create a new MessageMedia object: + +```javascript +const { MessageMedia } = require('whatsapp-web.js'); + +client.on('message', async (msg) => { + if (msg.body === '!send-media') { + // Create an buffer object + const someBuffer = Buffer.from(...); + + const media = new MessageMedia('mime/type', someBuffer.toString("base64")); + await client.sendMessage(msg.from, media); + } +}); +``` + ### Caveat for Sending Videos and GIFs Whatsapp-web.js uses [Puppeteer](https://github.com/Puppeteer/Puppeteer), which comes bundled with the Chromium browser, an open source version of the popular Google Chrome browser. Since AAC and H.264 are licensed formats, they are not supported by Chromium. More info on this can be found on the [Puppeteer documentation](https://developer.chrome.com/docs/puppeteer/faq#what_features_does_puppeteer_not_support). @@ -103,4 +121,4 @@ The `executablePath` value will depend on your OS and install location for Chrom * **macOS:** `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome` * **Windows**: `C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe` -* **Linux:** `/usr/bin/google-chrome-stable` \ No newline at end of file +* **Linux:** `/usr/bin/google-chrome-stable`