我正在尝试使用 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 上未收到图像和消息。我的代码没有抛出任何错误,并且报告成功。但是,该消息从未出现在聊天中。
我希望当我发送包含多个图像的消息时,WhatsApp 上应该可以毫无问题地接收该消息以及所有图像。
附加信息:
不可能。你在 Whatsapp 上看到的是一系列单独的图像。您应该将 sendMessage 函数调用放在函数中,而不是推入 mediaArray 中。