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.02k stars 796 forks source link

Problem when trying to receive messages sent by cell phone #1133

Open ruanduarte opened 2 years ago

ruanduarte commented 2 years ago

Hello, guys.

I have to trigger the event where the cell phone sends messages (outside the webwhatsapp).

So, i changed the "_newMessagesListener " function in the wapi.js from:

window.WAPI._newMessagesListener = window.Store.Msg.on('add', (newMessage) => {
        if (newMessage && newMessage.isNewMsg && !newMessage.isSentByMe) {  
            let message = window.WAPI.processMessageObj(newMessage, true, false);
            if (message) {
                window.WAPI._newMessagesQueue.push(message);
                window.WAPI._newMessagesBuffer.push(message);
            }

            // Starts debouncer time to don't call a callback for each message if more than one message arrives
            // in the same second
            if (!window.WAPI._newMessagesDebouncer && window.WAPI._newMessagesQueue.length > 0) {
                window.WAPI._newMessagesDebouncer = setTimeout(() => {
                    let queuedMessages = window.WAPI._newMessagesQueue;

                    window.WAPI._newMessagesDebouncer = null;
                    window.WAPI._newMessagesQueue     = [];

                    let removeCallbacks = [];

                    window.WAPI._newMessagesCallbacks.forEach(function (callbackObj) {
                        if (callbackObj.callback !== undefined) {
                            callbackObj.callback(queuedMessages);
                        }
                        if (callbackObj.rmAfterUse === true) {
                            removeCallbacks.push(callbackObj);
                        }
                    });

                    // Remove removable callbacks.
                    removeCallbacks.forEach(function (rmCallbackObj) {
                        let callbackIndex = window.WAPI._newMessagesCallbacks.indexOf(rmCallbackObj);
                        window.WAPI._newMessagesCallbacks.splice(callbackIndex, 1);
                    });
                }, 1000);
            }
        }
    });
};

To this (changes only in the begin of the code):

window.WAPI._newMessagesListener = window.Store.Msg.on('add', (newMessage) => {
    let message;
    if (newMessage && newMessage.isNewMsg && newMessage.isSentByMe && newMessage.self === 'in') {
        message = window.WAPI.processMessageObj(newMessage, true, false);
    }
    else if (newMessage && newMessage.isNewMsg && !newMessage.isSentByMe) {
        message = window.WAPI.processMessageObj(newMessage, false, false);
    }else{
        message = false;
    }
    if (message) {
        if (message) {
            window.WAPI._newMessagesQueue.push(message);
            window.WAPI._newMessagesBuffer.push(message);
        }

        // Starts debouncer time to don't call a callback for each message if more than one message arrives
        // in the same second
        if (!window.WAPI._newMessagesDebouncer && window.WAPI._newMessagesQueue.length > 0) {
            window.WAPI._newMessagesDebouncer = setTimeout(() => {
                let queuedMessages = window.WAPI._newMessagesQueue;

                window.WAPI._newMessagesDebouncer = null;
                window.WAPI._newMessagesQueue     = [];

                let removeCallbacks = [];

                window.WAPI._newMessagesCallbacks.forEach(function (callbackObj) {
                    if (callbackObj.callback !== undefined) {
                        callbackObj.callback(queuedMessages);
                    }
                    if (callbackObj.rmAfterUse === true) {
                        removeCallbacks.push(callbackObj);
                    }
                });

                // Remove removable callbacks.
                removeCallbacks.forEach(function (rmCallbackObj) {
                    let callbackIndex = window.WAPI._newMessagesCallbacks.indexOf(rmCallbackObj);
                    window.WAPI._newMessagesCallbacks.splice(callbackIndex, 1);
                });
            }, 1000);
        }
    }
});

But after the user sends some messages, the js "pauses" for a while (5 or 10 minutes) without triggering any new messages event, and then after that "pause" interval it goes back to showing all messages that were not triggered previously. I assume the code goes into some loop that I don't know where it is.

Neither python nor the debugger of the browser where webwhatsapp is does not catch any error.

Does anyone know where the error would be in this code?