Deploy: 2026-05-23 22:54:24
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const baileys = require('@whiskeysockets/baileys');
|
||||
const makeWASocket = baileys.default || baileys.makeWASocket || baileys;
|
||||
const { useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion, downloadMediaMessage, makeCacheableSignalKeyStore } = baileys;
|
||||
const { useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion, downloadMediaMessage, makeCacheableSignalKeyStore, makeInMemoryStore } = baileys;
|
||||
const pino = require('pino');
|
||||
const NodeCache = require('node-cache');
|
||||
const axios = require('axios');
|
||||
@@ -11,6 +11,8 @@ const sessions = new Map(); // Store active sockets in memory
|
||||
const retryCounters = new Map(); // Track reconnection attempts per session
|
||||
const recentMessages = new Map(); // Cache of recent messages in memory to serve getMessage callback
|
||||
const phoneToLid = new Map(); // Map phone numbers to LID JIDs for correct E2EE routing
|
||||
const sessionStores = new Map(); // Store active stores in memory
|
||||
const storeIntervals = new Map(); // Store save intervals in memory
|
||||
|
||||
// Global retry counter cache — persists across socket reconnects
|
||||
// This is CRITICAL for E2EE: tracks message retry attempts so Baileys can
|
||||
@@ -57,6 +59,28 @@ async function startSession(session_key, webhook_url) {
|
||||
const sessionFolder = path.join(SESSIONS_DIR, session_key);
|
||||
const { state, saveCreds } = await useMultiFileAuthState(sessionFolder);
|
||||
|
||||
// Initialize InMemoryStore for chat history
|
||||
const store = makeInMemoryStore({ logger: pino({ level: 'silent' }) });
|
||||
const storeFile = path.join(SESSIONS_DIR, `${session_key}_store.json`);
|
||||
if (fs.existsSync(storeFile)) {
|
||||
try {
|
||||
store.readFromFile(storeFile);
|
||||
console.log(`[Store] Loaded store from file for ${session_key}`);
|
||||
} catch (e) {
|
||||
console.error(`[Store] Failed to load store file for ${session_key}:`, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Periodically save store to file every 10 seconds
|
||||
const storeInterval = setInterval(() => {
|
||||
try {
|
||||
store.writeToFile(storeFile);
|
||||
} catch (e) {
|
||||
console.error(`[Store] Failed to write store file for ${session_key}:`, e.message);
|
||||
}
|
||||
}, 10000);
|
||||
storeIntervals.set(session_key, storeInterval);
|
||||
|
||||
// Fetch the latest WhatsApp Web version to avoid 405 rejection
|
||||
let version;
|
||||
try {
|
||||
@@ -95,6 +119,9 @@ async function startSession(session_key, webhook_url) {
|
||||
const sock = makeWASocket(socketConfig);
|
||||
console.log(`[Session] Socket created for ${session_key}`);
|
||||
|
||||
store.bind(sock.ev);
|
||||
sessionStores.set(session_key, store);
|
||||
|
||||
sessions.set(session_key, sock);
|
||||
|
||||
sock.ev.on('creds.update', saveCreds);
|
||||
@@ -299,6 +326,13 @@ async function startSession(session_key, webhook_url) {
|
||||
* Cleanup session: remove from memory and delete auth files
|
||||
*/
|
||||
async function cleanupSession(session_key) {
|
||||
const interval = storeIntervals.get(session_key);
|
||||
if (interval) {
|
||||
clearInterval(interval);
|
||||
storeIntervals.delete(session_key);
|
||||
}
|
||||
sessionStores.delete(session_key);
|
||||
|
||||
const sock = sessions.get(session_key);
|
||||
if (sock) {
|
||||
try { sock.end(); } catch (e) { } // Gracefully close socket without logout
|
||||
@@ -318,6 +352,18 @@ async function cleanupSession(session_key) {
|
||||
* Disconnect session: logout from WhatsApp and cleanup
|
||||
*/
|
||||
async function disconnectSession(session_key) {
|
||||
const interval = storeIntervals.get(session_key);
|
||||
if (interval) {
|
||||
clearInterval(interval);
|
||||
storeIntervals.delete(session_key);
|
||||
}
|
||||
sessionStores.delete(session_key);
|
||||
|
||||
const storeFile = path.join(SESSIONS_DIR, `${session_key}_store.json`);
|
||||
if (fs.existsSync(storeFile)) {
|
||||
try { fs.unlinkSync(storeFile); } catch (e) {}
|
||||
}
|
||||
|
||||
const sock = sessions.get(session_key);
|
||||
if (sock) {
|
||||
try { sock.logout(); } catch (e) { } // best effort logout
|
||||
@@ -455,6 +501,72 @@ async function checkContact(session_key, phone) {
|
||||
}
|
||||
}
|
||||
|
||||
async function exportChatHistory(session_key) {
|
||||
const store = sessionStores.get(session_key);
|
||||
if (!store) {
|
||||
throw new Error(`No store found for session ${session_key}`);
|
||||
}
|
||||
|
||||
const chats = store.chats.all();
|
||||
let outputText = `==================================================\n`;
|
||||
outputText += `سجل محادثات منصة نبيه - جلسة: ${session_key}\n`;
|
||||
outputText += `تاريخ التصدير: ${new Date().toLocaleString('ar-EG')}\n`;
|
||||
outputText += `==================================================\n\n`;
|
||||
|
||||
for (const chat of chats) {
|
||||
const jid = chat.id;
|
||||
const phone = jid.split('@')[0];
|
||||
const name = chat.name || 'عميل غير مسمى';
|
||||
|
||||
// Skip groups or broadcasts
|
||||
if (jid.endsWith('@g.us') || jid.endsWith('@broadcast')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const messages = store.messages[jid]?.all() || [];
|
||||
if (messages.length === 0) continue;
|
||||
|
||||
outputText += `--------------------------------------------------\n`;
|
||||
outputText += `المحادثة مع: ${name} (${phone})\n`;
|
||||
outputText += `--------------------------------------------------\n`;
|
||||
|
||||
for (const msg of messages) {
|
||||
const fromMe = msg.key.fromMe;
|
||||
const sender = fromMe ? 'المنصة (نبيه)' : name;
|
||||
|
||||
const body = msg.message?.conversation ||
|
||||
msg.message?.extendedTextMessage?.text ||
|
||||
msg.message?.imageMessage?.caption ||
|
||||
msg.message?.videoMessage?.caption || '';
|
||||
|
||||
let dateStr = 'تاريخ غير معروف';
|
||||
if (msg.messageTimestamp) {
|
||||
const timestamp = Number(msg.messageTimestamp) * 1000;
|
||||
dateStr = new Date(timestamp).toLocaleString('ar-EG');
|
||||
}
|
||||
|
||||
if (body) {
|
||||
outputText += `[${dateStr}] ${sender}: ${body}\n`;
|
||||
} else if (msg.message?.audioMessage) {
|
||||
outputText += `[${dateStr}] ${sender}: [رسالة صوتية]\n`;
|
||||
} else if (msg.message?.imageMessage) {
|
||||
outputText += `[${dateStr}] ${sender}: [صورة]\n`;
|
||||
} else {
|
||||
outputText += `[${dateStr}] ${sender}: [رسالة غير معروفة]\n`;
|
||||
}
|
||||
}
|
||||
outputText += `\n\n`;
|
||||
}
|
||||
|
||||
const publicDir = path.join(__dirname, '..', 'backend', 'public');
|
||||
if (!fs.existsSync(publicDir)) {
|
||||
fs.mkdirSync(publicDir, { recursive: true });
|
||||
}
|
||||
const filePath = path.join(publicDir, 'whatsapp_chats_history.txt');
|
||||
fs.writeFileSync(filePath, outputText, 'utf8');
|
||||
return filePath;
|
||||
}
|
||||
|
||||
function getActiveSessions() {
|
||||
return Array.from(sessions.keys());
|
||||
}
|
||||
@@ -464,6 +576,7 @@ module.exports = {
|
||||
disconnectSession,
|
||||
sendMessage,
|
||||
getActiveSessions,
|
||||
checkContact
|
||||
checkContact,
|
||||
exportChatHistory
|
||||
};
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ for (const p of envPaths) {
|
||||
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const { startSession, disconnectSession, sendMessage, getActiveSessions, checkContact } = require('./baileys-client');
|
||||
const { startSession, disconnectSession, sendMessage, getActiveSessions, checkContact, exportChatHistory } = require('./baileys-client');
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -99,6 +99,28 @@ app.post('/api/contacts/check', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Export chat history to file
|
||||
app.post('/api/chats/export', async (req, res) => {
|
||||
const { session_key } = req.body;
|
||||
|
||||
if (!session_key) {
|
||||
return res.status(400).json({ error: 'Missing session_key' });
|
||||
}
|
||||
|
||||
try {
|
||||
const filePath = await exportChatHistory(session_key);
|
||||
res.json({
|
||||
status: 'success',
|
||||
message: 'Chat history exported successfully',
|
||||
file_name: 'whatsapp_chats_history.txt',
|
||||
path: filePath
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(`Error exporting chats for session ${session_key}:`, err);
|
||||
res.status(500).json({ error: err.message || 'Failed to export chats' });
|
||||
}
|
||||
});
|
||||
|
||||
// Send outbound message
|
||||
app.post('/api/messages/send', async (req, res) => {
|
||||
const { session_key, phone, message, media_url, audio, mimetype, image } = req.body;
|
||||
|
||||
Reference in New Issue
Block a user