Search code examples
javascriptnode.jsexpressmessagewhatsapp

Sending Multiple Images in a Single Message Using WhatsApp-WebJS


I am attempting to send multiple images in a single WhatsApp message using the WhatsApp-WebJS library. While my code executes without errors and returns "success," the images and message are not received on WhatsApp. I'm seeking guidance on how to resolve this issue and ensure successful image transmission.

Code Sample:

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);
    }
}

Issue Description: I have tried to send multiple images in a single WhatsApp message using the WhatsApp-WebJS library, but the images and message are not being received on WhatsApp. My code doesn't throw any errors, and it reports success. However, the message never appears in the chat.

  1. Checked for errors or exceptions in the console, but found none.
  2. Verified that the image files exist and are accessible.

I expect that when I send a message with multiple images, the message along with all the images should be received on WhatsApp without any issues.

Additional Information:

  • WhatsApp-WebJS version: ["https://github.com/Julzk/whatsapp-web.js/tarball/jkr_hotfix_7"]
  • Node.js version: [v18.17.0]

Solution

  • Not possible. What you see on whatsapp is a barrage of individual images. You should place your sendMessage function call within the function instead of pushing into mediaArray.