Get All Participants Data from A Whatsapp Group using whatsappweb.js | How to Export or Extract WhatsApp Group Contacts to Excel
Raunit Verma - in Projects
Views: 7
Unlock the potential of whatsappweb.js to gather all participant data from your WhatsApp group. Simplify your data collection process now. Find out how to export WhatsApp contacts in this detailed guide. It has included a step by step solution to export WhatsApp group contacts to Excel easily.
const { Client, LocalAuth, MessageMedia } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const fs = require('fs');
const path = require('path');
const fastcsv = require('fast-csv');
const csvFilePath = path.join(__dirname, 'group_contacts.csv');
const SESSION_FILE_PATH = './session.json';
let sessionData;
if (fs.existsSync(SESSION_FILE_PATH)) {
sessionData = require(SESSION_FILE_PATH);
}
const client = new Client({
authStrategy: new LocalAuth({ clientId: 'client-one' }),
});
client.on('qr', (qr) => {
console.log('QR RECEIVED', qr);
qrcode.generate(qr, { small: true });
});
client.on('ready', async() => {
console.log('Client is ready!');
const chats = await client.getChats();
let groupContactsData = [];
for (let chat of chats) {
if (chat.id._serialized.endsWith('@g.us')) {
console.log(`Processing group: ${chat.name}`);
try {
const groupChat = await client.getChatById(chat.id._serialized);
const participants = groupChat.groupMetadata.participants;
if (!participants) {
console.log(`No participants found in group: ${chat.name}`);
continue;
}
for (let participant of participants) {
try {
const contact = await client.getContactById(participant.id._serialized);
const contactInfo = {
groupName: chat.name,
groupId: chat.id._serialized,
participantNumber: participant.id.user || contact.number,
participantName: contact.name || contact.pushname || 'Unknown',
isAdmin: participant.isAdmin || false,
isSuperAdmin: participant.isSuperAdmin || false,
isMyContact: contact.isMyContact,
isBlocked: contact.isBlocked,
type: contact.type
};
groupContactsData.push(contactInfo);
console.log(`Processed participant: ${contactInfo.participantName} (${contactInfo.participantNumber})`);
} catch (error) {
console.error(`Error processing participant in ${chat.name}:`, error);
const basicContactInfo = {
groupName: chat.name,
groupId: chat.id._serialized,
participantNumber: participant.id.user,
participantName: 'Unknown',
isAdmin: participant.isAdmin || false,
isSuperAdmin: participant.isSuperAdmin || false,
error: 'Failed to fetch contact details'
};
groupContactsData.push(basicContactInfo);
}
}
console.log(`Successfully processed ${participants.length} participants in ${chat.name}`);
} catch (error) {
console.error(`Error processing group ${chat.name}:`, error);
}
}
}
const filePath = './group_contacts.json';
fs.writeFileSync(filePath, JSON.stringify(groupContactsData, null, 2), 'utf-8');
const ws = fs.createWriteStream(csvFilePath);
fastcsv
.write(groupContactsData, { headers: true })
.pipe(ws)
.on("finish", () => {
console.log(`Group contacts saved to ${csvFilePath}`);
})
.on("error", (err) => {
console.error("Error writing CSV:", err);
});
console.log(`Group contacts saved to ${filePath}`);
console.log(`Total contacts extracted: ${groupContactsData.length}`);
});
client.initialize().then(r => console.log("Client initialized")).catch(error => console.log(error));
Sample Video of How It Works
Watch this video to understand how to run the script and get all contacts from whatsapp group.
Tagswhatsapp-web.jsget all contacts from whatsapp groupjavascript code automation
Raunit Verma
Technical Writer at CodingKaro
Share this on