open-wa / wa-automate-python

💬 🤖 The most advanced Python whatsapp library for chatbots with advanced features. Be sure to ⭐ this repository for updates!
Other
330 stars 71 forks source link

Change profile information #17

Closed riquedev closed 3 years ago

riquedev commented 4 years ago

Is there a way to change photo, status and profile name? I noticed that there are functions in js, but only the Status one worked.

mrodal commented 4 years ago

Hey, I added support for changing the status and name. The name one was broken on wapi, but there's currently no support for changing the profile picture.

Please try the latest version, 1.1.4. The new driver methods are set_profile_status and set_profile_name

riquedev commented 4 years ago

Hey, I added support for changing the status and name. The name one was broken on wapi, but there's currently no support for changing the profile picture.

Please try the latest version, 1.1.4. The new driver methods are set_profile_status and set_profile_name

Thank you!! I used this method to change the profile picture, I don't know if it still works (it's manipulating the DOM)

window.WAPI.showInputProfilePicture = function (done) {
    function _waitFor(cond, ms, howMany) {
        function sleep() {
            return new Promise(function (cb, eb) {
                setTimeout(function () {
                    cb()
                }, ms)
            })
        }

        return new Promise(function (resolve, reject) {
            var counter = 0;

            function check() {
                try {
                    var v = cond()
                }
                catch (err) {
                    reject(err)
                    return
                }
                if (v)
                    resolve(v)
                else {
                    counter++;
                    if (counter == howMany)
                        reject("Element not found")
                    else
                        sleep().then(check)
                }
            }

            sleep().then(check)
        })
    }

    var event = document.createEvent("MouseEvent")
    event.initEvent("mousedown", true, true)
    document.querySelector("[data-icon='menu']").dispatchEvent(event, "mousedown")

    return _waitFor(function () {
        return document.querySelector("[data-icon='menu']").parentElement.nextSibling.firstElementChild.firstElementChild.children[1]
    }, 100, 20).then(function (el) {
        el.click()
        return _waitFor(function () {
            var input = document.getElementsByTagName("input"),
                i = undefined
            for (var k = 0; k < input.length; k++) {
                if (input[k].getAttribute("type") == "file" && input[k].hasAttribute("accept")) {
                    i = input[k]
                    break;
                }
            }
            if (i !== undefined) {
                i.style.display = "block"
                return i
            }
            else
                return false
        }, 10000, 20)
    }).then(function (el) {
        if (done !== undefined)
            done(el)
        return el
    }).catch(function (err) {
        console.log(err)
        if (done !== undefined)
            done(false)
    })
}

WAPI.clickChangeProfilePicture = function (done) {
    function _waitFor(cond, ms, howMany) {
        function sleep() {
            return new Promise(function (cb, eb) {
                setTimeout(function () {
                    cb()
                }, ms)
            })
        }

        return new Promise(function (resolve, reject) {
            var counter = 0;

            function check() {
                try {
                    var v = cond()
                }
                catch (err) {
                    reject(err)
                    return
                }
                if (v)
                    resolve(v)
                else {
                    counter++;
                    if (counter == howMany)
                        reject("Element not found")
                    else
                        sleep().then(check)
                }
            }

            sleep().then(check)
        })
    }

    return _waitFor(function () {
        var v = document.querySelector("[data-icon='checkmark-light-l']")
        if (v) {
            v.click();
            return true;
        }
        return false
    }, 200, 10).then(function () {
        if (done !== undefined)
            done(true)
    })
}

WAPI.hideInputProfilePicture = function (done) {
    var input = document.getElementsByTagName("input"),
        i = undefined
    for (var k = 0; k < input.length; k++) {
        if (input[k].getAttribute("type") == "file" && input[k].hasAttribute("accept")) {
            i = input[k]
            i.style.display = "none"
            break;
        }
    }

    var v = document.querySelector("[data-icon='back-light']")
    if (v)
        v.click()

    var result = false;

    if (i !== undefined)
        result = true;

    if (done !== undefined)
        done(result)
}
    def set_profile_picture(self, path):
        """
        param: path: full path of the image file that will be used as profile picture
        """
        import os
        if not os.path.exists(path):
            raise Exception("profile picture path not found")

        profilePictureInput = self.wapi_functions.showInputProfilePicture()

        if profilePictureInput:
            profilePictureInput.send_keys(path)
            self.wapi_functions.clickChangeProfilePicture()
            self.wapi_functions.hideInputProfilePicture()
            return True
        else:
            raise Exception("Can't find input for profile picture")

I even saw some calls to change the profile picture in the window.Store however I didn't know how to use it.