feat: add option to reupload in downloadMediaMessage

This commit is contained in:
Adhiraj Singh
2022-06-01 13:19:05 +05:30
parent f62898dee7
commit cafc707628
2 changed files with 74 additions and 27 deletions

View File

@@ -569,7 +569,7 @@ The presence expires after about 10 seconds.
If you want to save the media you received
``` ts
import { writeFile } from 'fs/promises'
import { downloadContentFromMessage } from '@adiwajshing/baileys'
import { downloadMediaMessage } from '@adiwajshing/baileys'
sock.ev.on('messages.upsert', async ({ messages }) => {
const m = messages[0]
@@ -578,18 +578,29 @@ sock.ev.on('messages.upsert', async ({ messages }) => {
const messageType = Object.keys (m.message)[0]// get what type of message it is -- text, image, video
// if the message is an image
if (messageType === 'imageMessage') {
// download stream
const stream = await downloadContentFromMessage(m.message.imageMessage, 'image')
let buffer = Buffer.from([])
for await(const chunk of stream) {
buffer = Buffer.concat([buffer, chunk])
}
// download the message
const buffer = await downloadMediaMessage(
m,
'buffer',
{ },
{
logger,
// pass this so that baileys can request a reupload of media
// that has been deleted
reuploadRequest: sock.updateMediaMessage
}
)
// save to file
await writeFile('./my-download.jpeg', buffer)
}
}
```
**Note:** WhatsApp automatically removes old media from their servers, and so for the device to access said media -- a re-upload is required by another device that has the media. This can be accomplished using:
``` ts
const updatedMediaMsg = await sock.updateMediaMessage(msg)
```
## Deleting Messages
``` ts