pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
14.53k stars 3.46k forks source link

Sending Local Files Are Not Working #1395

Closed NyllRE closed 2 years ago

NyllRE commented 2 years ago

Is there an existing issue for this?

Describe the bug

I've made code that takes in a link and decides if it is a localfile or a url to send it to the user, the MessageMedia.fromFilePath function seems to not work at all

Expected behavior

expected:

image should be sent

result:

sending image results in error

Steps to Reproduce the Bug or Issue

this is the function that returns the media

const sendMedia = async (msg) => {
    let media;
    if (msg.startsWith('http')) {
        media = await MessageMedia.fromUrl(msg);
    } else {
        try {
            media = await MessageMedia.fromFilePath("./files/" + msg)
        } catch (e) {
            console.log("File Not Found!!", e);
            media = "file was not found"
        }
    }
    return media
}

the function is being called here:

client.on('message', async message => {
    ...
        const link = 'linktomedia.pdf'
        const media = await sendMedia(link)
        client.sendMessage(message.from, media);
        // I tried message.reply(media) too if you're curious
    }
  }
  else message.reply(data[0].response)
})

Relevant Code

these are the error messages:

/home/user/dev/wabot-production/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221
            throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails));
                  ^

Error: Evaluation failed: DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
    at Object.window.WWebJS.mediaInfoToFile (__puppeteer_evaluation_script__:385:35)
    at Object.window.WWebJS.processMediaData (__puppeteer_evaluation_script__:232:36)
    at Object.window.WWebJS.sendMessage (__puppeteer_evaluation_script__:19:39)
    at __puppeteer_evaluation_script__:10:45
    at ExecutionContext._evaluateInternal (/home/nyll/dev/wabot-production/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ExecutionContext.evaluate (/home/nyll/dev/wabot-production/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:110:16)
    at async Client.sendMessage (/home/user/dev/wabot-production/node_modules/whatsapp-web.js/src/Client.js:594:28)

the error seems to be coming from puppeteer

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS: Linux Ubuntu 20.04.4 Phone OS: Android 12 whatsabb-web.js: 1.16.5 NodeJS: v16.14.2

Additional context

the error only happens when a user requests a localfile, the local file is there and I checked the local file's name and even made an exception but puppeteer's errors seem to pass the try argument

KeruMx commented 2 years ago

you error is because you are doing this media = await MessageMedia.fromFilePath("./files/" + msg) try just like this media = await MessageMedia.fromFilePath("./files/")

NyllRE commented 2 years ago

try just like this media = await MessageMedia.fromFilePath("./files/")

het-t commented 2 years ago

I recently used string interpolation ${msg} and it works completely fine ! Can you try that for me ?

NyllRE commented 2 years ago

you mean something like

MessageMedia.fromFilePath(`./files/${msg}`)

because I tried multiple ways of inserting the string. I tried to log the media it retrieves and what I got was:

MessageMedia {
  mimetype: 'application/pdf',
  data: '',
  filename: 'UserGuid.pdf'
}

so the naming is right and everything. it's just not able to send the data pro... hold up. the file I sent was empty. I tried to put in dummy data and it worked... before an update I was able to send an empty file and it worked fine then... something happened in the new update that made it get broken when you send empty files... this wasted hella lot of my time lol.

NyllRE commented 2 years ago

so the issue is resolved, but there is a secondary issue which is sending empty files. it used to work and now it doesn't work so this should be looked at in the future because it was really confusing.

digitalsolitario commented 1 year ago

Error: Evaluation failed: TypeError: window.Store.MediaPrep.prepRawMedia is not a function 0|server | at window.WWebJS.processMediaData (puppeteer_evaluation_script:235:50) 0|server | at async window.WWebJS.sendMessage (puppeteer_evaluation_script:19:19) 0|server | at async puppeteer_evaluation_script:10:25 0|server | at ExecutionContext._evaluateInternal (F:\xampp\htdocs\Whatsapp\node\node_modules\whatsapp-web.js\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19) 0|server | at processTicksAndRejections (internal/process/task_queues.js:95:5) 0|server | at async ExecutionContext.evaluate (F:\xampp\htdocs\Whatsapp\node\node_modules\whatsapp-web.js\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16) 0|server | at async Client.sendMessage (F:\xampp\htdocs\Whatsapp\node\node_modules\whatsapp-web.js\src\Client.js:686:28)

I got this error when i am sending Image.

NyllRE commented 1 year ago

@digitalsolitario are you sure the image is not empty for testing? when I tried sending empty images for testing it didn't work so make sure you are sending a real image