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 797 forks source link

Set chat as unread #1032

Open erm3nda opened 3 years ago

erm3nda commented 3 years ago

I've been digging inside window.Store.Chat and cannot found a way to set a chat as unread.

Does anyone have any idea of this?

I tried also setting unreadCount and markedUnread, but that only works locally in the webdriver. Such changes are not synced to the phone. Trying with hasUnread throws error because it's a derived property (surely from unreadCount).

Cannot find property, event nor function that does that, but indeed it works when you choose that option in the context menu in the web app or phone.

Regards.

erm3nda commented 1 year ago

Got it today.

Steps: 1) make sure Store loads the required module by adding to the very first lines in wapi.js:

                {
                    id: "MarkUnread",
                    conditions: (module) => (module.markUnread) ? module.markUnread : null
                },

and create the function:

window.WAPI.markUnread = function(id, done) {
    var chat = window.WAPI.getChat(id);
    if (chat !== undefined) {
        if (done !== undefined) {
            if (chat.getLastMsgKeyForAction === undefined)
                chat.getLastMsgKeyForAction = function() {};

            Store.MarkUnread(chat, true);
            done(true);
            return true;
        } else { // esto nunca debería suseder
            Store.MarkUnread(chat, true); // unreadCount will be -1. To set normal, send false instead, and it will be 0.
            return true;
        }
    }
    if (done !== undefined) done();
    return false;
};

2) Create the methods in init.py (webwhatsapi module) and the Chat.py interface. This is needed to be able to call the function directly to get a chat, then chat.mark_unread(). Chat_id is xx666666666-@c.us string, serialized id.

    def chat_mark_unread(self, chat_id):
        """
        Set chat as unread given its ID

        :param chat_id: Chat ID
        :type chat_id: str
        """
        return self.wapi_functions.markUnread(chat_id)

3) Add that function to Chat.js object:

    @driver_needed
    def mark_unread(self):
        return self.driver.chat_mark_unread(self.id)

And now, you can use markUnread the very same way you use send_seen. Set false at markUnread to set as normal. I had a problem with the true/false parameter, and simply created 2 functions, one to markUnread and markRead, which sets false.