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.96k stars 3.55k forks source link

Unable to send media #714

Closed Diego-Schmidt closed 3 years ago

Diego-Schmidt commented 3 years ago

Bug description

Unable to send media on NodeJS 14 , windows 10 , using v1.12.6 source zip and example.js

Reproduction steps

Added code to example.js to send a test picture "picture.jpg" on command !sendpic

Expected behavior

Bot responding to !sendpic command and sending picture.jpg to the user

Relevant code

Added

else if (msg.body === '!sendpic') {
        // Send a new message as a reply to the current one
        const media = MessageMedia.fromFilePath('./media/picture.jpg');
        chat.sendMessage(media);

      }

To example.js

Environment (please complete the following information):

WhatsApp

Library

Additional context

Error on console

(node:16328) UnhandledPromiseRejectionWarning: ReferenceError: MessageMedia is not defined at Client. (C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\paya.js:49:23) at Client.emit (events.js:315:20) at C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\src\Client.js:238:18 at Page._onBindingCalled (C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:717:62) at C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:151:60 at C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:62 at Array.map () at Object.emit (C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\node_modules\puppeteer\lib\cjs\vendor\mitt\src\index.js:51:43) at CDPSession.emit (C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\node_modules\puppeteer\lib\cjs\puppeteer\common\EventEmitter.js:72:22) at CDPSession._onMessage (C:\Users\user\Downloads\bot\whatsapp-web.js-1.12.6\node_modules\puppeteer\lib\cjs\puppeteer\common\Connection.js:225:18) (Use node --trace-warnings ... to show where the warning was created) (node:16328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:16328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Diego-Schmidt commented 3 years ago

All the commands of example.js are running ok.

PurpShell commented 3 years ago

You haven't required MessageMedia!!!!!!

const {Client, **MessageMedia**} = require("./index.js");

add that bit to the code, you will find

const {Client} = require("./index.js");

something like this on the top of your code, add the comma and MessageMedia.

Useful Links:

Best regards,

PurpShell

PurpShell commented 3 years ago

Closing this issue, you are welcome to open it again if you think this was done in error.

Best regards,

PurpShell

Diego-Schmidt commented 3 years ago

The MEssagemedia not defined problem is solved, but i have "ReferenceError: chat is not defined" when i use !sendpic , should I add Chat to the line? I have added Chat to the const line but it is not working. By the way, thanks for answer my noob questions.

const { Client, Location, MessageMedia, Chat} = require('./index');

Diego-Schmidt commented 3 years ago

Solved, changed the code chat.sendMessage(media); To client.sendMessage(msg.from, media); and then it worked.