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
15.17k stars 3.62k forks source link

Evaluation failed: a at ExecutionContext._ExecutionContext_evaluate #3261

Closed SyaPratama closed 4 weeks ago

SyaPratama commented 1 month ago

Is there an existing issue for this?

Describe the bug

i am getting error when i try to upload video to user when user send Command

Evaluation failed: a at ExecutionContext._ExecutionContext_evaluate (C:\Users\inira\OneDrive\Documents\BotWa\node_modules\whatsapp-web.js\node_modules\puppeteer-core\lib\cjs\puppeteer\common\ExecutionContext.js:229:15) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ExecutionContext.evaluate (C:\Users\inira\OneDrive\Documents\BotWa\node_modules\whatsapp-web.js\node_modules\puppeteer-core\lib\cjs\puppeteer\common\ExecutionContext.js:107:16) at async Client.sendMessage (C:\Users\inira\OneDrive\Documents\BotWa\node_modules\whatsapp-web.js\src\Client.js:947:28) at async WhatsAppBot.handleVideo (file:///C:/Users/inira/OneDrive/Documents/BotWa/index.js:504:9) at async handleFeature (file:///C:/Users/inira/OneDrive/Documents/BotWa/index.js:111:9) at async WhatsAppBot.handleCommonFeatures (file:///C:/Users/inira/OneDrive/Documents/BotWa/index.js:124:5)
at async Client. (file:///C:/Users/inira/OneDrive/Documents/BotWa/index.js:69:7)

and I've done the replacement

this.client = new Client({ authStrategy: new LocalAuth(), // Your authstrategy here puppeteer: { headless: true, args: ["--no-sandbox", "--disable-setuid-sandbox"], executablePath: "C:\Program Files\Google\Chrome\Application\chrome.exe", // Path ke Google Chrome di Windows }, webVersionCache: { type: "remote", remotePath: https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${wwebVersion}.html, }, });

and i use exodus

"whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#webpack-exodus"

Expected behavior

it should be when I upload the video and I've buffered it. The video can be sent to the user who sent the request

Steps to Reproduce the Bug or Issue

This is my code to handle send video to whatsapp

Relevant Code

async downloadYouTubeVideo(query) { return new Promise((resolve, reject) => { const isYouTubeLink = /^(https?:\/\/)?(www.)?(youtube.com\/|youtu.be\/).+$/i.test(query);

  // Tentukan perintah berdasarkan apakah query adalah link atau judul
  const command = isYouTubeLink
    ? `yt-dlp -f bestvideo+bestaudio --merge-output-format mp4 -o "${path.join(
        this.outputPath,
        "%(title)s.%(ext)s"
      )}" "${query}"`
    : `yt-dlp -f bestvideo+bestaudio --merge-output-format mp4 -o "${path.join(
        this.outputPath,
        "%(title)s.%(ext)s"
      )}" "ytsearch:${query}"`;

  exec(command, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error saat mengunduh video: ${error.message}`);
      return reject(error);
    }
    if (stderr) {
      console.error(`stderr: ${stderr}`);
      return reject(new Error(stderr));
    }

    fs.readdir(this.outputPath, (err, files) => {
      if (err) {
        console.error(`Error membaca direktori: ${err.message}`);
        return reject(err);
      }

      const mp4Files = files.filter((file) => file.endsWith(".mp4"));
      if (mp4Files.length === 0) {
        return reject(new Error("No MP4 files found"));
      }

      const mp4File = path.join(this.outputPath, mp4Files[0]);
      resolve(mp4File);
    });
  });
});

}

async handleVideo(message) { const videoQuery = message.body.replace(/^(video|film)\s*/i, "").trim(); const statusMessage = await message.reply("Sedang mengunduh video...");

try {
  // Unduh video dari YouTube
  const videoPath = await this.downloadYouTubeVideo(videoQuery);

  if (fs.existsSync(videoPath)) {
    // Membaca video sebagai buffer
    const fileBuffer = fs.readFileSync(videoPath);
    const fileExtension = path.extname(videoPath).slice(1); // Mengambil ekstensi file tanpa titik
    const fileName = path.basename(videoPath); // Nama file

    // Membuat objek MessageMedia dengan buffer
    const media = new MessageMedia(
      `video/${fileExtension}`, // Mimetype video (misalnya: video/mp4)
      fileBuffer.toString("base64"), // Base64 encoded video
      fileName // Nama file
    );

    // Mendapatkan chatId dari pesan
    const chatId = message.from; // Use message.from for chat ID

    // Mengirim video dengan caption
    await this.client.sendMessage(chatId, media, {
      caption: "Video telah siap!",
    });

    // Hapus file setelah dikirim
    fs.unlinkSync(videoPath);
    await statusMessage.edit("Video berhasil dikirim.");
  } else {
    await statusMessage.edit("Gagal mengunduh video.");
  }
} catch (error) {
  console.error("Error saat mengunduh atau mengirim video:", error);
  await statusMessage.edit("Gagal mengunduh video.");
}

}

Browser Type

Google Chrome

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

Node.js Version v22.3.0 Npm Version v10.8.2 WhatsApp-Web.js Version v2.30.1 OS Windows

Additional context

No response