Deploy: 2026-07-30 11:05:21
This commit is contained in:
@@ -178,6 +178,7 @@ 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 resolvedJidCache = new Map(); // Map phone numbers to plain resolved JIDs (avoids re-querying on every send)
|
||||
const sessionStores = new Map(); // Store active stores in memory
|
||||
const storeIntervals = new Map(); // Store save intervals in memory
|
||||
|
||||
@@ -586,6 +587,9 @@ async function resolveJid(sock, phone) {
|
||||
console.log(`[LID] Routing reply to ${phone} via LID: ${lid}`);
|
||||
return lid;
|
||||
}
|
||||
if (resolvedJidCache.has(phone)) {
|
||||
return resolvedJidCache.get(phone);
|
||||
}
|
||||
// Query WhatsApp to resolve the correct JID/LID for this phone number.
|
||||
try {
|
||||
console.log(`[JID Resolve] Resolving JID for ${phone}...`);
|
||||
@@ -594,6 +598,13 @@ async function resolveJid(sock, phone) {
|
||||
console.log(`[JID Resolve] Successfully resolved JID for ${phone}: ${result.jid}`);
|
||||
if (result.jid.endsWith('@lid')) {
|
||||
phoneToLid.set(phone, result.jid);
|
||||
} else {
|
||||
// Cache plain JIDs too — this lookup is a network roundtrip that previously
|
||||
// ran on every single send to the same number.
|
||||
resolvedJidCache.set(phone, result.jid);
|
||||
if (resolvedJidCache.size > 5000) {
|
||||
resolvedJidCache.delete(resolvedJidCache.keys().next().value);
|
||||
}
|
||||
}
|
||||
return result.jid;
|
||||
}
|
||||
@@ -669,7 +680,7 @@ async function _rawSend(sock, jid, message, mediaUrl, audioBase64, mimetype, ima
|
||||
* Send a message using an active session — routed through ThrottleManager
|
||||
* for human-like delays, typing indicators, and anti-ban protection.
|
||||
*/
|
||||
async function sendMessage(session_key, phone, message, mediaUrl = null, audioBase64 = null, mimetype = null, imageBase64 = null) {
|
||||
async function sendMessage(session_key, phone, message, mediaUrl = null, audioBase64 = null, mimetype = null, imageBase64 = null, type = null) {
|
||||
const sock = sessions.get(session_key);
|
||||
if (!sock) {
|
||||
throw new Error(`Session ${session_key} is not active or connected`);
|
||||
@@ -677,9 +688,11 @@ async function sendMessage(session_key, phone, message, mediaUrl = null, audioBa
|
||||
|
||||
const jid = await resolveJid(sock, phone);
|
||||
|
||||
// Determine message type for throttle delay calculation
|
||||
let msgType = 'auto_reply';
|
||||
if (audioBase64) msgType = 'voice_reply';
|
||||
// Determine message type for throttle delay calculation. An explicit type from the
|
||||
// caller wins — that's how transactional sends (OTP codes, order confirmations) opt out
|
||||
// of the human-behaviour simulation.
|
||||
let msgType = type || 'auto_reply';
|
||||
if (!type && audioBase64) msgType = 'voice_reply';
|
||||
|
||||
// Build the content descriptor for typing delay calculation
|
||||
const content = { text: message || '' };
|
||||
|
||||
Reference in New Issue
Block a user