138 lines
4.2 KiB
JavaScript
138 lines
4.2 KiB
JavaScript
const { Client, LocalAuth } = require('whatsapp-web.js');
|
|
const qrcode = require('qrcode-terminal');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const BASE_DIR = path.join(__dirname);
|
|
const DATA_DIR = path.join(BASE_DIR, 'data');
|
|
const MESSAGES_FILE = path.join(DATA_DIR, 'messages.jsonl');
|
|
|
|
console.log('Data directory:', DATA_DIR);
|
|
|
|
const client = new Client({
|
|
authStrategy: new LocalAuth(),
|
|
puppeteer: {
|
|
executablePath: '/usr/sbin/chromium',
|
|
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']
|
|
}
|
|
});
|
|
|
|
console.log('Creating data dir:', DATA_DIR);
|
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
console.log('Data dir created');
|
|
|
|
function formatMessage(msg, chat) {
|
|
const phoneNumber = msg.from ? msg.from.split('@')[0] : '';
|
|
return {
|
|
id: msg.id._serialized,
|
|
chatId: chat.id._serialized,
|
|
chatName: chat.name,
|
|
from: phoneNumber,
|
|
fromName: msg.author || phoneNumber,
|
|
body: msg.body || '',
|
|
timestamp: msg.timestamp,
|
|
type: msg.type,
|
|
hasMedia: msg.hasMedia,
|
|
isGroup: chat.isGroup,
|
|
isMe: msg.fromMe
|
|
};
|
|
}
|
|
|
|
// NOTE: Limited to top 5 messages for testing - remove/comment this block to collect all messages
|
|
// CHANGES:
|
|
// - Added messageCount to track messages
|
|
// - Added MAX_MESSAGES = 5 limit
|
|
// - Added MAX_MESSAGES_REACHED check
|
|
// - Modified saveMessage to count and stop at limit
|
|
let messageCount = 0;
|
|
const MAX_MESSAGES = 5;
|
|
const MAX_MESSAGES_REACHED = () => {
|
|
if (messageCount >= MAX_MESSAGES) {
|
|
console.log(`\n📝 Reached limit of ${MAX_MESSAGES} messages - stopping collection`);
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
function saveMessage(msgData) {
|
|
// DEBUG: Limited message collection for testing
|
|
messageCount++;
|
|
console.log(` (${messageCount}/${MAX_MESSAGES} messages collected)`);
|
|
|
|
if (MAX_MESSAGES_REACHED()) return; // Stop after reaching limit
|
|
|
|
const line = JSON.stringify(msgData) + '\n';
|
|
fs.appendFileSync(MESSAGES_FILE, line);
|
|
}
|
|
|
|
client.on('qr', (qr) => {
|
|
qrcode.generate(qr, { small: true });
|
|
});
|
|
|
|
client.on('ready', async () => {
|
|
console.log('✅ Connected!');
|
|
|
|
const chats = await client.getChats();
|
|
console.log(`\n📱 ${chats.length} conversations loaded:\n`);
|
|
chats.forEach((chat, i) => {
|
|
console.log(`[${i}] ${chat.name} (${chat.unreadCount} unread)`);
|
|
});
|
|
|
|
for (const chat of chats) {
|
|
const msgs = await chat.fetchMessages({ limit: 10 });
|
|
for (const msg of msgs) {
|
|
if (msg.body && msg.body.trim().length > 0) {
|
|
const msgData = formatMessage(msg, chat);
|
|
const contact = {
|
|
phone: msgData.from,
|
|
name: chat.name
|
|
};
|
|
try {
|
|
const res = await fetch('http://localhost:3000/api/contacts', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(contact)
|
|
});
|
|
if (res.ok) {
|
|
console.log(` 📇 Contact synced: ${chat.name}`);
|
|
}
|
|
} catch (e) {
|
|
// skip duplicates/errors
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
client.on('message', async (msg) => {
|
|
const chat = await msg.getChat();
|
|
const msgData = formatMessage(msg, chat);
|
|
|
|
console.log(`💬 [${chat.name}] ${msg.from}: ${msg.body.substring(0, 50)}`);
|
|
|
|
saveMessage(msgData);
|
|
|
|
// Only create contact if there's actual message content (not system notifications)
|
|
if (msg.body && msg.body.trim().length > 0) {
|
|
const contact = {
|
|
phone: msgData.from,
|
|
name: chat.name
|
|
};
|
|
|
|
try {
|
|
const res = await fetch('http://localhost:3000/api/contacts', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(contact)
|
|
});
|
|
if (res.ok) {
|
|
console.log(` 📇 Contact created in CRM`);
|
|
}
|
|
} catch (e) {
|
|
console.log(' (CRM sync skipped)');
|
|
}
|
|
}
|
|
});
|
|
|
|
client.initialize();
|