Refactor message handling and polling updates, including decryption logic for poll votes and cache management for message updates. Improved event processing flow and added handling for various message types.
O Typebot não respondia mensagens vindas de JIDs que terminam com "@lid", apenas "@s.whatsapp.net".
O comportamento ocorria porque o número era sempre extraído via:
remoteJid.split('@')[0]
Com a atualização do WhatsApp Web, algumas mensagens de mídia chegam com JID "@lid", e nesses casos o JID completo precisa ser mantido.
Ajuste realizado:
ANTES:
number: remoteJid.split('@')[0]
DEPOIS:
number: remoteJid.includes('@lid') ? remoteJid : remoteJid.split('@')[0]
Com essa condição, mensagens vindas de ambos os formatos passam a ser tratadas corretamente pelo Typebot.
Resolve o erro 'ON CONFLICT DO UPDATE command cannot affect row a second time' que ocorria ao importar histórico de contatos duplicados.
A correção remove a tentativa de atualizar o campo 'identifier' no ON CONFLICT, já que este campo faz parte da constraint de conflito e não pode ser atualizado.
Erro original:
- identifier = EXCLUDED.identifier
Correção:
- updated_at = NOW()
Isso permite que contatos duplicados sejam atualizados corretamente sem causar erro.
Updated the error handling in the saveOnWhatsappCache function to log the error message separately, improving clarity on issues encountered during item processing.
Dessa forma, quando uma nova conexão era estabelecida reutilizando o mesmo instanceName, o Baileys carregava chaves antigas e inválidas, incompatíveis com o novo conjunto de credenciais (creds) gerado na reconexão.
Essa inconsistência gerava o seguinte sintoma prático:
A instância autenticava com sucesso;
Contudo, ao tentar enviar mensagens, entrava em estado de bloqueio, exibindo o status “aguardando mensagem” indefinidamente.
- Add cleanup logic in mount() to prevent memory leaks from multiple subscriptions
- Recreate messageSubject if it was completed during logout
- Remount messageProcessor in connectToWhatsapp() to ensure subscription is active after reconnection
This fixes the issue where incoming message events stop working after logout and reconnect, while outgoing message events continue to work normally.
The root cause was that onDestroy() calls complete() on the RxJS Subject, making it permanently closed. When reconnecting, the Subject would silently ignore all new messages.
The fix ensures that:
1. Old subscriptions are properly cleaned up before creating new ones
2. If the Subject is closed, a new one is created automatically
3. The messageProcessor is remounted on every connection to ensure active subscription