whatsapp-web.js
whatsapp-web.js copied to clipboard
downloadMedia() when downloading gifs
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
When trying to download a gif with message.downloadMedia() the function returns 'undefined'. After some digging i found that the line 400 in Message.js is the responsible for the problem. Line: if (msg.mediaData.mediaStage.includes('ERROR') || msg.mediaData.mediaStage === 'FETCHING') {
the problem is 'msg.mediaData.mediaStage' equals to 'FETCHING' causing it to return undefined. After checking that the media is downloaded correctly on chromium dev tools (in chrome the mediaStage is 'NEED_POKE'), i have removed the check for 'FETCHING' and now the gif is download correctly. It's safe to remove this line? What should i do? This line was modified on issue #763
Expected behavior
I expect the gif is downloaded but downloadMedia is returning 'undefined'
Steps to Reproduce the Bug or Issue
Send a gif to your own number.
Relevant Code
var media = await message.downloadMedia();
media is 'undefined'
Browser Type
Chromium
WhatsApp Account Type
WhatsApp Business
Does your WhatsApp account have multidevice enabled?
Yes, I am using Multi Device
Environment
windows + latest whatsapp-web.js npm version
Additional context
No response
If you don't check if the media is undefined the media will be always undefined
I do check if media is undefined on downloadMedia() return. But the 'fetching' check doesn't make sense to me, but i don't have enough experience on this project to affirm that. I was looking for an explanation for what could happen if i remove the 'fetching' check but keep the 'error' check part. Can you clarify? I would be grateful.
Write proper steps to reproduce the bug, downloading gifs from whatsapp (which are always .mp4 if you dont know) works normally for me
I've got a very simillar problem, I tried to make a sticker bot and I consistently got the issue, where after a couple successful conversions, downloadMedia()
eventually starts returning undefined. After I removed || msg.mediaData.mediaStage === 'FETCHING'
in Message.js in the downloadMedia()
method, just like @leftbrasil, the issue was gone.
@gkp1 if you need code where the issue becomes apparent, you can try this code and convert media to stickers a couple times by quoting a message with a media and adding the caption "sticker":
client.on('message', async msg => {
if (msg.body == 'sticker' || msg.body == 'Sticker') {
if (msg.hasQuotedMsg) {
const quoted_msg = await msg.getQuotedMessage();
const chat = await msg.getChat();
const chat_id = chat.id._serialized;
const media_message = await quoted_msg.downloadMedia();
await client.sendMessage(chat_id, media_message, { sendMediaAsSticker: true });
}
}
});
Hopefully someone can eventially confirm whether it's safe to remove || msg.mediaData.mediaStage === 'FETCHING'
Your code looks right, I'm not sure if the chat Id serialized is correct tho. I believe You can use message.from instead of chat Id serialized, and it will still work for groups and private dms.
or even better, what I use is msg.reply(media, undefined, {sendMediaAsSticker:true}) ( yes undefined is required there)
If you use chrome stable instead of chromium, this will also work with sending animated stickers for .mp4 and gif videos. As I said GIFs in the WhatsApp chat are just short mp4s.
https://wwebjs.dev/guide/handling-attachments.html#caveat-for-sending-videos-and-gifs
Thank you for your suggestions. Using message.from
seemst to be more elegant, I just changed it for me. As for msg.reply()
, I deliberately didn't use it, because I didn't want the bot to quote the message. I have no problems converting diffrent types of media to sticker, my only issue was that it would stop working after a short while and removing || msg.mediaData.mediaStage === 'FETCHING'
seemed to fix the issue for me.
Before i posted this issue, I did some research and found that might be a problem when you try to download a sticker that already have been downloaded before and/or when sending messages to myself (same number - from and to equals)
I thought the same, but I noticed, that it doesn't have to be the same media all the time for the code I sent to start failing. The 'FETCHING' thing was deffenetly the cause.
Now and then, I get a slight feeling that there are concepts in play here, that are currently beyond my understanding lol
I can confirm removing || msg.mediaData.mediaStage === 'FETCHING'
fixes it. Thanks!
It happens to me too with gif and videos, it also works by removing the fetching. I get the video and everything being able to manipulate it succesfully but I don't really like to edit the package, I would like to stay within the code that comes when updating with npm to have the latest becuase it looks like an active package that will get a lot of other features, also maintaining that code change will be really painfull.
There must be something that that line is doing, right? otherwise somebody would have sent a merge request removing that line already.
Is there any other solution to this? I tried a couple and non of them worked, I get undefined no matter what
Well, actually nobody could explain why these lines are there. Maybe we could PR this and try it out
I've got a very simillar problem, I tried to make a sticker bot and I consistently got the issue, where after a couple successful conversions,
downloadMedia()
eventually starts returning undefined. After I removed|| msg.mediaData.mediaStage === 'FETCHING'
in Message.js in thedownloadMedia()
method, just like @leftbrasil, the issue was gone.@gkp1 if you need code where the issue becomes apparent, you can try this code and convert media to stickers a couple times by quoting a message with a media and adding the caption "sticker":
client.on('message', async msg => { if (msg.body == 'sticker' || msg.body == 'Sticker') { if (msg.hasQuotedMsg) { const quoted_msg = await msg.getQuotedMessage(); const chat = await msg.getChat(); const chat_id = chat.id._serialized; const media_message = await quoted_msg.downloadMedia(); await client.sendMessage(chat_id, media_message, { sendMediaAsSticker: true }); } } });
Hopefully someone can eventially confirm whether it's safe to remove
|| msg.mediaData.mediaStage === 'FETCHING'
Yeah this work for me, just remove
|| msg.mediaData.mediaStage === 'FETCHING'
in node_modules/whatsapp-web.js/src/structures/Message.js
Can someone make PR to fix this problem? Or this will make another problem?
https://github.com/pedroslopez/whatsapp-web.js/pull/2491
just opened a PR to fix this
I can confirm removing
|| msg.mediaData.mediaStage === 'FETCHING'
fixes it. Thanks!
how did you manage to use whatsapp web.js to convert gif to sticker i have been trying this for like eternity
I can confirm removing
|| msg.mediaData.mediaStage === 'FETCHING'
fixes it. Thanks!
Hello, may I ask if there are any new issues after deleting this line of code? Will frequent calls increase the frequency of being banned
That line of code is from the node modules right?
On Mon, Apr 8, 2024, 9:17 AM ethan @.***> wrote:
I can confirm removing || msg.mediaData.mediaStage === 'FETCHING' fixes it. Thanks!
Hello, may I ask if there are any new issues after deleting this line of code? Will frequent calls increase the frequency of being banned
— Reply to this email directly, view it on GitHub https://github.com/pedroslopez/whatsapp-web.js/issues/2029#issuecomment-2042137250, or unsubscribe https://github.com/notifications/unsubscribe-auth/BA7EIS6DZY3HUBGI5FAQQODY4JHC7AVCNFSM6AAAAAAVH3QB52VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBSGEZTOMRVGA . You are receiving this because you commented.Message ID: @.***>