使用 WhatsApp-WebJS 在一条消息中发送多个图像

问题描述 投票:0回答:1

我正在尝试使用 WhatsApp-WebJS 库在一条 WhatsApp 消息中发送多个图像。虽然我的代码执行时没有错误并返回“成功”,但 WhatsApp 上未收到图像和消息。我正在寻求有关如何解决此问题并确保图像传输成功的指导。

代码示例:

const { Client, MessageMedia  } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const path = require("path");
const client = new Client();

// Listen for the 'qr' event to display the QR code for authentication
client.on('qr', (qr) => {
    // Display the QR code for the user to scan
    console.log('Scan the QR code to authenticate:');
    console.log(qr);
    qrcode.generate(qr, {small: true});
});

// Listen for the 'ready' event, indicating the client is ready to use
client.on('ready', () => {
    console.log('Client is ready!');
    
    // Now that the client is ready, you can use it to check if a user is registered
    sendMultipleImagesFromURLs();
});

// Initialize the client
client.initialize();

// Function to send multiple images from URLs
async function sendMultipleImagesFromURLs() {
    const phoneNumber = '[email protected]'; // Recipient's phone number
    const imageUrls = [
        'https://example.com/image1.jpg',
        'https://example.com/image2.jpg',
        'https://example.com/image3.jpg',
    ];

    try {
        // Create an array to store MessageMedia objects for the images
        const mediaArray = [];

        // Create a MessageMedia object for the image
        for (const imageUrl of imageUrls) {
            const media = await MessageMedia.fromUrl(imageUrl);
            mediaArray.push(media);
        }

        // Send the images as attachments in a single message
        const msg = await client.sendMessage(phoneNumber, mediaArray);

        console.log('Multiple images sent successfully from URLs.');
    } catch (error) {
        console.error('Error sending multiple images from URLs:', error);
    }
}

问题描述: 我尝试使用 WhatsApp-WebJS 库在一条 WhatsApp 消息中发送多个图像,但 WhatsApp 上未收到图像和消息。我的代码没有抛出任何错误,并且报告成功。但是,该消息从未出现在聊天中。

  1. 在控制台检查是否有错误或异常,但没有发现。
  2. 已验证图像文件存在且可访问。

我希望当我发送包含多个图像的消息时,WhatsApp 上应该可以毫无问题地接收该消息以及所有图像。

附加信息:

  • WhatsApp-WebJS 版本:[“https://github.com/Julzk/whatsapp-web.js/tarball/jkr_hotfix_7”]
  • Node.js 版本:[v18.17.0]
javascript node.js express message whatsapp
1个回答
0
投票

不可能。你在 Whatsapp 上看到的是一系列单独的图像。您应该将 sendMessage 函数调用放在函数中,而不是推入 mediaArray 中。

© www.soinside.com 2019 - 2024. All rights reserved.