mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add option to reupload in downloadMediaMessage
This commit is contained in:
25
README.md
25
README.md
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user