open-wa / wa-automate-nodejs

💬 🤖 The most reliable tool for chatbots with advanced features. Be sure to 🌟 this repository for updates!
https://docs.openwa.dev/
Other
3.11k stars 591 forks source link

sendImageAsSticker throws error on success (Multi-Device) #2193

Closed ahmed-safari closed 2 years ago

ahmed-safari commented 2 years ago

Are you using the latest version of the library?

Current Behavior

`ThesendImageAsSticker` method throws an error even after successfully sending the sticker.

The error is:

Error: Evaluation failed: Error: Unexpected null or undefined: this.get(newSticker.id)
    at t.default (https://web.whatsapp.com/bootstrap_qr.d3ab17b51a79359ce87d.js:56:329142)
    at _.addNewSticker (https://web.whatsapp.com/bootstrap_qr.d3ab17b51a79359ce87d.js:55:544706)
    at _.addStickerWithMediaData (https://web.whatsapp.com/bootstrap_qr.d3ab17b51a79359ce87d.js:55:544546)
    at https://web.whatsapp.com/bootstrap_qr.d3ab17b51a79359ce87d.js:44:29797
    at V (https://web.whatsapp.com/bootstrap_qr.d3ab17b51a79359ce87d.js:2:379724)
    at MutationObserver.K (https://web.whatsapp.com/bootstrap_qr.d3ab17b51a79359ce87d.js:2:379303)
    at ExecutionContext._evaluateInternal (/home/nodejs/nodeApps/API/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:217:19)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (/home/nodejs/nodeApps/API/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:106:16)

Expected Behavior

It should send the sticker and move to the "then" block instead of the "catch" block.

Steps To Reproduce

client.sendImageAsSticker(from, imageBase64, { author: 'Ahmeds Bot', pack: 'Insta: @Download.Media', keepScale: true, discord: '295155004996714497' })
                            .then(() => {
                                console.log(`Sticker Sent!`)
                            }).catch(err => {

                                client.reply(from, `Oh No 😰\nAn Error Occured While Proccessing The Sticker.\nPlease Try Again In A Few Seconds..\n\nError: ${err.name}`, id)
console.error(err);

                            })

after running this command, the bot successfully sends the sticker but ends up sending an error message to the user as well as log the error mentioned above.

Mode

My own code

create() code

wa.create({
                sessionId: "InstagramBot",
                eventMode: true,
                authTimeout: 0,
                multiDevice : true,
                disableSpins: true,
                hostNotificationLang: 'en-gb',
                logConsole: false,
                qrTimeout: 0,
                callTimeout: 300 * 1000,
                licenseKey: 'XXXXXXXX-837C405D-XXXXXXXX-BC7CB7AD'
            })

DEBUG INFO

Debug info: {
  "WA_VERSION": "2.2138.13",
  "PAGE_UA": "WhatsApp/2.2108.8 Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
  "WA_AUTOMATE_VERSION": "4.22.5",
  "BROWSER_VERSION": "HeadlessChrome/88.0.4298.0",
  "OS": "Linux 5.4",
  "START_TS": 1633134373898
}

Environment

- OS: Ubuntu 
- Node: 14.15.1
- npm: 7.15.1

Screenshots

No response

Anything else?

No response

smashah commented 2 years ago

odd, thanks for filling template.

I guess with MD we should keep the mindset of anything that can go wrong will go wrong

smashah commented 2 years ago

is this still happening?

ahmed-safari commented 2 years ago

yes

smashah commented 2 years ago

@Sp0derDev I literally copy pasted your exact code and cannot replicate this odd behaviour where the promise both resolves and throws an error - which seems like an impossible situation as is.

I suggest trying to isolate the behaviour by using just this code and see if it happens again.

This is literally all I'm trying:

const start = async () => {
    try {
        const client = await create({
            headless: false,
            sessionId: "md_test",
            multiDevice: true,
            disableSpins: true,
            qrTimeout: 0,
            logConsole: true,
            logConsoleErrors: true,
            authTimeout: 0,
            licenseKey: "..."
        });
        client.onMessage(async m => {
            try {
                if (m.from === 'MY_NUMBER') {

                    let mediaData;
                    if (m.type === 'sticker') {
                        const stickerDecryptable = await client.getStickerDecryptable(m.id);
                        if (stickerDecryptable) mediaData = await decryptMedia(stickerDecryptable);
                    } else {
                        mediaData = await decryptMedia(m);
                    }

                    if (mediaData) {
                        client.sendImageAsSticker(m.from, `data:${m.mimetype};base64,${mediaData.toString('base64')}`, {
                                author: 'Ahmeds Bot',
                                pack: 'Insta: @Download.Media',
                                keepScale: true,
                                discord: '295155004996714497'
                            })
                            .then(() => {
                                console.log('SUCCESS', `Sticker Sent!`)
                            }).catch(err => {
                                client.reply(m.from, `Oh No 😰\nAn Error Occured While Proccessing The Sticker.\nPlease Try Again In A Few Seconds..\n\nError: ${err.name}`, m.id)
                                console.error('FAIL', err);
                            })
                    }
                }
            } catch (error) {
                console.log("🚀  start ~ error", error)
            }
        })
    } catch (error) {
        console.error(error)
    }
}

start();

Something else must be going on here. Till you can provide some reproducible code I will have to close this issue thanks.