mukulhase / WebWhatsapp-Wrapper

An API for sending and receiving messages over web.whatsapp [Working as of 18th May 2018]
https://webwhatsapi.readthedocs.io/en/latest/
MIT License
2.04k stars 794 forks source link

Issue in loading image from long long time ago #727

Open stevo-king opened 5 years ago

stevo-king commented 5 years ago

I try to use the api to load an image from long long time ago, but the api cannot load it. I need to scoll on my web whatsapp to trigger the web whatsapp to cache the old image file first, so that I can use the script to load my old image.

May I know if there is any workaround so the whole process can be done on the script?

jesussales commented 5 years ago

Which funtion are you use?

rodrigolmacedo commented 5 years ago

@stevo-king are u try use loadAllearlierMessages funcion?

gfaraj commented 5 years ago

You can have it automatically open the chat and scroll to the message that contains the image so that it has to load it, you just have to poll it a few times:

            let maxWaitCount = 8;
            let imageWaitInterval = setInterval(function() {
                if (maxWaitCount <= 0) {
                    clearInterval(imageWaitInterval);
                    return;
                }
                maxWaitCount--;
                WAPI.getMessageById(message.quotedMsgObj.id, async (m) => {
                    console.log(m);
                    if (m && m.mediaData.mediaStage === 'RESOLVED') {
                        clearInterval(imageWaitInterval);
                        if (m.mediaData.mediaBlob) {
                            getBase64ImageData(m.mediaData.mediaBlob._blob, (data) => {
                                WAPI.getMessageById(message.id, (m2) => {
                                    m2.quotedMsgObj.body = data;
                                    processMessage(m2);
                                });
                            });
                        }
                        else if (m.type == "sticker") {
                            WAPI.getMessageById(message.id, (m2) => {
                                processMessage(m2);
                            });
                        }
                        else {
                            window.WAPI.downloadFileAndDecrypt({ url: m.clientUrl, type: m.type, mediaKey: m.mediaKey, mimetype: m.mimetype }, (data) => {
                                WAPI.getMessageById(message.id, (m2) => {
                                    m2.quotedMsgObj.body = data.result;
                                    processMessage(m2);
                                });
                            });
                        }
                    }
                    else {
                        let chat = Store.Chat.get(message.chatId);
                        await Store.UiController.openChatBottom(chat);
                        await chat.loadEarlierMsgs();
                        if (m) {
                            Store.UiController.scrollToPtt(m);
                        }
                    }
                });
            }, 3000);

That code has some extra function and I needed a reference to the Ui Controller, you can check out my version of the wapi.js file here:

https://github.com/gfaraj/super-bot/blob/master/clients/whatsapp/src/web/wapi.js