fix: usar remitente correcto en DMs y evitar Invalid sender ID

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
pull/1/head
borja 2 months ago
parent b75d71c86b
commit 4a305dc007

@ -145,11 +145,43 @@ export class WebhookServer {
return; return;
} }
// Determine sender depending on context (group vs DM) and ignore non-user messages
const remoteJid = data.key.remoteJid;
const participant = data.key.participant;
const fromMe = !!data.key.fromMe;
// Ignore broadcasts/status
if (remoteJid === 'status@broadcast' || (typeof remoteJid === 'string' && remoteJid.endsWith('@broadcast'))) {
if (process.env.NODE_ENV !== 'test') {
console.log(' Ignoring broadcast/status message');
}
return;
}
// Ignore our own messages
if (fromMe) {
if (process.env.NODE_ENV !== 'test') {
console.log(' Ignoring message sent by the bot (fromMe=true)');
}
return;
}
// Compute sender JID based on chat type
const senderRaw = isGroupId(remoteJid) ? participant : remoteJid;
// Normalize sender ID for consistency and validation // Normalize sender ID for consistency and validation
const normalizedSenderId = normalizeWhatsAppId(data.key.participant); const normalizedSenderId = normalizeWhatsAppId(senderRaw);
if (!normalizedSenderId) { if (!normalizedSenderId) {
if (process.env.NODE_ENV !== 'test') { if (process.env.NODE_ENV !== 'test') {
console.log('⚠️ Invalid sender ID, ignoring message'); console.debug('⚠️ Invalid sender ID, ignoring message', { remoteJid, participant, fromMe });
}
return;
}
// Avoid processing messages from the bot number
if (process.env.CHATBOT_PHONE_NUMBER && normalizedSenderId === process.env.CHATBOT_PHONE_NUMBER) {
if (process.env.NODE_ENV !== 'test') {
console.log(' Ignoring message from the bot number');
} }
return; return;
} }
@ -157,7 +189,7 @@ export class WebhookServer {
// Ensure user exists in database (swallow DB errors to keep webhook 200) // Ensure user exists in database (swallow DB errors to keep webhook 200)
let userId: string | null = null; let userId: string | null = null;
try { try {
userId = ensureUserExists(data.key.participant, WebhookServer.dbInstance); userId = ensureUserExists(senderRaw, WebhookServer.dbInstance);
} catch (e) { } catch (e) {
if (process.env.NODE_ENV !== 'test') { if (process.env.NODE_ENV !== 'test') {
console.error('⚠️ Error ensuring user exists, ignoring message:', e); console.error('⚠️ Error ensuring user exists, ignoring message:', e);

Loading…
Cancel
Save