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

downloadMedia() when downloading gifs #2029

Open
1 task done
leftbrasil opened this issue Feb 25, 2023 · 16 comments
Open
1 task done

downloadMedia() when downloading gifs #2029

leftbrasil opened this issue Feb 25, 2023 · 16 comments
Labels
bug Something isn't working

Comments

@leftbrasil
Copy link
Contributor

Is there an existing issue for this?

  • 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

@leftbrasil leftbrasil added the bug Something isn't working label Feb 25, 2023
@Verzach3
Copy link

If you don't check if the media is undefined the media will be always undefined

@leftbrasil
Copy link
Contributor Author

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.

@gkp1
Copy link

gkp1 commented Mar 10, 2023

Write proper steps to reproduce the bug, downloading gifs from whatsapp (which are always .mp4 if you dont know) works normally for me

@Zi5han
Copy link

Zi5han commented Mar 17, 2023

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'

@gkp1
Copy link

gkp1 commented Mar 17, 2023

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

@Zi5han
Copy link

Zi5han commented Mar 17, 2023

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.

@leftbrasil
Copy link
Contributor Author

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)

@Zi5han
Copy link

Zi5han commented Mar 17, 2023

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

@andresindlp
Copy link

I can confirm removing || msg.mediaData.mediaStage === 'FETCHING' fixes it. Thanks!

@DannyAndres
Copy link

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

@leftbrasil
Copy link
Contributor Author

Well, actually nobody could explain why these lines are there. Maybe we could PR this and try it out

@rama4zis
Copy link

rama4zis commented Jul 3, 2023

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'

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?

@pedrokohler
Copy link

#2491

just opened a PR to fix this

@MikeyA-yo
Copy link

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

@ethan2cl
Copy link

ethan2cl commented Apr 8, 2024

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

@MikeyA-yo
Copy link

MikeyA-yo commented Apr 8, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants