diff --git a/whatsapp-router/src/store/conversation.ts b/whatsapp-router/src/store/conversation.ts index 207b5d0..c074b4d 100644 --- a/whatsapp-router/src/store/conversation.ts +++ b/whatsapp-router/src/store/conversation.ts @@ -119,13 +119,21 @@ export async function addMessageToConversation( waMsg: WhatsAppMessage, openWaUrl: string, ): Promise { - const convo = await getConversation(chatId, openWaUrl); - const msg = toMsg(waMsg); - convo.messages.push(msg); - - // keep last 100 msgs only - if (convo.messages.length > 100) convo.messages.shift(); - - ensureParticipant(convo.participants, waMsg.sender); - return convo; -} \ No newline at end of file + console.log(`[conversationStore] Adding message to ${chatId}`); + const conv = await getConversation(chatId, openWaUrl); + const mapped = mapMessage(msg); + // avoid duplicates if multiple webhook events deliver the same message + if (!conv.messages.some((m) => m.id === mapped.id)) { + conv.messages.push(mapped); + if (conv.messages.length > 20) conv.messages.shift(); + } + const s = msg.sender; + if (s && !conv.participants.some((p) => p.id === s.id)) { + conv.participants.push({ + id: s.id, + name: s.pushname || s.name || '', + isMe: s.isMe, + }); + } + return conv; +} diff --git a/whatsapp-router/src/webhook.ts b/whatsapp-router/src/webhook.ts index 1a1e6c2..2aa5f4b 100644 --- a/whatsapp-router/src/webhook.ts +++ b/whatsapp-router/src/webhook.ts @@ -120,7 +120,8 @@ export async function registerWebhook(config: WebhookConfig, port: number) { const eventConfig = { onAck: false, onAddedToGroup: true, - onAnyMessage: false, + // use onAnyMessage to also receive messages sent by nucleo-whatsapp itself + onAnyMessage: true, onBattery: true, onBroadcast: true, onButton: true, @@ -135,7 +136,8 @@ export async function registerWebhook(config: WebhookConfig, port: number) { onIncomingCall: false, onLabel: true, onLogout: true, - onMessage: true, + // disable onMessage to avoid duplicates with onAnyMessage + onMessage: false, onMessageDeleted: true, onNewProduct: true, onOrder: true,