I am trying to send a message to a group using below code. The code works fine, however dont see message in the group.
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
// Initialize the client
const client = new Client();
// Generate QR code and display it
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
console.log('Scan the QR code with your WhatsApp mobile app');
});
// Handle successful authentication
client.on('authenticated', () => {
console.log('Authenticated');
});
// Connect to WhatsApp Web
client.initialize().catch(error => {
console.error('Connection error:', error);
});
// Event listener for successful connection
client.on('ready', () => {
console.log('Connected to WhatsApp Web');
// Now you can perform actions such as sending messages
// For example, sending a message to a group
const groupId = 'group_id';
const message = 'Hello, group!';
client.sendMessage(groupId, message).then(() => {
console.log('Message sent successfully');
// Disconnect from WhatsApp Web
client.destroy().then(() => {
console.log('Disconnected from WhatsApp Web');
});
}).catch(error => {
console.error('Failed to send message:', error);
// Disconnect from WhatsApp Web
client.destroy().then(() => {
console.log('Disconnected from WhatsApp Web');
});
});
});
// Event listener for connection errors
client.on('auth_failure', () => {
console.error('Authentication failure. Please check your credentials or session file.');
});
client.on('disconnected', reason => {
console.log('Disconnected from WhatsApp Web:', reason);
});