Compare commits

...

108 Commits
2.3.6 ... main

Author SHA1 Message Date
Davidson Gomes
cd800f2976 Merge branch 'release/2.3.7'
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
2025-12-05 11:28:52 -03:00
Davidson Gomes
4f642e17a7 chore: changelog v2.3.7
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
2025-12-05 11:28:40 -03:00
Davidson Gomes
afa6d633c6 chore(changelog): update version 2.3.7 with latest features and fixes 2025-12-05 11:13:17 -03:00
Davidson Gomes
2e3e752719 style(whatsapp): fix indentation and remove unnecessary blank lines in BaileysStartupService 2025-12-05 11:03:52 -03:00
Davidson Gomes
de11e6f9ca fix(websocket): improve host validation logic in WebsocketController 2025-12-05 11:03:52 -03:00
Davidson Gomes
26e7eefe51
Merge pull request #2259 from muriloleal13/fix/baileys-message-stub-placeholder
fix(baileys): prevent message loss from WhatsApp stub placeholders
2025-12-05 11:02:34 -03:00
Davidson Gomes
b55c9fcab7
Merge pull request #2250 from gabrielmouallem/fix/respect-database-save-data-contacts
fix: respect DATABASE_SAVE_DATA_CONTACTS in contact updates
2025-12-05 11:01:46 -03:00
Davidson Gomes
86b194af5f
Merge pull request #2260 from alexandrereyes/feat/add-islatest-progress-to-messages-set
feat(events): add isLatest and progress to messages.set event
2025-12-05 11:01:21 -03:00
Davidson Gomes
3c1573c400
Merge pull request #2238 from jamesjhonatan123/feature/quote-message-n8n
Feature/quote message n8n
2025-12-05 11:00:54 -03:00
Davidson Gomes
178386594c
Merge branch 'develop' into feature/quote-message-n8n 2025-12-05 11:00:40 -03:00
Davidson Gomes
cea1fa0979
Merge pull request #2247 from msantosjader/fix/postgres-chat-constraint
fix(prisma): add unique constraint to Chat model in Postgres
2025-12-05 10:59:22 -03:00
Davidson Gomes
38be0b49d9
Merge pull request #2280 from micaelmz/feature/wildcard-for-websocket-allowed-hosts
feat: add wildcard "*" to allow all hosts to connect via websocket
2025-12-05 10:59:03 -03:00
Alexandre Martins
04ac880fcc style: fix lint formatting issues 2025-12-05 10:58:42 -03:00
Davidson Gomes
3864366e75
Merge pull request #2273 from kay0ramon/fix/minio-messagecontextinfo-upload-error
fix: handle messageContextInfo in media upload to prevent MinIO errors
2025-12-05 10:57:55 -03:00
Davidson Gomes
2756d7e61c
Merge pull request #2264 from lucascampuus/patch-1
Fix Typebot message routing for @lid JIDs
2025-12-05 10:57:31 -03:00
Davidson Gomes
bb36bfe424
Merge pull request #2249 from rodps/fix/fetch-messages-jid
fix: unify remoteJid filtering using OR with remoteJidAlt
2025-12-05 10:55:50 -03:00
Davidson Gomes
6277c5d084
Merge branch 'develop' into patch-1 2025-12-05 10:55:05 -03:00
Davidson Gomes
b1d77019f5
Merge pull request #2275 from Vitordotpy/fix/all-wrong-things-in-this-api
Fix: @lid problems, messages events and chatwoot integration errors
2025-12-05 10:51:49 -03:00
Davidson Gomes
8d5c7d875e
Merge pull request #2240 from JefersonRamos/bugfix/media-upload-failed-on-all-hosts
Bugfix/media upload failed on all hosts
2025-12-05 10:48:04 -03:00
micaelmz
abd0351f8f feat: add wildcard "*" to allow all hosts to connect via websocket 2025-12-02 18:01:19 -03:00
Vitordotpy
c7a2aa51ee fix: reorganize imports and improve message handling in BaileysStartupService 2025-11-30 19:56:03 -03:00
Vitor Manoel Santos Moura
bbf60e30b0
Refactor imports and clean up code structure 2025-11-30 18:51:34 -03:00
Vitor Manoel Santos Moura
2408384b0f
Refactor message handling and polling updates
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.
2025-11-30 00:25:17 -03:00
Vitordotpy
250ddd2e89 fix(chatwoot): improve jid normalization and type safety in chatwoot integration
Refactor  to preserve LID identifiers and update  parameter type for better type safety as per code review feedback.
2025-11-28 21:28:45 -03:00
Vitordotpy
bee309cd28 fix: streamline message handling logic and improve cache management in BaileysStartupService 2025-11-28 21:14:19 -03:00
Vitordotpy
92c2ace7bc fix: enhance remoteJid processing to handle '@lid' cases 2025-11-28 19:03:24 -03:00
Vitordotpy
faed3f4574 fix: improve error handling for existing contacts and simplify remoteJid processing 2025-11-28 16:32:06 -03:00
Vitordotpy
baff4e8f5e fix: update remoteJid handling to avoid unnecessary splitting for message number 2025-11-28 16:18:33 -03:00
Kayo Ramon Oliveira
1c3a7ab027 fix: handle messageContextInfo in media upload to prevent MinIO errors 2025-11-28 15:59:09 -03:00
Lucas Luiz Campos
338cc93cfc
Fix Typebot message routing for @lid JIDs
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.
2025-11-27 09:31:40 -03:00
Alexandre Martins
930d32df3a fix(events): guard extra spread and prevent core field override
- Use (extra ?? {}) to handle undefined extra safely
- Spread extra first to prevent overriding core fields like event, instance, data
- Applied fix to all 7 event controllers

Addresses Sourcery AI review feedback.
2025-11-26 15:48:53 -03:00
Alexandre Martins
fa6b5c28a6 feat(events): add isLatest and progress to messages.set event
- Add extra field to EmitData type for additional payload properties
- Update EventManager and sendDataWebhook to support extra parameters
- Update all event controllers (webhook, rabbitmq, sqs, websocket, pusher, kafka, nats) to include extra fields in payload
- Pass isLatest and progress from Baileys messaging-history.set to messages.set webhook

This allows consumers to know when the history sync is complete (isLatest=true) and track sync progress percentage.
2025-11-26 15:44:18 -03:00
Murilo Leal
8e7f348c12 fix(baileys): prevent message loss from WhatsApp stub placeholders
Mensagens do WhatsApp estavam sendo perdidas e não eram salvas no banco de dados, especialmente mensagens de canais/newsletters (@lid) e mensagens com criptografia complexa.

O WhatsApp/Baileys envia mensagens criptografadas em duas etapas:

1. Primeiro: Envia um stub (placeholder) com messageStubParameters: ['Message absent from node'] enquanto descriptografa a mensagem

2. Depois: Envia a mensagem real com o conteúdo descriptografado

O problema ocorria porque:

- O stub chegava primeiro e era adicionado ao cache de mensagens duplicadas

- O stub era descartado (corretamente) por não ter conteúdo (!received?.message)

- A mensagem real chegava depois, mas era ignorada como duplicata porque o ID já estava no cache

- Resultado: mensagem nunca era salva no banco de dados

Solução:

- Detectar stubs do WhatsApp através de messageStubParameters contendo 'Message absent from node'

- Não adicionar stubs ao cache de mensagens duplicadas

- Permitir que a mensagem real seja processada quando chegar

- Manter o descarte do stub para evitar salvar placeholders vazios
2025-11-26 13:29:31 -03:00
Jeferson Ramos
5c58cb7eae lint 2025-11-24 14:19:48 -03:00
Jeferson Ramos
879bee962b lint 2025-11-24 14:17:27 -03:00
Jeferson Ramos
af47b859e4 socks5 update 2025-11-24 13:59:50 -03:00
Jeferson Ramos
1c61116a3e Merge remote-tracking branch 'upstream/develop' into bugfix/media-upload-failed-on-all-hosts
# Conflicts:
#	package-lock.json
#	src/utils/makeProxyAgent.ts
2025-11-24 13:58:25 -03:00
Davidson Gomes
13f96a366b chore(dependencies): update baileys and AWS SDK packages to latest versions
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
- Upgraded baileys to version 7.0.0-rc.9 in package.json and package-lock.json.
- Updated AWS SDK packages to version 3.936.0 for improved functionality and compatibility.
- Adjusted various dependencies to ensure stability and performance enhancements.
2025-11-24 12:02:09 -03:00
Gabriel Mouallem
08a4795016 fix: respect DATABASE_SAVE_DATA_CONTACTS in contact updates
- Added missing conditional checks for `DATABASE_SAVE_DATA_CONTACTS` in `contacts.upsert` and `contacts.update` handlers.
- Fixed an issue where profile picture updates were attempting to save to the database even when disabled.
- Fixed an unawaited promise in `contacts.upsert` to ensure database operations complete correctly.
2025-11-23 23:09:42 -03:00
Gabriel Mouallem
53a94af3f7 fix: respect DATABASE_SAVE_DATA_CONTACTS in contact updates 2025-11-23 22:59:18 -03:00
Rodrigo da Silva
302e219f7f fix: unify remoteJid filtering using OR with remoteJidAlt 2025-11-23 18:46:06 -03:00
Jader Santos
1e036ba3ae fix(migration): add deduplication step before creating index 2025-11-21 22:09:15 -03:00
Jader Santos
377993e4b0 fix(prisma): add unique constraint to Chat model in Postgres
Generated migration to add unique index on instanceId and remoteJid.
2025-11-21 21:40:27 -03:00
Davidson Gomes
689f347457
Merge pull request #2241 from victoreduardo/evo/main
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
Fix: Using all IPs including x-forwarded-for when checking if the requester has access to metrics
2025-11-19 17:47:10 -03:00
Davidson Gomes
7743063439
Merge pull request #2220 from victoreduardo/evo/feature-pix
feature: handle with interactive button message for pix
2025-11-19 17:45:02 -03:00
Davidson Gomes
f5e43a3b3f chore(dependencies): update baileys and AWS SDK packages
Some checks are pending
Check Code Quality / check-lint-and-build (push) Waiting to run
Build Docker image / Build and Deploy (push) Waiting to run
Security Scan / CodeQL Analysis (javascript) (push) Waiting to run
Security Scan / Dependency Review (push) Waiting to run
- Updated baileys to version 7.0.0-rc.8 in package.json and package-lock.json.
- Downgraded AWS SDK packages to specific versions for compatibility.
- Adjusted various dependencies to maintain stability and performance.
2025-11-19 16:52:33 -03:00
Jeferson Ramos
ea88edd512 socks 2025-11-19 16:51:59 -03:00
Victor Eduardo
e6a9ed92ce Fix: Using all IPs including x-forwarded-for when checking if the requester has access to metrics 2025-11-19 16:20:54 -03:00
Victor Eduardo
8707520a3e lint 2025-11-19 16:12:16 -03:00
Jeferson Ramos
d3e3c458a0 lint 2025-11-19 14:09:07 -03:00
Jeferson Ramos
067f0999b5 lint 2025-11-19 14:07:23 -03:00
Jeferson Ramos
179af3f41c lint 2025-11-19 14:02:52 -03:00
Jeferson Ramos
31a6f2d92e ; 2025-11-19 14:00:17 -03:00
Jeferson Ramos
dc72f01625 Merge remote-tracking branch 'upstream/main' into bugfix/media-upload-failed-on-all-hosts 2025-11-19 13:34:47 -03:00
Jeferson Ramos
3b139078c3 Removendo uso do undici com proxy socks 2025-11-19 13:34:33 -03:00
Jonatas
f2c2a6a64a refactor: improve formatting and consistency in makeProxyAgent functions 2025-11-18 23:58:18 -03:00
Jonatas
e5a249109c feat: add quotedMessage to payload in sendMessageToBot on N8N 2025-11-18 23:52:36 -03:00
Davidson Gomes
73fb376602
Merge pull request #2219 from victoreduardo/evo/main
Some checks failed
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Fix: Chatwoot service fails when processing read message
2025-11-13 14:03:09 -03:00
Victor Eduardo
27633aad53 feature: handle with interactive button message for pix 2025-11-13 09:47:53 -03:00
Victor Eduardo
06543e89e5 fix: await chatwootRequest in update_last_seen method for proper asynchronous handling 2025-11-12 22:47:05 -03:00
Davidson Gomes
90640b7cee
Merge pull request #2203 from RaFaeL-Cunha/patch-1
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
fix(chatwoot): corrige erro de duplicação na importação de contatos
2025-11-11 08:45:00 -03:00
Rafael Freire Cunha
da8774caa2
fix(chatwoot): corrige erro de duplicação na importação de contatos
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.
2025-11-09 14:56:53 -04:00
Davidson Gomes
4ae3139163 chore(changelog): update to version 2.3.7 with new features and fixes
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
- Added update and delete endpoints for WhatsApp Business Meta templates.
- Fixed issues with incoming message events and authentication states after reconnection.
- Resolved unique constraint errors in OnWhatsapp cache and optimized database writes.
- Improved proxy integration for media uploads and fixed handling of base64, filename, and caption in the WhatsApp Business API.
- Enhanced chat service reliability and contact filtering capabilities.
- Made multiple integration improvements between Chatwoot and Baileys.
- Refactored code for better quality and consistency.
2025-11-07 14:45:38 -03:00
Davidson Gomes
f4043a9141 fix(whatsapp-cache): improve error logging in save function
Updated the error handling in the saveOnWhatsappCache function to log the error message separately, improving clarity on issues encountered during item processing.
2025-11-07 14:41:40 -03:00
Davidson Gomes
139ad9b3cb
Merge pull request #2186 from muriloleal13/fix/baileys-message-processor-reconnection
fix(baileys): resolve incoming message events not working after reconnection
2025-11-07 14:39:26 -03:00
Davidson Gomes
fca39a2b34
Merge pull request #2191 from JefersonRamos/bugfix/waiting-for-message
Durante o processo de logout de uma instância, as chaves associadas a…
2025-11-07 14:38:36 -03:00
Davidson Gomes
1e3a23588e
Merge pull request #2163 from ricaelchiquetti/feat/update_delete_meta_templates
Feat/update and delete meta templates
2025-11-07 14:36:14 -03:00
Davidson Gomes
27be03ea95
Merge pull request #2162 from moothz/fix/saveOnWhatsappCache-remoteJid-failed-constraint
fix: Erro ao enviar mensagem para grupos (remoteJid failed constraint)
2025-11-07 14:35:23 -03:00
Davidson Gomes
9b73252f35
Merge branch 'develop' into fix/saveOnWhatsappCache-remoteJid-failed-constraint 2025-11-07 14:33:58 -03:00
Davidson Gomes
71322cd8f6
Merge pull request #2161 from ricaelchiquetti/main
feat(whatsapp): corrigir Business (base64/filename/caption), remoteJid
2025-11-07 14:32:37 -03:00
Davidson Gomes
263854db47
Merge pull request #2160 from moothz/fix/fetchChats-cleanMessageData-errors
fix: fetchChats e chat - Painel de mensagens no Manager
2025-11-07 14:27:25 -03:00
Davidson Gomes
400b6291a2
Merge pull request #2158 from KokeroO/develop
fix: Integration Chatwoot and Baileys services
2025-11-07 14:26:32 -03:00
Jeferson Ramos
feff038446 lint ajustes 2025-11-05 16:30:32 -03:00
Jeferson Ramos
4d2a189905 lint ajustes 2025-11-05 16:29:09 -03:00
Jeferson Ramos
48625a739c Merge branch 'main' into bugfix/waiting-for-message 2025-11-05 16:27:22 -03:00
Jeferson Ramos
b6620d2bd6 responde in log 2025-11-05 16:26:55 -03:00
Jeferson Ramos
45e461e757 lint 2025-11-05 16:03:09 -03:00
Jeferson Ramos
be5760905e Durante o processo de logout de uma instância, as chaves associadas ao estado criptográfico não estavam sendo removidas corretamente do Redis.
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.
2025-11-05 15:39:21 -03:00
Murilo Leal
92626fa559 fix(baileys): resolve incoming message events not working after reconnection
- 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
2025-11-04 13:49:38 -03:00
ricael
1aaad541ad chore(dto): remove unused template edit/delete DTOs 2025-10-30 16:59:30 -03:00
ricael
3b0432dd9f refactor(utils): lint 2025-10-30 16:36:01 -03:00
ricael
a95c843e77 feat(template): add edit/delete endpoints, DTOs and validation" 2025-10-30 16:28:53 -03:00
moothz
8d1151d0a0
fix: lint
Descartei as mudanças nos arquivos que não me pertencem, dá uma folga aí, botinho
2025-10-30 16:18:33 -03:00
moothz
a1393b679c fix(OnWhatsappCache): Prevent unique constraint errors and optimize DB writes
Refactors the cache-saving logic to prevent `Unique constraint failed` errors. This issue occurs when an item's `remoteJid` is not yet included in the `jidOptions` of the existing record.

The database query now uses an `OR` condition to find a matching record by either `jidOptions` (using `contains`) or by the `remoteJid` itself in a single query.

Additionally, this commit introduces several performance optimizations:

1.  **Skip Unnecessary Updates**: The function now performs a deep comparison between the new payload and the `existingRecord`. An `update` operation is only executed if the data has actually changed, reducing unnecessary database writes.
2.  **Parallel Processing**: The sequential `for...of` loop has been replaced with `Promise.allSettled`. This allows all items in the `data` array to be processed concurrently, significantly speeding up execution for batch inputs.
3.  **Data Consistency**: The JIDs in `jidOptions` are now sorted alphabetically before being joined into a string. This ensures that the change-detection logic is accurate, regardless of the order in which JIDs were discovered.
4.  **Refactor**: Simplified JID unification logic using a `Set` and introduced a `normalizeJid` helper function for cleaner code.

TODO: Investigate the root cause of why `remoteJid` is sometimes not present in `jidOptions` upon initial discovery.
2025-10-30 16:00:44 -03:00
moothz
5cbc163716 Update channel.service.ts 2025-10-30 10:58:24 -03:00
ricael
a84faaa575 refactor(utils): improve makeProxyAgent for Undici compatibility 2025-10-30 10:55:47 -03:00
ricael
503cbfb21c Merge branch 'fix/business_api' into improv/update_baileys_version 2025-10-30 10:49:37 -03:00
KokeroO
40281871c8 fix: improve code formatting and consistency in makeProxyAgent.ts 2025-10-29 23:07:31 -03:00
KokeroO
85868b3439 chore: package 2025-10-29 22:59:18 -03:00
KokeroO
066e060b86 fix: baileys and chatwoot 2025-10-29 22:52:20 -03:00
Davidson Gomes
c555048783
Merge pull request #2155 from gomessguii/develop
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
Fix merge
2025-10-29 17:39:30 -03:00
Guilherme Gomes
2d14c8849b Merge branch 'main' into develop 2025-10-29 17:38:15 -03:00
Davidson Gomes
df20c5fc93
Merge pull request #2141 from JefersonRamos/bugfix/media-upload-failed-on-all-hosts
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
fix: "Media upload failed on all hosts" utilizando proxy
2025-10-29 17:14:23 -03:00
Jeferson Ramos
3818313161 fix: handle undefined protocol in makeProxyAgent 2025-10-27 10:40:10 -03:00
Jeferson Ramos
4a38e505f4 Esse erro acontece porque o Node.js (a partir da versão 18) usa o Undici como implementação nativa de fetch(), e o Undici não aceita mais objetos agent tradicionais (como os criados por https-proxy-agent ou socks-proxy-agent).
Ele espera objetos compatíveis com a interface moderna Dispatcher, que possuem o método dispatch().

Ou seja, o Baileys estava recebendo um tipo de agente incompatível com o novo sistema de rede do Node.

Foi criada uma nova função makeProxyAgentUndici() para gerar agentes de proxy compatíveis com o Undici, mantendo a versão antiga (makeProxyAgent()) inalterada para compatibilidade com bibliotecas como Axios.

A nova função substitui os antigos HttpsProxyAgent e SocksProxyAgent por ProxyAgent da biblioteca undici, garantindo compatibilidade total com o Baileys e com qualquer uso de fetch() moderno.
2025-10-27 10:20:34 -03:00
Davidson Gomes
d5f5b8325e
Merge pull request #2120 from FaelN1/develop
Some checks failed
Check Code Quality / check-lint-and-build (push) Has been cancelled
Build Docker image / Build and Deploy (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
fix(chat): apply where filters correctly in findContacts endpoint
2025-10-23 06:55:28 -03:00
Rafael Nicolas
1ad51a434b fix(chat): apply where filters correctly in findContacts endpoint
Anteriormente, o endpoint findContacts processava apenas o campo remoteJid da cláusula where, ignorando outros campos como id e pushName.

Alterações:

- Atualiza método fetchContacts para processar todos os campos do where (id, remoteJid, pushName)

- Adiciona campo remoteJid ao contactValidateSchema para validação adequada

- Garante isolamento multi-tenant mantendo filtro por instanceId

Esta correção permite que usuários filtrem contatos por qualquer um dos campos suportados ao invés de sempre retornar todos os contatos da instância.
2025-10-22 23:52:48 -03:00
William Dumes
f0c6300599
Merge pull request #4 from ricaelchiquetti/fix/evolution_baileys_7
fix: ajustar a manipulação do remoteJid na mensagem
2025-09-17 17:46:03 -03:00
ricael
24c339343f fix: ajustar a manipulação do remoteJid na mensagem do serviço WhatsApp para garantir a utilização da chave alternativa quando disponível 2025-09-17 17:41:07 -03:00
Roberto Oswaldo Klann
ddbaf2335a
Merge pull request #3 from ricaelchiquetti/fix/evolution_baileys_7
Fix/evolution baileys 7
2025-09-17 13:58:20 -03:00
ricael
20c8a2ff0e Merge branch 'fix/business_api' into fix/evolution_baileys_7 2025-09-17 08:18:20 -03:00
ricael
e623269a18 fix: ajustar o tratamento da chave de mídia na mensagem do serviço WhatsApp para usar Uint8Array 2025-09-17 08:13:33 -03:00
ricael
0363fa979d improv 2025-09-16 13:35:06 -03:00
William Dumes
b640329cf8
Merge branch 'EvolutionAPI:main' into fix/business_api 2025-09-11 11:33:01 -03:00
ricaelchiquetti
f72b1f7717
Merge branch 'EvolutionAPI:main' into fix/business_api 2025-09-11 11:10:41 -03:00
William Dumes
ed4c8868a0
Merge pull request #2 from ricaelchiquetti/fix/business_api
Fix/business api
2025-09-11 08:16:27 -03:00
ricael
06081f6502 fix: adicionado tratamento para evitar envio de filename e caption em mensagens de vídeo e áudio no BusinessStartupService 2025-09-10 16:59:04 -03:00
William Dumes
8e51ae63ae fix: corrigido para que o envio de base64 nas mensagens do business api seja enviada somente qnd a config esta ativa 2025-08-07 15:21:30 -03:00
47 changed files with 2991 additions and 1781 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -59,7 +59,7 @@ body:
value: |
- OS: [e.g. Ubuntu 20.04, Windows 10, macOS 12.0]
- Node.js version: [e.g. 18.17.0]
- Evolution API version: [e.g. 2.3.6]
- Evolution API version: [e.g. 2.3.7]
- Database: [e.g. PostgreSQL 14, MySQL 8.0]
- Connection type: [e.g. Baileys, WhatsApp Business API]
validations:

View File

@ -1,3 +1,154 @@
# 2.3.7 (2025-12-05)
### Features
* **WhatsApp Business Meta Templates**: Add update and delete endpoints for Meta templates
- New endpoints to edit and delete WhatsApp Business templates
- Added DTOs and validation schemas for template management
- Enhanced template lifecycle management capabilities
* **Events API**: Add isLatest and progress to messages.set event
- Allows consumers to know when history sync is complete (isLatest=true)
- Track sync progress percentage through webhooks
- Added extra field to EmitData type for additional payload properties
- Updated all event controllers (webhook, rabbitmq, sqs, websocket, pusher, kafka, nats)
* **N8N Integration**: Add quotedMessage to payload in sendMessageToBot
- Support for quoted messages in N8N chatbot integration
- Enhanced message context information
* **WebSocket**: Add wildcard "*" to allow all hosts to connect via websocket
- More flexible host configuration for WebSocket connections
- Improved host validation logic in WebsocketController
* **Pix Support**: Handle interactive button message for pix
- Support for interactive Pix button messages
- Enhanced payment flow integration
### Fixed
* **Baileys Message Processor**: Fix incoming message events not working after reconnection
- Added 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
- Fixed issue where onDestroy() calls complete() on RxJS Subject, making it permanently closed
- Ensures old subscriptions are properly cleaned up before creating new ones
* **Baileys Authentication**: Resolve "waiting for message" state after reconnection
- Fixed Redis keys not being properly removed during instance logout
- Prevented loading of old/invalid cryptographic keys on reconnection
- Fixed blocking state where instances authenticate but cannot send messages
- Ensures new credentials (creds) are properly used after reconnection
* **OnWhatsapp Cache**: Prevent unique constraint errors and optimize database writes
- Fixed `Unique constraint failed on the fields: (remoteJid)` error when sending to groups
- Refactored query to use OR condition finding by jidOptions or remoteJid
- Added deep comparison to skip unnecessary database updates
- Replaced sequential processing with Promise.allSettled for parallel execution
- Sorted JIDs alphabetically in jidOptions for accurate change detection
- Added normalizeJid helper function for cleaner code
* **Proxy Integration**: Fix "Media upload failed on all hosts" error when using proxy
- Created makeProxyAgentUndici() for Undici-compatible proxy agents
- Fixed compatibility with Node.js 18+ native fetch() implementation
- Replaced traditional HttpsProxyAgent/SocksProxyAgent with Undici ProxyAgent
- Maintained legacy makeProxyAgent() for Axios compatibility
- Fixed protocol handling in makeProxyAgent to prevent undefined errors
* **WhatsApp Business API**: Fix base64, filename and caption handling
- Corrected base64 media conversion in Business API
- Fixed filename handling for document messages
- Improved caption processing for media messages
- Enhanced remoteJid validation and processing
* **Chat Service**: Fix fetchChats and message panel errors
- Fixed cleanMessageData errors in Manager message panel
- Improved chat fetching reliability
- Enhanced message data sanitization
* **Contact Filtering**: Apply where filters correctly in findContacts endpoint
- Fixed endpoint to process all where clause fields (id, remoteJid, pushName)
- Previously only processed remoteJid field, ignoring other filters
- Added remoteJid field to contactValidateSchema for proper validation
- Maintained multi-tenant isolation with instanceId filtering
- Allows filtering contacts by any supported field instead of returning all contacts
* **Chatwoot and Baileys Integration**: Multiple integration improvements
- Enhanced code formatting and consistency
- Fixed integration issues between Chatwoot and Baileys services
- Improved message handling and delivery
* **Baileys Message Loss**: Prevent message loss from WhatsApp stub placeholders
- Fixed messages being lost and not saved to database, especially for channels/newsletters (@lid)
- Detects WhatsApp stubs through messageStubParameters containing 'Message absent from node'
- Prevents adding stubs to duplicate message cache
- Allows real message to be processed when it arrives after decryption
- Maintains stub discard to avoid saving empty placeholders
* **Database Contacts**: Respect DATABASE_SAVE_DATA_CONTACTS in contact updates
- Added missing conditional checks for DATABASE_SAVE_DATA_CONTACTS configuration
- Fixed profile picture updates attempting to save when database save is disabled
- Fixed unawaited promise in contacts.upsert handler
* **Prisma/PostgreSQL**: Add unique constraint to Chat model
- Generated migration to add unique index on instanceId and remoteJid
- Added deduplication step before creating index to prevent constraint violations
- Prevents chat duplication in database
* **MinIO Upload**: Handle messageContextInfo in media upload to prevent MinIO errors
- Prevents errors when uploading media with messageContextInfo metadata
- Improved error handling for media storage operations
* **Typebot**: Fix message routing for @lid JIDs
- Typebot now responds to messages from JIDs ending with @lid
- Maintains complete JID for @lid instead of extracting only number
- Fixed condition: `remoteJid.includes('@lid') ? remoteJid : remoteJid.split('@')[0]`
- Handles both @s.whatsapp.net and @lid message formats
* **Message Filtering**: Unify remoteJid filtering using OR with remoteJidAlt
- Improved message filtering with alternative JID support
- Better handling of messages with different JID formats
* **@lid Integration**: Multiple fixes for @lid problems, message events and chatwoot errors
- Reorganized imports and improved message handling in BaileysStartupService
- Enhanced remoteJid processing to handle @lid cases
- Improved jid normalization and type safety in Chatwoot integration
- Streamlined message handling logic and cache management
- Refactored message handling and polling updates with decryption logic for poll votes
- Improved event processing flow for various message types
* **Chatwoot Contacts**: Fix contact duplication error on import
- Resolved 'ON CONFLICT DO UPDATE command cannot affect row a second time' error
- Removed attempt to update identifier field in conflict (part of constraint)
- Changed to update only updated_at field: `updated_at = NOW()`
- Allows duplicate contacts to be updated correctly without errors
* **Chatwoot Service**: Fix async handling in update_last_seen method
- Added missing await for chatwootRequest in read message processing
- Prevents service failure when processing read messages
* **Metrics Access**: Fix IP validation including x-forwarded-for
- Uses all IPs including x-forwarded-for header when checking metrics access
- Improved security and access control for metrics endpoint
### Dependencies
* **Baileys**: Updated to version 7.0.0-rc.9
- Latest release candidate with multiple improvements and bug fixes
* **AWS SDK**: Updated packages to version 3.936.0
- Enhanced functionality and compatibility
- Performance improvements
### Code Quality & Refactoring
* **Template Management**: Remove unused template edit/delete DTOs after refactoring
* **Proxy Utilities**: Improve makeProxyAgent for Undici compatibility
* **Code Formatting**: Enhance code formatting and consistency across services
* **BaileysStartupService**: Fix indentation and remove unnecessary blank lines
* **Event Controllers**: Guard extra spread and prevent core field override in all event controllers
* **Import Organization**: Reorganize imports for better code structure and maintainability
# 2.3.6 (2025-10-21)
### Features

View File

@ -2,7 +2,7 @@ version: "3.7"
services:
evolution_v2:
image: evoapicloud/evolution-api:v2.3.6
image: evoapicloud/evolution-api:v2.3.7
volumes:
- evolution_instances:/evolution/instances
networks:

3061
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "2.3.6",
"version": "2.3.7",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/main.js",
"type": "commonjs",
@ -77,7 +77,7 @@
"amqplib": "^0.10.5",
"audio-decode": "^2.2.3",
"axios": "^1.7.9",
"baileys": "7.0.0-rc.6",
"baileys": "7.0.0-rc.9",
"class-validator": "^0.14.1",
"compression": "^1.7.5",
"cors": "^2.8.5",
@ -90,12 +90,14 @@
"fluent-ffmpeg": "^2.1.3",
"form-data": "^4.0.1",
"https-proxy-agent": "^7.0.6",
"fetch-socks": "^1.3.2",
"i18next": "^23.7.19",
"jimp": "^1.6.0",
"json-schema": "^0.4.0",
"jsonschema": "^1.4.1",
"jsonwebtoken": "^9.0.2",
"kafkajs": "^2.2.4",
"libphonenumber-js": "^1.12.25",
"link-preview-js": "^3.0.13",
"long": "^5.2.3",
"mediainfo.js": "^0.3.4",
@ -121,6 +123,7 @@
"socks-proxy-agent": "^8.0.5",
"swagger-ui-express": "^5.0.1",
"tsup": "^8.3.5",
"undici": "^7.16.0",
"uuid": "^13.0.0"
},
"devDependencies": {

View File

@ -0,0 +1,16 @@
-- 1. Cleanup: Remove duplicate chats, keeping the most recently updated one
DELETE FROM "Chat"
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY "instanceId", "remoteJid"
ORDER BY "updatedAt" DESC
) as row_num
FROM "Chat"
) t
WHERE t.row_num > 1
);
-- 2. Create the unique index (Constraint)
CREATE UNIQUE INDEX "Chat_instanceId_remoteJid_key" ON "Chat"("instanceId", "remoteJid");

View File

@ -132,6 +132,7 @@ model Chat {
instanceId String
unreadMessages Int @default(0)
@@unique([instanceId, remoteJid])
@@index([instanceId])
@@index([remoteJid])
}

View File

@ -92,6 +92,15 @@ export class InstanceController {
instanceId: instanceId,
});
const instanceDto: InstanceDto = {
instanceName: instance.instanceName,
instanceId: instance.instanceId,
connectionStatus:
typeof instance.connectionStatus === 'string'
? instance.connectionStatus
: instance.connectionStatus?.state || 'unknown',
};
if (instanceData.proxyHost && instanceData.proxyPort && instanceData.proxyProtocol) {
const testProxy = await this.proxyService.testProxy({
host: instanceData.proxyHost,
@ -103,8 +112,7 @@ export class InstanceController {
if (!testProxy) {
throw new BadRequestException('Invalid proxy');
}
await this.proxyService.createProxy(instance, {
await this.proxyService.createProxy(instanceDto, {
enabled: true,
host: instanceData.proxyHost,
port: instanceData.proxyPort,
@ -125,7 +133,7 @@ export class InstanceController {
wavoipToken: instanceData.wavoipToken || '',
};
await this.settingsService.create(instance, settings);
await this.settingsService.create(instanceDto, settings);
let webhookWaBusiness = null,
accessTokenWaBusiness = '';
@ -155,7 +163,10 @@ export class InstanceController {
integration: instanceData.integration,
webhookWaBusiness,
accessTokenWaBusiness,
status: instance.connectionStatus.state,
status:
typeof instance.connectionStatus === 'string'
? instance.connectionStatus
: instance.connectionStatus?.state || 'unknown',
},
hash,
webhook: {
@ -217,7 +228,7 @@ export class InstanceController {
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
this.chatwootService.create(instanceDto, {
enabled: true,
accountId: instanceData.chatwootAccountId,
token: instanceData.chatwootToken,
@ -246,7 +257,10 @@ export class InstanceController {
integration: instanceData.integration,
webhookWaBusiness,
accessTokenWaBusiness,
status: instance.connectionStatus.state,
status:
typeof instance.connectionStatus === 'string'
? instance.connectionStatus
: instance.connectionStatus?.state || 'unknown',
},
hash,
webhook: {
@ -338,20 +352,38 @@ export class InstanceController {
throw new BadRequestException('The "' + instanceName + '" instance does not exist');
}
if (state == 'close') {
if (state === 'close') {
throw new BadRequestException('The "' + instanceName + '" instance is not connected');
} else if (state == 'open') {
}
this.logger.info(`Restarting instance: ${instanceName}`);
if (typeof instance.restart === 'function') {
await instance.restart();
// Wait a bit for the reconnection to be established
await new Promise((r) => setTimeout(r, 2000));
return {
instance: {
instanceName: instanceName,
status: instance.connectionStatus?.state || 'connecting',
},
};
}
// Fallback for Baileys (uses different mechanism)
if (state === 'open' || state === 'connecting') {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instance.clearCacheChatwoot();
this.logger.info('restarting instance' + instanceName);
instance.client?.ws?.close();
instance.client?.end(new Error('restart'));
return await this.connectToWhatsapp({ instanceName });
} else if (state == 'connecting') {
instance.client?.ws?.close();
instance.client?.end(new Error('restart'));
return await this.connectToWhatsapp({ instanceName });
}
return {
instance: {
instanceName: instanceName,
status: state,
},
};
} catch (error) {
this.logger.error(error);
return { error: true, message: error.toString() };
@ -409,7 +441,7 @@ export class InstanceController {
}
try {
this.waMonitor.waInstances[instanceName]?.logoutInstance();
await this.waMonitor.waInstances[instanceName]?.logoutInstance();
return { status: 'SUCCESS', error: false, response: { message: 'Instance logged out' } };
} catch (error) {

View File

@ -12,4 +12,15 @@ export class TemplateController {
public async findTemplate(instance: InstanceDto) {
return this.templateService.find(instance);
}
public async editTemplate(
instance: InstanceDto,
data: { templateId: string; category?: string; components?: any; allowCategoryChange?: boolean; ttl?: number },
) {
return this.templateService.edit(instance, data);
}
public async deleteTemplate(instance: InstanceDto, data: { name: string; hsmId?: string }) {
return this.templateService.delete(instance, data);
}
}

View File

@ -12,6 +12,7 @@ export class InstanceDto extends IntegrationDto {
token?: string;
status?: string;
ownerJid?: string;
connectionStatus?: string;
profileName?: string;
profilePicUrl?: string;
// settings

View File

@ -6,3 +6,16 @@ export class TemplateDto {
components: any;
webhookUrl?: string;
}
export class TemplateEditDto {
templateId: string;
category?: 'AUTHENTICATION' | 'MARKETING' | 'UTILITY';
allowCategoryChange?: boolean;
ttl?: number;
components?: any;
}
export class TemplateDeleteDto {
name: string;
hsmId?: string;
}

View File

@ -516,7 +516,9 @@ export class BusinessStartupService extends ChannelStartupService {
const mediaUrl = await s3Service.getObjectUrl(fullName);
messageRaw.message.mediaUrl = mediaUrl;
if (this.localWebhook.enabled && this.localWebhook.webhookBase64) {
messageRaw.message.base64 = buffer.data.toString('base64');
}
// Processar OpenAI speech-to-text para áudio após o mediaUrl estar disponível
if (this.configService.get<Openai>('OPENAI').ENABLED && mediaType === 'audio') {
@ -554,11 +556,19 @@ export class BusinessStartupService extends ChannelStartupService {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
} else {
if (this.localWebhook.enabled && this.localWebhook.webhookBase64) {
const buffer = await this.downloadMediaMessage(received?.messages[0]);
messageRaw.message.base64 = buffer.toString('base64');
}
// Processar OpenAI speech-to-text para áudio mesmo sem S3
if (this.configService.get<Openai>('OPENAI').ENABLED && message.type === 'audio') {
let openAiBase64 = messageRaw.message.base64;
if (!openAiBase64) {
const buffer = await this.downloadMediaMessage(received?.messages[0]);
openAiBase64 = buffer.toString('base64');
}
const openAiDefaultSettings = await this.prismaRepository.openaiSetting.findFirst({
where: {
instanceId: this.instanceId,
@ -574,7 +584,7 @@ export class BusinessStartupService extends ChannelStartupService {
openAiDefaultSettings.OpenaiCreds,
{
message: {
base64: messageRaw.message.base64,
base64: openAiBase64,
...messageRaw,
},
},
@ -1016,6 +1026,7 @@ export class BusinessStartupService extends ChannelStartupService {
[message['mediaType']]: {
[message['type']]: message['id'],
...(message['mediaType'] !== 'audio' &&
message['mediaType'] !== 'video' &&
message['fileName'] &&
!isImage && { filename: message['fileName'] }),
...(message['mediaType'] !== 'audio' && message['caption'] && { caption: message['caption'] }),
@ -1606,9 +1617,14 @@ export class BusinessStartupService extends ChannelStartupService {
const messageType = msg.messageType.includes('Message') ? msg.messageType : msg.messageType + 'Message';
const mediaMessage = msg.message[messageType];
if (!msg.message?.base64) {
const buffer = await this.downloadMediaMessage({ type: messageType, ...msg.message });
msg.message.base64 = buffer.toString('base64');
}
return {
mediaType: msg.messageType,
fileName: mediaMessage?.fileName,
fileName: mediaMessage?.fileName || mediaMessage?.filename,
caption: mediaMessage?.caption,
size: {
fileLength: mediaMessage?.fileLength,

View File

@ -19,6 +19,22 @@ export class BaileysMessageProcessor {
}>();
mount({ onMessageReceive }: MountProps) {
// Se já existe subscription, fazer cleanup primeiro
if (this.subscription && !this.subscription.closed) {
this.subscription.unsubscribe();
}
// Se o Subject foi completado, recriar
if (this.messageSubject.closed) {
this.processorLogs.warn('MessageSubject was closed, recreating...');
this.messageSubject = new Subject<{
messages: WAMessage[];
type: MessageUpsertType;
requestId?: string;
settings: any;
}>();
}
this.subscription = this.messageSubject
.pipe(
tap(({ messages }) => {

View File

@ -82,7 +82,7 @@ import { createId as cuid } from '@paralleldrive/cuid2';
import { Instance, Message } from '@prisma/client';
import { createJid } from '@utils/createJid';
import { fetchLatestWaWebVersion } from '@utils/fetchLatestWaWebVersion';
import { makeProxyAgent } from '@utils/makeProxyAgent';
import { makeProxyAgent, makeProxyAgentUndici } from '@utils/makeProxyAgent';
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
import { status } from '@utils/renderStatus';
import { sendTelemetry } from '@utils/sendTelemetry';
@ -99,6 +99,7 @@ import makeWASocket, {
Chat,
ConnectionState,
Contact,
decryptPollVote,
delay,
DisconnectReason,
downloadContentFromMessage,
@ -113,6 +114,7 @@ import makeWASocket, {
isJidGroup,
isJidNewsletter,
isPnUser,
jidNormalizedUser,
makeCacheableSignalKeyStore,
MessageUpsertType,
MessageUserReceiptUpdate,
@ -133,6 +135,7 @@ import { Label } from 'baileys/lib/Types/Label';
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
import { spawn } from 'child_process';
import { isArray, isBase64, isURL } from 'class-validator';
import { createHash } from 'crypto';
import EventEmitter2 from 'eventemitter2';
import ffmpeg from 'fluent-ffmpeg';
import FormData from 'form-data';
@ -247,6 +250,7 @@ export class BaileysStartupService extends ChannelStartupService {
private readonly userDevicesCache: CacheStore = new NodeCache({ stdTTL: 300000, useClones: false });
private endSession = false;
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
private eventProcessingQueue: Promise<void> = Promise.resolve();
// Cache TTL constants (in seconds)
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
@ -266,6 +270,28 @@ export class BaileysStartupService extends ChannelStartupService {
this.client?.ws?.close();
const db = this.configService.get<Database>('DATABASE');
const cache = this.configService.get<CacheConf>('CACHE');
const provider = this.configService.get<ProviderSession>('PROVIDER');
if (provider?.ENABLED) {
const authState = await this.authStateProvider.authStateProvider(this.instance.id);
await authState.removeCreds();
}
if (cache?.REDIS.ENABLED && cache?.REDIS.SAVE_INSTANCES) {
const authState = await useMultiFileAuthStateRedisDb(this.instance.id, this.cache);
await authState.removeCreds();
}
if (db.SAVE_DATA.INSTANCE) {
const authState = await useMultiFileAuthStatePrisma(this.instance.id, this.cache);
await authState.removeCreds();
}
const sessionExists = await this.prismaRepository.session.findFirst({ where: { sessionId: this.instanceId } });
if (sessionExists) {
await this.prismaRepository.session.delete({ where: { sessionId: this.instanceId } });
@ -569,15 +595,6 @@ export class BaileysStartupService extends ChannelStartupService {
const version = baileysVersion.version;
const log = `Baileys version: ${version.join('.')}`;
// if (session.VERSION) {
// version = session.VERSION.split('.');
// log = `Baileys version env: ${version}`;
// } else {
// const baileysVersion = await fetchLatestWaWebVersion({});
// version = baileysVersion.version;
// log = `Baileys version: ${version}`;
// }
this.logger.info(log);
this.logger.info(`Group Ignore: ${this.localSettings.groupsIgnore}`);
@ -594,7 +611,7 @@ export class BaileysStartupService extends ChannelStartupService {
const proxyUrls = text.split('\r\n');
const rand = Math.floor(Math.random() * Math.floor(proxyUrls.length));
const proxyUrl = 'http://' + proxyUrls[rand];
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgent(proxyUrl) };
options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgentUndici(proxyUrl) };
} catch {
this.localProxy.enabled = false;
}
@ -607,7 +624,7 @@ export class BaileysStartupService extends ChannelStartupService {
username: this.localProxy.username,
password: this.localProxy.password,
}),
fetchAgent: makeProxyAgent({
fetchAgent: makeProxyAgentUndici({
host: this.localProxy.host,
port: this.localProxy.port,
protocol: this.localProxy.protocol,
@ -710,6 +727,11 @@ export class BaileysStartupService extends ChannelStartupService {
this.loadWebhook();
this.loadProxy();
// Remontar o messageProcessor para garantir que está funcionando após reconexão
this.messageProcessor.mount({
onMessageReceive: this.messageHandle['messages.upsert'].bind(this),
});
return await this.createClient(number);
} catch (error) {
this.logger.error(error);
@ -839,10 +861,12 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CONTACTS_UPDATE, updatedContacts);
await Promise.all(
updatedContacts.map(async (contact) => {
const update = this.prismaRepository.contact.updateMany({
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS) {
await this.prismaRepository.contact.updateMany({
where: { remoteJid: contact.remoteJid, instanceId: this.instanceId },
data: { profilePicUrl: contact.profilePicUrl },
});
}
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
const instance = { instanceName: this.instance.name, instanceId: this.instance.id };
@ -861,8 +885,6 @@ export class BaileysStartupService extends ChannelStartupService {
avatar_url: contact.profilePicUrl,
});
}
return update;
}),
);
}
@ -886,6 +908,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactsRaw);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.CONTACTS) {
const updateTransactions = contactsRaw.map((contact) =>
this.prismaRepository.contact.upsert({
where: { remoteJid_instanceId: { remoteJid: contact.remoteJid, instanceId: contact.instanceId } },
@ -894,6 +917,7 @@ export class BaileysStartupService extends ChannelStartupService {
}),
);
await this.prismaRepository.$transaction(updateTransactions);
}
//const usersContacts = contactsRaw.filter((c) => c.remoteJid.includes('@s.whatsapp'));
},
@ -1022,7 +1046,10 @@ export class BaileysStartupService extends ChannelStartupService {
messagesRaw.push(this.prepareMessage(m));
}
this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw]);
this.sendDataWebhook(Events.MESSAGES_SET, [...messagesRaw], true, undefined, {
isLatest,
progress,
});
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true });
@ -1068,6 +1095,7 @@ export class BaileysStartupService extends ChannelStartupService {
'Invalid PreKey ID',
'No session record',
'No session found to decrypt message',
'Message absent from node',
].some((err) => param?.includes?.(err)),
)
) {
@ -1103,6 +1131,11 @@ export class BaileysStartupService extends ChannelStartupService {
);
await this.sendDataWebhook(Events.MESSAGES_EDITED, editedMessage);
if (received.key?.id && editedMessage.key?.id) {
await this.baileysCache.set(`protocol_${received.key.id}`, editedMessage.key.id, 60 * 60 * 24);
}
const oldMessage = await this.getMessage(editedMessage.key, true);
if ((oldMessage as any)?.id) {
const editedMessageTimestamp = Long.isLong(received?.messageTimestamp)
@ -1130,22 +1163,7 @@ export class BaileysStartupService extends ChannelStartupService {
}
}
const messageKey = `${this.instance.id}_${received.key.id}`;
const cached = await this.baileysCache.get(messageKey);
if (cached && !editedMessage && !requestId) {
this.logger.info(`Message duplicated ignored: ${received.key.id}`);
continue;
}
await this.baileysCache.set(messageKey, true, this.MESSAGE_CACHE_TTL_SECONDS);
if (
(type !== 'notify' && type !== 'append') ||
editedMessage ||
received.message?.pollUpdateMessage ||
!received?.message
) {
if ((type !== 'notify' && type !== 'append') || editedMessage || !received?.message) {
continue;
}
@ -1185,6 +1203,107 @@ export class BaileysStartupService extends ChannelStartupService {
const messageRaw = this.prepareMessage(received);
if (messageRaw.messageType === 'pollUpdateMessage') {
const pollCreationKey = messageRaw.message.pollUpdateMessage.pollCreationMessageKey;
const pollMessage = (await this.getMessage(pollCreationKey, true)) as proto.IWebMessageInfo;
const pollMessageSecret = (await this.getMessage(pollCreationKey)) as any;
if (pollMessage) {
const pollOptions =
(pollMessage.message as any).pollCreationMessage?.options ||
(pollMessage.message as any).pollCreationMessageV3?.options ||
[];
const pollVote = messageRaw.message.pollUpdateMessage.vote;
const voterJid = received.key.fromMe
? this.instance.wuid
: received.key.participant || received.key.remoteJid;
let pollEncKey = pollMessageSecret?.messageContextInfo?.messageSecret;
let successfulVoterJid = voterJid;
if (typeof pollEncKey === 'string') {
pollEncKey = Buffer.from(pollEncKey, 'base64');
} else if (pollEncKey?.type === 'Buffer' && Array.isArray(pollEncKey.data)) {
pollEncKey = Buffer.from(pollEncKey.data);
}
if (Buffer.isBuffer(pollEncKey) && pollEncKey.length === 44) {
pollEncKey = Buffer.from(pollEncKey.toString('utf8'), 'base64');
}
if (pollVote.encPayload && pollEncKey) {
const creatorCandidates = [
this.instance.wuid,
this.client.user?.lid,
pollMessage.key.participant,
(pollMessage.key as any).participantAlt,
pollMessage.key.remoteJid,
];
const key = received.key as any;
const voterCandidates = [
this.instance.wuid,
this.client.user?.lid,
key.participant,
key.participantAlt,
key.remoteJidAlt,
key.remoteJid,
];
const uniqueCreators = [
...new Set(creatorCandidates.filter(Boolean).map((id) => jidNormalizedUser(id))),
];
const uniqueVoters = [...new Set(voterCandidates.filter(Boolean).map((id) => jidNormalizedUser(id)))];
let decryptedVote;
for (const creator of uniqueCreators) {
for (const voter of uniqueVoters) {
try {
decryptedVote = decryptPollVote(pollVote, {
pollCreatorJid: creator,
pollMsgId: pollMessage.key.id,
pollEncKey,
voterJid: voter,
} as any);
if (decryptedVote) {
successfulVoterJid = voter;
break;
}
} catch {
// Continue trying
}
}
if (decryptedVote) break;
}
if (decryptedVote) {
Object.assign(pollVote, decryptedVote);
}
}
const selectedOptions = pollVote?.selectedOptions || [];
const selectedOptionNames = pollOptions
.filter((option) => {
const hash = createHash('sha256').update(option.optionName).digest();
return selectedOptions.some((selected) => Buffer.compare(selected, hash) === 0);
})
.map((option) => option.optionName);
messageRaw.message.pollUpdateMessage.vote.selectedOptions = selectedOptionNames;
const pollUpdates = pollOptions.map((option) => ({
name: option.optionName,
voters: selectedOptionNames.includes(option.optionName) ? [successfulVoterJid] : [],
}));
messageRaw.pollUpdates = pollUpdates;
}
}
const isMedia =
received?.message?.imageMessage ||
received?.message?.videoMessage ||
@ -1234,7 +1353,9 @@ export class BaileysStartupService extends ChannelStartupService {
}
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
const msg = await this.prismaRepository.message.create({ data: messageRaw });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { pollUpdates, ...messageData } = messageRaw;
const msg = await this.prismaRepository.message.create({ data: messageData });
const { remoteJid } = received.key;
const timestamp = msg.messageTimestamp;
@ -1282,6 +1403,11 @@ export class BaileysStartupService extends ChannelStartupService {
} else {
const media = await this.getBase64FromMediaMessage({ message }, true);
if (!media) {
this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO');
return;
}
const { buffer, mediaType, fileName, size } = media;
const mimetype = mimeTypes.lookup(fileName).toString();
const fullName = join(
@ -1349,6 +1475,10 @@ export class BaileysStartupService extends ChannelStartupService {
this.logger.verbose(messageRaw);
sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
}
console.log(messageRaw);
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
@ -1435,18 +1565,26 @@ export class BaileysStartupService extends ChannelStartupService {
continue;
}
if (update.message !== null && update.status === undefined) continue;
const updateKey = `${this.instance.id}_${key.id}_${update.status}`;
const cached = await this.baileysCache.get(updateKey);
if (cached) {
this.logger.info(`Message duplicated ignored [avoid deadlock]: ${updateKey}`);
const secondsSinceEpoch = Math.floor(Date.now() / 1000);
console.log('CACHE:', { cached, updateKey, messageTimestamp: update.messageTimestamp, secondsSinceEpoch });
if (
(update.messageTimestamp && update.messageTimestamp === cached) ||
(!update.messageTimestamp && secondsSinceEpoch === cached)
) {
this.logger.info(`Update Message duplicated ignored [avoid deadlock]: ${updateKey}`);
continue;
}
await this.baileysCache.set(updateKey, true, 30 * 60);
if (update.messageTimestamp) {
await this.baileysCache.set(updateKey, update.messageTimestamp, 30 * 60);
} else {
await this.baileysCache.set(updateKey, secondsSinceEpoch, 30 * 60);
}
if (status[update.status] === 'READ' && key.fromMe) {
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
@ -1477,19 +1615,32 @@ export class BaileysStartupService extends ChannelStartupService {
remoteJid: key?.remoteJid,
fromMe: key.fromMe,
participant: key?.participant,
status: status[update.status] ?? 'DELETED',
status: status[update.status] ?? 'SERVER_ACK',
pollUpdates,
instanceId: this.instanceId,
};
if (update.message) {
message.message = update.message;
}
let findMessage: any;
const configDatabaseData = this.configService.get<Database>('DATABASE').SAVE_DATA;
if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
// Use raw SQL to avoid JSON path issues
const protocolMapKey = `protocol_${key.id}`;
const originalMessageId = (await this.baileysCache.get(protocolMapKey)) as string;
if (originalMessageId) {
message.keyId = originalMessageId;
}
const searchId = originalMessageId || key.id;
const messages = (await this.prismaRepository.$queryRaw`
SELECT * FROM "Message"
WHERE "instanceId" = ${this.instanceId}
AND "key"->>'id' = ${key.id}
AND "key"->>'id' = ${searchId}
LIMIT 1
`) as any[];
findMessage = messages[0] || null;
@ -1502,7 +1653,7 @@ export class BaileysStartupService extends ChannelStartupService {
}
if (update.message === null && update.status === undefined) {
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
this.sendDataWebhook(Events.MESSAGES_DELETE, { ...key, status: 'DELETED' });
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
await this.prismaRepository.messageUpdate.create({ data: message });
@ -1550,8 +1701,11 @@ export class BaileysStartupService extends ChannelStartupService {
this.sendDataWebhook(Events.MESSAGES_UPDATE, message);
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
await this.prismaRepository.messageUpdate.create({ data: message });
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { message: _msg, ...messageData } = message;
await this.prismaRepository.messageUpdate.create({ data: messageData });
}
const existingChat = await this.prismaRepository.chat.findFirst({
where: { instanceId: this.instanceId, remoteJid: message.remoteJid },
@ -1602,9 +1756,9 @@ export class BaileysStartupService extends ChannelStartupService {
// This enables LID to phoneNumber conversion without breaking existing webhook consumers
// Helper to normalize participantId as phone number
const normalizePhoneNumber = (id: string): string => {
const normalizePhoneNumber = (id: string | null | undefined): string => {
// Remove @lid, @s.whatsapp.net suffixes and extract just the number part
return id.split('@')[0];
return String(id || '').split('@')[0];
};
try {
@ -1720,6 +1874,8 @@ export class BaileysStartupService extends ChannelStartupService {
private eventHandler() {
this.client.ev.process(async (events) => {
this.eventProcessingQueue = this.eventProcessingQueue.then(async () => {
try {
if (!this.endSession) {
const database = this.configService.get<Database>('DATABASE');
const settings = await this.findSettings();
@ -1753,19 +1909,19 @@ export class BaileysStartupService extends ChannelStartupService {
if (events['messaging-history.set']) {
const payload = events['messaging-history.set'];
this.messageHandle['messaging-history.set'](payload);
await this.messageHandle['messaging-history.set'](payload);
}
if (events['messages.upsert']) {
const payload = events['messages.upsert'];
this.messageProcessor.processMessage(payload, settings);
// this.messageHandle['messages.upsert'](payload, settings);
// this.messageProcessor.processMessage(payload, settings);
await this.messageHandle['messages.upsert'](payload, settings);
}
if (events['messages.update']) {
const payload = events['messages.update'];
this.messageHandle['messages.update'](payload, settings);
await this.messageHandle['messages.update'](payload, settings);
}
if (events['message-receipt.update']) {
@ -1849,6 +2005,10 @@ export class BaileysStartupService extends ChannelStartupService {
return;
}
}
} catch (error) {
this.logger.error(error);
}
});
});
}
@ -2314,6 +2474,11 @@ export class BaileysStartupService extends ChannelStartupService {
} else {
const media = await this.getBase64FromMediaMessage({ message }, true);
if (!media) {
this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO');
return;
}
const { buffer, mediaType, fileName, size } = media;
const mimetype = mimeTypes.lookup(fileName).toString();
@ -3687,7 +3852,8 @@ export class BaileysStartupService extends ChannelStartupService {
}
if ('messageContextInfo' in msg.message && Object.keys(msg.message).length === 1) {
throw 'The message is messageContextInfo';
this.logger.verbose('Message contains only messageContextInfo, skipping media processing');
return null;
}
let mediaMessage: any;
@ -4870,7 +5036,6 @@ export class BaileysStartupService extends ChannelStartupService {
AND: [
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
keyFilters?.participant ? { key: { path: ['participant'], equals: keyFilters?.participant } } : {},
{
OR: [
@ -4900,7 +5065,6 @@ export class BaileysStartupService extends ChannelStartupService {
AND: [
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {},
keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {},
keyFilters?.participant ? { key: { path: ['participant'], equals: keyFilters?.participant } } : {},
{
OR: [

View File

@ -211,7 +211,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
try {
if (mediaType === 'audio') {
await instance.audioWhatsapp({
number: remoteJid.split('@')[0],
number: remoteJid.includes('@lid') ? remoteJid : remoteJid.split('@')[0],
delay: (settings as any)?.delayMessage || 1000,
audio: url,
caption: altText,
@ -219,7 +219,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
} else {
await instance.mediaMessage(
{
number: remoteJid.split('@')[0],
number: remoteJid.includes('@lid') ? remoteJid : remoteJid.split('@')[0],
delay: (settings as any)?.delayMessage || 1000,
mediatype: mediaType,
media: url,
@ -290,7 +290,7 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
setTimeout(async () => {
await instance.textMessage(
{
number: remoteJid.split('@')[0],
number: remoteJid.includes('@lid') ? remoteJid : remoteJid.split('@')[0],
delay: settings?.delayMessage || 1000,
text: message,
linkPreview,

View File

@ -1,10 +1,6 @@
import { InstanceDto } from '@api/dto/instance.dto';
import { ChatwootDto } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto';
import { ChatwootService } from '@api/integrations/chatbot/chatwoot/services/chatwoot.service';
import { PrismaRepository } from '@api/repository/repository.service';
import { waMonitor } from '@api/server.module';
import { CacheService } from '@api/services/cache.service';
import { CacheEngine } from '@cache/cacheengine';
import { Chatwoot, ConfigService, HttpServer } from '@config/env.config';
import { BadRequestException } from '@exceptions';
import { isURL } from 'class-validator';
@ -13,7 +9,6 @@ export class ChatwootController {
constructor(
private readonly chatwootService: ChatwootService,
private readonly configService: ConfigService,
private readonly prismaRepository: PrismaRepository,
) {}
public async createChatwoot(instance: InstanceDto, data: ChatwootDto) {
@ -84,9 +79,6 @@ export class ChatwootController {
public async receiveWebhook(instance: InstanceDto, data: any) {
if (!this.configService.get<Chatwoot>('CHATWOOT').ENABLED) throw new BadRequestException('Chatwoot is disabled');
const chatwootCache = new CacheService(new CacheEngine(this.configService, ChatwootService.name).getEngine());
const chatwootService = new ChatwootService(waMonitor, this.configService, this.prismaRepository, chatwootCache);
return chatwootService.receiveWebhook(instance, data);
return this.chatwootService.receiveWebhook(instance, data);
}
}

View File

@ -27,6 +27,7 @@ import { WAMessageContent, WAMessageKey } from 'baileys';
import dayjs from 'dayjs';
import FormData from 'form-data';
import { Jimp, JimpMime } from 'jimp';
import { parsePhoneNumberFromString } from 'libphonenumber-js';
import Long from 'long';
import mimeTypes from 'mime-types';
import path from 'path';
@ -345,6 +346,16 @@ export class ChatwootService {
return contact;
} catch (error) {
if ((error.status === 422 || error.response?.status === 422) && jid) {
this.logger.warn(`Contact with identifier ${jid} creation failed (422). Checking if it already exists...`);
const existingContact = await this.findContactByIdentifier(instance, jid);
if (existingContact) {
const contactId = existingContact.id;
await this.addLabelToContact(this.provider.nameInbox, contactId);
return existingContact;
}
}
this.logger.error('Error creating contact');
console.log(error);
return null;
@ -414,6 +425,55 @@ export class ChatwootService {
}
}
public async findContactByIdentifier(instance: InstanceDto, identifier: string) {
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn('client not found');
return null;
}
// Direct search by query (q) - most common way to search by identifier/email/phone
const contact = (await (client as any).get('contacts/search', {
params: {
q: identifier,
sort: 'name',
},
})) as any;
if (contact && contact.data && contact.data.payload && contact.data.payload.length > 0) {
return contact.data.payload[0];
}
// Fallback for older API versions or different response structures
if (contact && contact.payload && contact.payload.length > 0) {
return contact.payload[0];
}
// Try search by attribute
const contactByAttr = (await (client as any).post('contacts/filter', {
payload: [
{
attribute_key: 'identifier',
filter_operator: 'equal_to',
values: [identifier],
query_operator: null,
},
],
})) as any;
if (contactByAttr && contactByAttr.payload && contactByAttr.payload.length > 0) {
return contactByAttr.payload[0];
}
// Check inside data property if using axios interceptors wrapper
if (contactByAttr && contactByAttr.data && contactByAttr.data.payload && contactByAttr.data.payload.length > 0) {
return contactByAttr.data.payload[0];
}
return null;
}
public async findContact(instance: InstanceDto, phoneNumber: string) {
const client = await this.clientCw(instance);
@ -589,7 +649,7 @@ export class ChatwootService {
`Identifier needs update: (contact.identifier: ${contact.identifier}, phoneNumber: ${phoneNumber}, body.key.remoteJidAlt: ${remoteJid}`,
);
const updateContact = await this.updateContact(instance, contact.id, {
identifier: remoteJid,
identifier: phoneNumber,
phone_number: `+${phoneNumber.split('@')[0]}`,
});
@ -611,13 +671,15 @@ export class ChatwootService {
if (await this.cache.has(cacheKey)) {
const conversationId = (await this.cache.get(cacheKey)) as number;
this.logger.verbose(`Found conversation to: ${phoneNumber}, conversation ID: ${conversationId}`);
let conversationExists: conversation | boolean;
let conversationExists: any;
try {
conversationExists = await client.conversations.get({
accountId: this.provider.accountId,
conversationId: conversationId,
});
this.logger.verbose(`Conversation exists: ${JSON.stringify(conversationExists)}`);
this.logger.verbose(
`Conversation exists: ID: ${conversationExists.id} - Name: ${conversationExists.meta.sender.name} - Identifier: ${conversationExists.meta.sender.identifier}`,
);
} catch (error) {
this.logger.error(`Error getting conversation: ${error}`);
conversationExists = false;
@ -669,7 +731,7 @@ export class ChatwootService {
if (isGroup) {
this.logger.verbose(`Processing group conversation`);
const group = await this.waMonitor.waInstances[instance.instanceName].client.groupMetadata(chatId);
this.logger.verbose(`Group metadata: ${JSON.stringify(group)}`);
this.logger.verbose(`Group metadata: JID:${group.JID} - Subject:${group?.subject || group?.Name}`);
const participantJid = isLid && !body.key.fromMe ? body.key.participantAlt : body.key.participant;
nameContact = `${group.subject} (GROUP)`;
@ -680,9 +742,11 @@ export class ChatwootService {
this.logger.verbose(`Participant profile picture URL: ${JSON.stringify(picture_url)}`);
const findParticipant = await this.findContact(instance, participantJid.split('@')[0]);
this.logger.verbose(`Found participant: ${JSON.stringify(findParticipant)}`);
if (findParticipant) {
this.logger.verbose(
`Found participant: ID:${findParticipant.id} - Name: ${findParticipant.name} - identifier: ${findParticipant.identifier}`,
);
if (!findParticipant.name || findParticipant.name === chatId) {
await this.updateContact(instance, findParticipant.id, {
name: body.pushName,
@ -692,7 +756,7 @@ export class ChatwootService {
} else {
await this.createContact(
instance,
participantJid.split('@')[0],
participantJid.split('@')[0].split(':')[0],
filterInbox.id,
false,
body.pushName,
@ -709,20 +773,13 @@ export class ChatwootService {
let contact = await this.findContact(instance, chatId);
if (contact) {
this.logger.verbose(`Found contact: ${JSON.stringify(contact)}`);
this.logger.verbose(`Found contact: ID:${contact.id} - Name:${contact.name}`);
if (!body.key.fromMe) {
const waProfilePictureFile =
picture_url?.profilePictureUrl?.split('#')[0].split('?')[0].split('/').pop() || '';
const chatwootProfilePictureFile = contact?.thumbnail?.split('#')[0].split('?')[0].split('/').pop() || '';
const pictureNeedsUpdate = waProfilePictureFile !== chatwootProfilePictureFile;
const nameNeedsUpdate =
!contact.name ||
contact.name === chatId ||
(`+${chatId}`.startsWith('+55')
? this.getNumbers(`+${chatId}`).some(
(v) => contact.name === v || contact.name === v.substring(3) || contact.name === v.substring(1),
)
: false);
const nameNeedsUpdate = !contact.name || contact.name === chatId;
this.logger.verbose(`Picture needs update: ${pictureNeedsUpdate}`);
this.logger.verbose(`Name needs update: ${nameNeedsUpdate}`);
if (pictureNeedsUpdate || nameNeedsUpdate) {
@ -741,7 +798,7 @@ export class ChatwootService {
isGroup,
nameContact,
picture_url.profilePictureUrl || null,
remoteJid,
phoneNumber,
);
}
@ -757,7 +814,6 @@ export class ChatwootService {
accountId: this.provider.accountId,
id: contactId,
})) as any;
this.logger.verbose(`Contact conversations: ${JSON.stringify(contactConversations)}`);
if (!contactConversations || !contactConversations.payload) {
this.logger.error(`No conversations found or payload is undefined`);
@ -769,7 +825,9 @@ export class ChatwootService {
);
if (inboxConversation) {
if (this.provider.reopenConversation) {
this.logger.verbose(`Found conversation in reopenConversation mode: ${JSON.stringify(inboxConversation)}`);
this.logger.verbose(
`Found conversation in reopenConversation mode: ID: ${inboxConversation.id} - Name: ${inboxConversation.meta.sender.name} - Identifier: ${inboxConversation.meta.sender.identifier}`,
);
if (inboxConversation && this.provider.conversationPending && inboxConversation.status !== 'open') {
await client.conversations.toggleStatus({
accountId: this.provider.accountId,
@ -789,7 +847,7 @@ export class ChatwootService {
if (inboxConversation) {
this.logger.verbose(`Returning existing conversation ID: ${inboxConversation.id}`);
this.cache.set(cacheKey, inboxConversation.id, 8 * 3600);
this.cache.set(cacheKey, inboxConversation.id, 1800);
return inboxConversation.id;
}
}
@ -803,14 +861,6 @@ export class ChatwootService {
data['status'] = 'pending';
}
/*
Triple check after lock
Utilizei uma nova verificação para evitar que outra thread execute entre o terminio do while e o set lock
*/
if (await this.cache.has(cacheKey)) {
return (await this.cache.get(cacheKey)) as number;
}
const conversation = await client.conversations.create({
accountId: this.provider.accountId,
data,
@ -822,7 +872,7 @@ export class ChatwootService {
}
this.logger.verbose(`New conversation created of ${remoteJid} with ID: ${conversation.id}`);
this.cache.set(cacheKey, conversation.id, 8 * 3600);
this.cache.set(cacheKey, conversation.id, 1800);
return conversation.id;
} finally {
await this.cache.delete(lockKey);
@ -1392,10 +1442,7 @@ export class ChatwootService {
}
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
if (
body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:' &&
body?.conversation?.messages[0]?.id === body?.id
) {
if (body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:') {
return { message: 'bot' };
}
@ -1586,7 +1633,11 @@ export class ChatwootService {
this.logger.verbose(`Update result: ${result} rows affected`);
if (this.isImportHistoryAvailable()) {
chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id);
try {
await chatwootImport.updateMessageSourceID(chatwootMessageIds.messageId, key.id);
} catch (error) {
this.logger.error(`Error updating Chatwoot message source ID: ${error}`);
}
}
}
@ -1666,6 +1717,10 @@ export class ChatwootService {
return result;
}
private isInteractiveButtonMessage(messageType: string, message: any) {
return messageType === 'interactiveMessage' && message.interactiveMessage?.nativeFlowMessage?.buttons?.length > 0;
}
private getAdsMessage(msg: any) {
interface AdsMessage {
title: string;
@ -1984,8 +2039,9 @@ export class ChatwootService {
const adsMessage = this.getAdsMessage(body);
const reactionMessage = this.getReactionMessage(body.message);
const isInteractiveButtonMessage = this.isInteractiveButtonMessage(body.messageType, body.message);
if (!bodyMessage && !isMedia && !reactionMessage) {
if (!bodyMessage && !isMedia && !reactionMessage && !isInteractiveButtonMessage) {
this.logger.warn('no body message found');
return;
}
@ -2031,18 +2087,10 @@ export class ChatwootService {
if (body.key.remoteJid.includes('@g.us')) {
const participantName = body.pushName;
const rawPhoneNumber =
body.key.addressingMode === 'lid' && !body.key.fromMe
? body.key.participantAlt.split('@')[0]
: body.key.participant.split('@')[0];
const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/);
let formattedPhoneNumber: string;
if (phoneMatch) {
formattedPhoneNumber = `+${phoneMatch[1]} (${phoneMatch[2]}) ${phoneMatch[3]}-${phoneMatch[4]}`;
} else {
formattedPhoneNumber = `+${rawPhoneNumber}`;
}
body.key.addressingMode === 'lid' && !body.key.fromMe && body.key.participantAlt
? body.key.participantAlt.split('@')[0].split(':')[0]
: body.key.participant.split('@')[0].split(':')[0];
const formattedPhoneNumber = parsePhoneNumberFromString(`+${rawPhoneNumber}`).formatInternational();
let content: string;
@ -2118,6 +2166,50 @@ export class ChatwootService {
return;
}
if (isInteractiveButtonMessage) {
const buttons = body.message.interactiveMessage.nativeFlowMessage.buttons;
this.logger.info('is Interactive Button Message: ' + JSON.stringify(buttons));
for (const button of buttons) {
const buttonParams = JSON.parse(button.buttonParamsJson);
const paymentSettings = buttonParams.payment_settings;
if (button.name === 'payment_info' && paymentSettings[0].type === 'pix_static_code') {
const pixSettings = paymentSettings[0].pix_static_code;
const pixKeyType = (() => {
switch (pixSettings.key_type) {
case 'EVP':
return 'Chave Aleatória';
case 'EMAIL':
return 'E-mail';
case 'PHONE':
return 'Telefone';
default:
return pixSettings.key_type;
}
})();
const pixKey = pixSettings.key_type === 'PHONE' ? pixSettings.key.replace('+55', '') : pixSettings.key;
const content = `*${pixSettings.merchant_name}*\nChave PIX: ${pixKey} (${pixKeyType})`;
const send = await this.createMessage(
instance,
getConversation,
content,
messageType,
false,
[],
body,
'WAID:' + body.key.id,
quotedMsg,
);
if (!send) this.logger.warn('message not sent');
} else {
this.logger.warn('Interactive Button Message not mapped');
}
}
return;
}
const isAdsMessage = (adsMessage && adsMessage.title) || adsMessage.body || adsMessage.thumbnailUrl;
if (isAdsMessage) {
const imgBuffer = await axios.get(adsMessage.thumbnailUrl, { responseType: 'arraybuffer' });
@ -2177,18 +2269,10 @@ export class ChatwootService {
if (body.key.remoteJid.includes('@g.us')) {
const participantName = body.pushName;
const rawPhoneNumber =
body.key.addressingMode === 'lid' && !body.key.fromMe
? body.key.participantAlt.split('@')[0]
: body.key.participant.split('@')[0];
const phoneMatch = rawPhoneNumber.match(/^(\d{2})(\d{2})(\d{4})(\d{4})$/);
let formattedPhoneNumber: string;
if (phoneMatch) {
formattedPhoneNumber = `+${phoneMatch[1]} (${phoneMatch[2]}) ${phoneMatch[3]}-${phoneMatch[4]}`;
} else {
formattedPhoneNumber = `+${rawPhoneNumber}`;
}
body.key.addressingMode === 'lid' && !body.key.fromMe && body.key.participantAlt
? body.key.participantAlt.split('@')[0].split(':')[0]
: body.key.participant.split('@')[0].split(':')[0];
const formattedPhoneNumber = parsePhoneNumberFromString(`+${rawPhoneNumber}`).formatInternational();
let content: string;
@ -2270,8 +2354,21 @@ export class ChatwootService {
}
if (event === 'messages.edit' || event === 'send.message.update') {
const editedMessageContent =
body?.editedMessage?.conversation || body?.editedMessage?.extendedTextMessage?.text;
const editedMessageContentRaw =
body?.editedMessage?.conversation ??
body?.editedMessage?.extendedTextMessage?.text ??
body?.editedMessage?.imageMessage?.caption ??
body?.editedMessage?.videoMessage?.caption ??
body?.editedMessage?.documentMessage?.caption ??
(typeof body?.text === 'string' ? body.text : undefined);
const editedMessageContent = (editedMessageContentRaw ?? '').trim();
if (!editedMessageContent) {
this.logger.info('[CW.EDIT] Conteúdo vazio — ignorando (DELETE tratará se for revoke).');
return;
}
const message = await this.getMessageByKeyId(instance, body?.key?.id);
if (!message) {
@ -2338,7 +2435,7 @@ export class ChatwootService {
const url =
`/public/api/v1/inboxes/${inbox.inbox_identifier}/contacts/${sourceId}` +
`/conversations/${conversationId}/update_last_seen`;
chatwootRequest(this.getClientCwConfig(), {
await chatwootRequest(this.getClientCwConfig(), {
method: 'POST',
url: url,
});
@ -2430,7 +2527,13 @@ export class ChatwootService {
}
}
public getNumberFromRemoteJid(remoteJid: string) {
public normalizeJidIdentifier(remoteJid: string) {
if (!remoteJid) {
return '';
}
if (remoteJid.includes('@lid')) {
return remoteJid;
}
return remoteJid.replace(/:\d+/, '').split('@')[0];
}

View File

@ -137,7 +137,7 @@ class ChatwootImport {
DO UPDATE SET
name = EXCLUDED.name,
phone_number = EXCLUDED.phone_number,
identifier = EXCLUDED.identifier`;
updated_at = NOW()`;
totalContactsImported += (await pgClient.query(sqlInsert, bindInsert))?.rowCount ?? 0;

View File

@ -51,6 +51,7 @@ export class N8nService extends BaseChatbotService<N8n, N8nSetting> {
pushName: pushName,
keyId: msg?.key?.id,
fromMe: msg?.key?.fromMe,
quotedMessage: msg?.contextInfo?.quotedMessage,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: instance.token,

View File

@ -327,7 +327,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
if (message.type === 'image') {
await instance.mediaMessage(
{
number: session.remoteJid.split('@')[0],
number: session.remoteJid,
delay: settings?.delayMessage || 1000,
mediatype: 'image',
media: message.content.url,
@ -342,7 +342,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
if (message.type === 'video') {
await instance.mediaMessage(
{
number: session.remoteJid.split('@')[0],
number: session.remoteJid,
delay: settings?.delayMessage || 1000,
mediatype: 'video',
media: message.content.url,
@ -357,7 +357,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
if (message.type === 'audio') {
await instance.audioWhatsapp(
{
number: session.remoteJid.split('@')[0],
number: session.remoteJid,
delay: settings?.delayMessage || 1000,
encoding: true,
audio: message.content.url,
@ -441,7 +441,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
*/
private async processListMessage(instance: any, formattedText: string, remoteJid: string) {
const listJson = {
number: remoteJid.split('@')[0],
number: remoteJid,
title: '',
description: '',
buttonText: '',
@ -490,7 +490,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
*/
private async processButtonMessage(instance: any, formattedText: string, remoteJid: string) {
const buttonJson = {
number: remoteJid.split('@')[0],
number: remoteJid,
thumbnailUrl: undefined,
title: '',
description: '',

View File

@ -14,12 +14,24 @@ export type EmitData = {
apiKey?: string;
local?: boolean;
integration?: string[];
extra?: Record<string, any>;
};
export interface EventControllerInterface {
set(instanceName: string, data: any): Promise<any>;
get(instanceName: string): Promise<any>;
emit({ instanceName, origin, event, data, serverUrl, dateTime, sender, apiKey, local }: EmitData): Promise<void>;
emit({
instanceName,
origin,
event,
data,
serverUrl,
dateTime,
sender,
apiKey,
local,
extra,
}: EmitData): Promise<void>;
}
export class EventController {

View File

@ -123,6 +123,7 @@ export class EventManager {
apiKey?: string;
local?: boolean;
integration?: string[];
extra?: Record<string, any>;
}): Promise<void> {
await this.websocket.emit(eventData);
await this.rabbitmq.emit(eventData);

View File

@ -262,6 +262,7 @@ export class KafkaController extends EventController implements EventControllerI
sender,
apiKey,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('kafka')) {
return;
@ -284,6 +285,7 @@ export class KafkaController extends EventController implements EventControllerI
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
const message = {
...(extra ?? {}),
event,
instance: instanceName,
data,

View File

@ -47,6 +47,7 @@ export class NatsController extends EventController implements EventControllerIn
sender,
apiKey,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('nats')) {
return;
@ -65,6 +66,7 @@ export class NatsController extends EventController implements EventControllerIn
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
const message = {
...(extra ?? {}),
event,
instance: instanceName,
data,

View File

@ -121,6 +121,7 @@ export class PusherController extends EventController implements EventController
apiKey,
local,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('pusher')) {
return;
@ -133,6 +134,7 @@ export class PusherController extends EventController implements EventController
const enabledLog = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
const eventName = event.replace(/_/g, '.').toLowerCase();
const pusherData = {
...(extra ?? {}),
event,
instance: instanceName,
data,

View File

@ -209,6 +209,7 @@ export class RabbitmqController extends EventController implements EventControll
sender,
apiKey,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('rabbitmq')) {
return;
@ -233,6 +234,7 @@ export class RabbitmqController extends EventController implements EventControll
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBHOOKS');
const message = {
...(extra ?? {}),
event,
instance: instanceName,
data,

View File

@ -93,6 +93,7 @@ export class SqsController extends EventController implements EventControllerInt
sender,
apiKey,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('sqs')) {
return;
@ -128,6 +129,7 @@ export class SqsController extends EventController implements EventControllerInt
const sqsUrl = `https://sqs.${sqsConfig.REGION}.amazonaws.com/${sqsConfig.ACCOUNT_ID}/${queueName}`;
const message = {
...(extra ?? {}),
event,
instance: instanceName,
dataType: 'json',

View File

@ -65,6 +65,7 @@ export class WebhookController extends EventController implements EventControlle
apiKey,
local,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('webhook')) {
return;
@ -90,6 +91,7 @@ export class WebhookController extends EventController implements EventControlle
const regex = /^(https?:\/\/)/;
const webhookData = {
...(extra ?? {}),
event,
instance: instanceName,
data,

View File

@ -33,7 +33,10 @@ export class WebsocketController extends EventController implements EventControl
const { remoteAddress } = req.socket;
const websocketConfig = configService.get<Websocket>('WEBSOCKET');
const allowedHosts = websocketConfig.ALLOWED_HOSTS || '127.0.0.1,::1,::ffff:127.0.0.1';
const isAllowedHost = allowedHosts
const allowAllHosts = allowedHosts.trim() === '*';
const isAllowedHost =
allowAllHosts ||
allowedHosts
.split(',')
.map((h) => h.trim())
.includes(remoteAddress);
@ -115,6 +118,7 @@ export class WebsocketController extends EventController implements EventControl
sender,
apiKey,
integration,
extra,
}: EmitData): Promise<void> {
if (integration && !integration.includes('websocket')) {
return;
@ -127,6 +131,7 @@ export class WebsocketController extends EventController implements EventControl
const configEv = event.replace(/[.-]/gm, '_').toUpperCase();
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBSOCKET');
const message = {
...(extra ?? {}),
event,
instance: instanceName,
data,

View File

@ -48,9 +48,14 @@ const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const metricsIPWhitelist = (req: Request, res: Response, next: NextFunction) => {
const metricsConfig = configService.get('METRICS');
const allowedIPs = metricsConfig.ALLOWED_IPS?.split(',').map((ip) => ip.trim()) || ['127.0.0.1'];
const clientIP = req.ip || req.connection.remoteAddress || req.socket.remoteAddress;
const clientIPs = [
req.ip,
req.connection.remoteAddress,
req.socket.remoteAddress,
req.headers['x-forwarded-for'],
].filter((ip) => ip !== undefined);
if (!allowedIPs.includes(clientIP)) {
if (allowedIPs.filter((ip) => clientIPs.includes(ip)) === 0) {
return res.status(403).send('Forbidden: IP not allowed');
}

View File

@ -1,9 +1,11 @@
import { RouterBroker } from '@api/abstract/abstract.router';
import { InstanceDto } from '@api/dto/instance.dto';
import { TemplateDto } from '@api/dto/template.dto';
import { TemplateDeleteDto, TemplateDto, TemplateEditDto } from '@api/dto/template.dto';
import { templateController } from '@api/server.module';
import { ConfigService } from '@config/env.config';
import { createMetaErrorResponse } from '@utils/errorResponse';
import { templateDeleteSchema } from '@validate/templateDelete.schema';
import { templateEditSchema } from '@validate/templateEdit.schema';
import { instanceSchema, templateSchema } from '@validate/validate.schema';
import { RequestHandler, Router } from 'express';
@ -35,6 +37,38 @@ export class TemplateRouter extends RouterBroker {
res.status(errorResponse.status).json(errorResponse);
}
})
.post(this.routerPath('edit'), ...guards, async (req, res) => {
try {
const response = await this.dataValidate<TemplateEditDto>({
request: req,
schema: templateEditSchema,
ClassRef: TemplateEditDto,
execute: (instance, data) => templateController.editTemplate(instance, data),
});
res.status(HttpStatus.OK).json(response);
} catch (error) {
console.error('Template edit error:', error);
const errorResponse = createMetaErrorResponse(error, 'template_edit');
res.status(errorResponse.status).json(errorResponse);
}
})
.delete(this.routerPath('delete'), ...guards, async (req, res) => {
try {
const response = await this.dataValidate<TemplateDeleteDto>({
request: req,
schema: templateDeleteSchema,
ClassRef: TemplateDeleteDto,
execute: (instance, data) => templateController.deleteTemplate(instance, data),
});
res.status(HttpStatus.OK).json(response);
} catch (error) {
console.error('Template delete error:', error);
const errorResponse = createMetaErrorResponse(error, 'template_delete');
res.status(errorResponse.status).json(errorResponse);
}
})
.get(this.routerPath('find'), ...guards, async (req, res) => {
try {
const response = await this.dataValidate<InstanceDto>({

View File

@ -82,7 +82,7 @@ const proxyService = new ProxyService(waMonitor);
export const proxyController = new ProxyController(proxyService, waMonitor);
const chatwootService = new ChatwootService(waMonitor, configService, prismaRepository, chatwootCache);
export const chatwootController = new ChatwootController(chatwootService, configService, prismaRepository);
export const chatwootController = new ChatwootController(chatwootService, configService);
const settingsService = new SettingsService(waMonitor);
export const settingsController = new SettingsController(settingsService);

View File

@ -60,6 +60,7 @@ export class ChannelStartupService {
this.instance.number = instance.number;
this.instance.token = instance.token;
this.instance.businessId = instance.businessId;
this.instance.ownerJid = instance.ownerJid;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
this.chatwootService.eventWhatsapp(
@ -431,7 +432,13 @@ export class ChannelStartupService {
return data;
}
public async sendDataWebhook<T extends object = any>(event: Events, data: T, local = true, integration?: string[]) {
public async sendDataWebhook<T extends object = any>(
event: Events,
data: T,
local = true,
integration?: string[],
extra?: Record<string, any>,
) {
const serverUrl = this.configService.get<HttpServer>('SERVER').URL;
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
@ -452,6 +459,7 @@ export class ChannelStartupService {
apiKey: expose && instanceApikey ? instanceApikey : null,
local,
integration,
extra,
});
}
@ -490,20 +498,23 @@ export class ChannelStartupService {
}
public async fetchContacts(query: Query<Contact>) {
const remoteJid = query?.where?.remoteJid
? query?.where?.remoteJid.includes('@')
? query.where?.remoteJid
: createJid(query.where?.remoteJid)
: null;
const where = {
const where: any = {
instanceId: this.instanceId,
};
if (remoteJid) {
if (query?.where?.remoteJid) {
const remoteJid = query.where.remoteJid.includes('@') ? query.where.remoteJid : createJid(query.where.remoteJid);
where['remoteJid'] = remoteJid;
}
if (query?.where?.id) {
where['id'] = query.where.id;
}
if (query?.where?.pushName) {
where['pushName'] = query.where.pushName;
}
const contactFindManyArgs: Prisma.ContactFindManyArgs = {
where,
};
@ -532,14 +543,12 @@ export class ChannelStartupService {
public cleanMessageData(message: any) {
if (!message) return message;
const cleanedMessage = { ...message };
const mediaUrl = cleanedMessage.message.mediaUrl;
if (cleanedMessage.message) {
const { mediaUrl } = cleanedMessage.message;
delete cleanedMessage.message.base64;
if (cleanedMessage.message) {
// Limpa imageMessage
if (cleanedMessage.message.imageMessage) {
cleanedMessage.message.imageMessage = {
@ -581,9 +590,9 @@ export class ChannelStartupService {
name: cleanedMessage.message.documentWithCaptionMessage.name,
};
}
}
if (mediaUrl) cleanedMessage.message.mediaUrl = mediaUrl;
}
return cleanedMessage;
}

View File

@ -38,14 +38,22 @@ export class WAMonitoringService {
private readonly logger = new Logger('WAMonitoringService');
public readonly waInstances: Record<string, any> = {};
private readonly delInstanceTimeouts: Record<string, NodeJS.Timeout> = {};
private readonly providerSession: ProviderSession;
public delInstanceTime(instance: string) {
const time = this.configService.get<DelInstance>('DEL_INSTANCE');
if (typeof time === 'number' && time > 0) {
setTimeout(
// Clear previous timeout if exists
if (this.delInstanceTimeouts[instance]) {
clearTimeout(this.delInstanceTimeouts[instance]);
}
// Set new timeout and store reference
this.delInstanceTimeouts[instance] = setTimeout(
async () => {
try {
if (this.waInstances[instance]?.connectionStatus?.state !== 'open') {
if (this.waInstances[instance]?.connectionStatus?.state === 'connecting') {
if ((await this.waInstances[instance].integration) === Integration.WHATSAPP_BAILEYS) {
@ -58,12 +66,23 @@ export class WAMonitoringService {
this.eventEmitter.emit('remove.instance', instance, 'inner');
}
}
} finally {
// Clean up timeout reference
delete this.delInstanceTimeouts[instance];
}
},
1000 * 60 * time,
);
}
}
public clearDelInstanceTime(instance: string) {
if (this.delInstanceTimeouts[instance]) {
clearTimeout(this.delInstanceTimeouts[instance]);
delete this.delInstanceTimeouts[instance];
}
}
public async instanceInfo(instanceNames?: string[]): Promise<any> {
if (instanceNames && instanceNames.length > 0) {
const inexistentInstances = instanceNames ? instanceNames.filter((instance) => !this.waInstances[instance]) : [];
@ -271,9 +290,19 @@ export class WAMonitoringService {
token: instanceData.token,
number: instanceData.number,
businessId: instanceData.businessId,
ownerJid: instanceData.ownerJid,
});
if (instanceData.connectionStatus === 'open' || instanceData.connectionStatus === 'connecting') {
this.logger.info(
`Auto-connecting instance "${instanceData.instanceName}" (status: ${instanceData.connectionStatus})`,
);
await instance.connectToWhatsapp();
} else {
this.logger.info(
`Skipping auto-connect for instance "${instanceData.instanceName}" (status: ${instanceData.connectionStatus || 'close'})`,
);
}
this.waInstances[instanceData.instanceName] = instance;
}
@ -299,6 +328,7 @@ export class WAMonitoringService {
token: instanceData.token,
number: instanceData.number,
businessId: instanceData.businessId,
connectionStatus: instanceData.connectionStatus as any, // Pass connection status
};
this.setInstance(instance);
@ -327,6 +357,8 @@ export class WAMonitoringService {
token: instance.token,
number: instance.number,
businessId: instance.businessId,
ownerJid: instance.ownerJid,
connectionStatus: instance.connectionStatus as any, // Pass connection status
});
}),
);
@ -351,6 +383,7 @@ export class WAMonitoringService {
integration: instance.integration,
token: instance.token,
businessId: instance.businessId,
connectionStatus: instance.connectionStatus as any, // Pass connection status
});
}),
);
@ -361,6 +394,8 @@ export class WAMonitoringService {
try {
await this.waInstances[instanceName]?.sendDataWebhook(Events.REMOVE_INSTANCE, null);
this.clearDelInstanceTime(instanceName);
this.cleaningUp(instanceName);
this.cleaningStoreData(instanceName);
} finally {
@ -377,6 +412,8 @@ export class WAMonitoringService {
try {
await this.waInstances[instanceName]?.sendDataWebhook(Events.LOGOUT_INSTANCE, null);
this.clearDelInstanceTime(instanceName);
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
this.waInstances[instanceName]?.clearCacheChatwoot();
}

View File

@ -88,6 +88,77 @@ export class TemplateService {
}
}
public async edit(
instance: InstanceDto,
data: { templateId: string; category?: string; components?: any; allowCategoryChange?: boolean; ttl?: number },
) {
const getInstance = await this.waMonitor.waInstances[instance.instanceName].instance;
if (!getInstance) {
throw new Error('Instance not found');
}
this.businessId = getInstance.businessId;
this.token = getInstance.token;
const payload: Record<string, unknown> = {};
if (typeof data.category === 'string') payload.category = data.category;
if (typeof data.allowCategoryChange === 'boolean') payload.allow_category_change = data.allowCategoryChange;
if (typeof data.ttl === 'number') payload.time_to_live = data.ttl;
if (data.components) payload.components = data.components;
const response = await this.requestEditTemplate(data.templateId, payload);
if (!response || response.error) {
if (response && response.error) {
const metaError = new Error(response.error.message || 'WhatsApp API Error');
(metaError as any).template = response.error;
throw metaError;
}
throw new Error('Error to edit template');
}
return response;
}
public async delete(instance: InstanceDto, data: { name: string; hsmId?: string }) {
const getInstance = await this.waMonitor.waInstances[instance.instanceName].instance;
if (!getInstance) {
throw new Error('Instance not found');
}
this.businessId = getInstance.businessId;
this.token = getInstance.token;
const response = await this.requestDeleteTemplate({ name: data.name, hsm_id: data.hsmId });
if (!response || response.error) {
if (response && response.error) {
const metaError = new Error(response.error.message || 'WhatsApp API Error');
(metaError as any).template = response.error;
throw metaError;
}
throw new Error('Error to delete template');
}
try {
// Best-effort local cleanup of stored template metadata
await this.prismaRepository.template.deleteMany({
where: {
OR: [
{ name: data.name, instanceId: getInstance.id },
data.hsmId ? { templateId: data.hsmId, instanceId: getInstance.id } : undefined,
].filter(Boolean) as any,
},
});
} catch (err) {
this.logger.warn(
`Failed to cleanup local template records after delete: ${(err as Error)?.message || String(err)}`,
);
}
return response;
}
private async requestTemplate(data: any, method: string) {
try {
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
@ -116,4 +187,38 @@ export class TemplateService {
throw new Error(`Connection error: ${e.message}`);
}
}
private async requestEditTemplate(templateId: string, data: any) {
try {
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
const version = this.configService.get<WaBusiness>('WA_BUSINESS').VERSION;
urlServer = `${urlServer}/${version}/${templateId}`;
const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${this.token}` };
const result = await axios.post(urlServer, data, { headers });
return result.data;
} catch (e) {
this.logger.error(
'WhatsApp API request error: ' + (e.response?.data ? JSON.stringify(e.response?.data) : e.message),
);
if (e.response?.data) return e.response.data;
throw new Error(`Connection error: ${e.message}`);
}
}
private async requestDeleteTemplate(params: { name: string; hsm_id?: string }) {
try {
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
const version = this.configService.get<WaBusiness>('WA_BUSINESS').VERSION;
urlServer = `${urlServer}/${version}/${this.businessId}/message_templates`;
const headers = { Authorization: `Bearer ${this.token}` };
const result = await axios.delete(urlServer, { headers, params });
return result.data;
} catch (e) {
this.logger.error(
'WhatsApp API request error: ' + (e.response?.data ? JSON.stringify(e.response?.data) : e.message),
);
if (e.response?.data) return e.response.data;
throw new Error(`Connection error: ${e.message}`);
}
}
}

View File

@ -52,6 +52,7 @@ export declare namespace wa {
pairingCode?: string;
authState?: { state: AuthenticationState; saveCreds: () => void };
name?: string;
ownerJid?: string;
wuid?: string;
profileName?: string;
profilePictureUrl?: string;

View File

@ -26,8 +26,8 @@ import cors from 'cors';
import express, { json, NextFunction, Request, Response, urlencoded } from 'express';
import { join } from 'path';
function initWA() {
waMonitor.loadInstance();
async function initWA() {
await waMonitor.loadInstance();
}
async function bootstrap() {
@ -159,7 +159,9 @@ async function bootstrap() {
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
initWA();
initWA().catch((error) => {
logger.error('Error loading instances: ' + error);
});
onUnexpectedError();
}

View File

@ -1,5 +1,7 @@
import { socksDispatcher } from 'fetch-socks';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { ProxyAgent } from 'undici';
type Proxy = {
host: string;
@ -17,12 +19,23 @@ function selectProxyAgent(proxyUrl: string): HttpsProxyAgent<string> | SocksProx
// the end so, we add the protocol constants without the `:` to avoid confusion.
const PROXY_HTTP_PROTOCOL = 'http:';
const PROXY_SOCKS_PROTOCOL = 'socks:';
const PROXY_SOCKS5_PROTOCOL = 'socks5:';
switch (url.protocol) {
case PROXY_HTTP_PROTOCOL:
return new HttpsProxyAgent(url);
case PROXY_SOCKS_PROTOCOL:
return new SocksProxyAgent(url);
case PROXY_SOCKS5_PROTOCOL: {
let urlSocks = '';
if (url.username && url.password) {
urlSocks = `socks://${url.username}:${url.password}@${url.hostname}:${url.port}`;
} else {
urlSocks = `socks://${url.hostname}:${url.port}`;
}
return new SocksProxyAgent(urlSocks);
}
default:
throw new Error(`Unsupported proxy protocol: ${url.protocol}`);
}
@ -42,3 +55,57 @@ export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> |
return selectProxyAgent(proxyUrl);
}
export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
let proxyUrl: string;
let protocol: string;
if (typeof proxy === 'string') {
const url = new URL(proxy);
protocol = url.protocol.replace(':', '');
proxyUrl = proxy;
} else {
const { host, password, port, protocol: proto, username } = proxy;
protocol = (proto || 'http').replace(':', '');
if (protocol === 'socks') {
protocol = 'socks5';
}
const auth = username && password ? `${username}:${password}@` : '';
proxyUrl = `${protocol}://${auth}${host}:${port}`;
}
protocol = protocol.toLowerCase();
const PROXY_HTTP_PROTOCOL = 'http';
const PROXY_HTTPS_PROTOCOL = 'https';
const PROXY_SOCKS4_PROTOCOL = 'socks4';
const PROXY_SOCKS5_PROTOCOL = 'socks5';
switch (protocol) {
case PROXY_HTTP_PROTOCOL:
case PROXY_HTTPS_PROTOCOL:
return new ProxyAgent(proxyUrl);
case PROXY_SOCKS4_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL: {
let type: 4 | 5 = 5;
if (PROXY_SOCKS4_PROTOCOL === protocol) type = 4;
const url = new URL(proxyUrl);
return socksDispatcher({
type: type,
host: url.hostname,
port: Number(url.port),
userId: url.username || undefined,
password: url.password || undefined,
});
}
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`);
}
}

View File

@ -65,78 +65,118 @@ interface ISaveOnWhatsappCacheParams {
lid?: 'lid' | undefined;
}
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
if (configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
for (const item of data) {
const remoteJid = item.remoteJid.startsWith('+') ? item.remoteJid.slice(1) : item.remoteJid;
// TODO: Buscar registro existente PRIMEIRO para preservar dados
const allJids = [remoteJid];
const altJid =
item.remoteJidAlt && item.remoteJidAlt.includes('@lid')
? item.remoteJidAlt.startsWith('+')
? item.remoteJidAlt.slice(1)
: item.remoteJidAlt
: null;
if (altJid) {
allJids.push(altJid);
function normalizeJid(jid: string | null | undefined): string | null {
if (!jid) return null;
return jid.startsWith('+') ? jid.slice(1) : jid;
}
const expandedJids = allJids.flatMap((jid) => getAvailableNumbers(jid));
export async function saveOnWhatsappCache(data: ISaveOnWhatsappCacheParams[]) {
if (!configService.get<Database>('DATABASE').SAVE_DATA.IS_ON_WHATSAPP) {
return;
}
// Processa todos os itens em paralelo para melhor performance
const processingPromises = data.map(async (item) => {
try {
const remoteJid = normalizeJid(item.remoteJid);
if (!remoteJid) {
logger.warn('[saveOnWhatsappCache] Item skipped, missing remoteJid.');
return;
}
const altJidNormalized = normalizeJid(item.remoteJidAlt);
const lidAltJid = altJidNormalized && altJidNormalized.includes('@lid') ? altJidNormalized : null;
const baseJids = [remoteJid]; // Garante que o remoteJid esteja na lista inicial
if (lidAltJid) {
baseJids.push(lidAltJid);
}
const expandedJids = baseJids.flatMap((jid) => getAvailableNumbers(jid));
// 1. Busca entrada por jidOptions e também remoteJid
// Às vezes acontece do remoteJid atual NÃO ESTAR no jidOptions ainda, ocasionando o erro:
// 'Unique constraint failed on the fields: (`remoteJid`)'
// Isso acontece principalmente em grupos que possuem o número do criador no ID (ex.: '559911223345-1234567890@g.us')
const existingRecord = await prismaRepository.isOnWhatsapp.findFirst({
where: {
OR: expandedJids.map((jid) => ({ jidOptions: { contains: jid } })),
OR: [
...expandedJids.map((jid) => ({ jidOptions: { contains: jid } })),
{ remoteJid: remoteJid }, // TODO: Descobrir o motivo que causa o remoteJid não estar (às vezes) incluso na lista de jidOptions
],
},
});
logger.verbose(`Register exists: ${existingRecord ? existingRecord.remoteJid : 'não not found'}`);
const finalJidOptions = [...expandedJids];
if (existingRecord?.jidOptions) {
const existingJids = existingRecord.jidOptions.split(',');
// TODO: Adicionar JIDs existentes que não estão na lista atual
existingJids.forEach((jid) => {
if (!finalJidOptions.includes(jid)) {
finalJidOptions.push(jid);
}
});
}
// TODO: Se tiver remoteJidAlt com @lid novo, adicionar
if (altJid && !finalJidOptions.includes(altJid)) {
finalJidOptions.push(altJid);
}
const uniqueNumbers = Array.from(new Set(finalJidOptions));
logger.verbose(
`Saving: remoteJid=${remoteJid}, jidOptions=${uniqueNumbers.join(',')}, lid=${item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null}`,
`[saveOnWhatsappCache] Register exists for [${expandedJids.join(',')}]? => ${existingRecord ? existingRecord.remoteJid : 'Not found'}`,
);
// 2. Unifica todos os JIDs usando um Set para garantir valores únicos
const finalJidOptions = new Set(expandedJids);
if (lidAltJid) {
finalJidOptions.add(lidAltJid);
}
if (existingRecord?.jidOptions) {
existingRecord.jidOptions.split(',').forEach((jid) => finalJidOptions.add(jid));
}
// 3. Prepara o payload final
// Ordena os JIDs para garantir consistência na string final
const sortedJidOptions = [...finalJidOptions].sort();
const newJidOptionsString = sortedJidOptions.join(',');
const newLid = item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null;
const dataPayload = {
remoteJid: remoteJid,
jidOptions: newJidOptionsString,
lid: newLid,
};
// 4. Decide entre Criar ou Atualizar
if (existingRecord) {
// Compara a string de JIDs ordenada existente com a nova
const existingJidOptionsString = existingRecord.jidOptions
? existingRecord.jidOptions.split(',').sort().join(',')
: '';
const isDataSame =
existingRecord.remoteJid === dataPayload.remoteJid &&
existingJidOptionsString === dataPayload.jidOptions &&
existingRecord.lid === dataPayload.lid;
if (isDataSame) {
logger.verbose(`[saveOnWhatsappCache] Data for ${remoteJid} is already up-to-date. Skipping update.`);
return; // Pula para o próximo item
}
// Os dados são diferentes, então atualiza
logger.verbose(
`[saveOnWhatsappCache] Register exists, updating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
);
await prismaRepository.isOnWhatsapp.update({
where: { id: existingRecord.id },
data: {
remoteJid: remoteJid,
jidOptions: uniqueNumbers.join(','),
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
},
data: dataPayload,
});
} else {
// Cria nova entrada
logger.verbose(
`[saveOnWhatsappCache] Register does not exist, creating: remoteJid=${remoteJid}, jidOptions=${dataPayload.jidOptions}, lid=${dataPayload.lid}`,
);
await prismaRepository.isOnWhatsapp.create({
data: {
remoteJid: remoteJid,
jidOptions: uniqueNumbers.join(','),
lid: item.lid === 'lid' || item.remoteJid?.includes('@lid') ? 'lid' : null,
},
data: dataPayload,
});
}
} catch (e) {
// Loga o erro mas não para a execução dos outros promises
logger.error(`[saveOnWhatsappCache] Error processing item for ${item.remoteJid}: `);
logger.error(e);
}
}
});
// Espera todas as operações paralelas terminarem
await Promise.allSettled(processingPromises);
}
export async function getOnWhatsappCache(remoteJids: string[]) {

View File

@ -1,6 +1,7 @@
import { prismaRepository } from '@api/server.module';
import { CacheService } from '@api/services/cache.service';
import { CacheConf, configService } from '@config/env.config';
import { Logger } from '@config/logger.config';
import { INSTANCE_DIR } from '@config/path.config';
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
import fs from 'fs/promises';
@ -73,12 +74,15 @@ async function fileExists(file: string): Promise<any> {
}
}
const logger = new Logger('useMultiFileAuthStatePrisma');
export default async function useMultiFileAuthStatePrisma(
sessionId: string,
cache: CacheService,
): Promise<{
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
}> {
const localFolder = path.join(INSTANCE_DIR, sessionId);
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
@ -142,6 +146,26 @@ export default async function useMultiFileAuthStatePrisma(
}
}
async function removeCreds(): Promise<any> {
const cacheConfig = configService.get<CacheConf>('CACHE');
// Redis
try {
if (cacheConfig.REDIS.ENABLED) {
await cache.delete(sessionId);
logger.info({ action: 'redis.delete', sessionId });
return;
}
} catch (err) {
logger.warn({ action: 'redis.delete', sessionId, err });
}
logger.info({ action: 'auth.key.delete', sessionId });
await deleteAuthKey(sessionId);
}
let creds = await readData('creds');
if (!creds) {
creds = initAuthCreds();
@ -183,5 +207,7 @@ export default async function useMultiFileAuthStatePrisma(
saveCreds: () => {
return writeData(creds, 'creds');
},
removeCreds,
};
}

View File

@ -39,7 +39,11 @@ import { Logger } from '@config/logger.config';
import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys';
import { isNotEmpty } from 'class-validator';
export type AuthState = { state: AuthenticationState; saveCreds: () => Promise<void> };
export type AuthState = {
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
};
export class AuthStateProvider {
constructor(private readonly providerFiles: ProviderFiles) {}
@ -86,6 +90,18 @@ export class AuthStateProvider {
return response;
};
const removeCreds = async () => {
const [response, error] = await this.providerFiles.removeSession(instance);
if (error) {
// this.logger.error(['removeData', error?.message, error?.stack]);
return;
}
logger.info({ action: 'remove.session', instance, response });
return;
};
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
return {
@ -126,6 +142,10 @@ export class AuthStateProvider {
saveCreds: async () => {
return await writeData(creds, 'creds');
},
removeCreds,
};
}
}
const logger = new Logger('useMultiFileAuthStatePrisma');

View File

@ -8,6 +8,7 @@ export async function useMultiFileAuthStateRedisDb(
): Promise<{
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
}> {
const logger = new Logger('useMultiFileAuthStateRedisDb');
@ -36,6 +37,16 @@ export async function useMultiFileAuthStateRedisDb(
}
};
async function removeCreds(): Promise<any> {
try {
logger.warn({ action: 'redis.delete', instanceName });
return await cache.delete(instanceName);
} catch {
return;
}
}
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
return {
@ -76,5 +87,7 @@ export async function useMultiFileAuthStateRedisDb(
saveCreds: async () => {
return await writeData(creds, 'creds');
},
removeCreds,
};
}

View File

@ -195,8 +195,9 @@ export const contactValidateSchema: JSONSchema7 = {
_id: { type: 'string', minLength: 1 },
pushName: { type: 'string', minLength: 1 },
id: { type: 'string', minLength: 1 },
remoteJid: { type: 'string', minLength: 1 },
},
...isNotEmpty('_id', 'id', 'pushName'),
...isNotEmpty('_id', 'id', 'pushName', 'remoteJid'),
},
},
};

View File

@ -0,0 +1,32 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties: Record<string, unknown> = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
} as JSONSchema7;
};
export const templateDeleteSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
name: { type: 'string' },
hsmId: { type: 'string' },
},
required: ['name'],
...isNotEmpty('name'),
};

View File

@ -0,0 +1,35 @@
import { JSONSchema7 } from 'json-schema';
import { v4 } from 'uuid';
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
const properties: Record<string, unknown> = {};
propertyNames.forEach(
(property) =>
(properties[property] = {
minLength: 1,
description: `The "${property}" cannot be empty`,
}),
);
return {
if: {
propertyNames: {
enum: [...propertyNames],
},
},
then: { properties },
} as JSONSchema7;
};
export const templateEditSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
templateId: { type: 'string' },
category: { type: 'string', enum: ['AUTHENTICATION', 'MARKETING', 'UTILITY'] },
allowCategoryChange: { type: 'boolean' },
ttl: { type: 'number' },
components: { type: 'array' },
},
required: ['templateId'],
...isNotEmpty('templateId'),
};

View File

@ -8,5 +8,7 @@ export * from './message.schema';
export * from './proxy.schema';
export * from './settings.schema';
export * from './template.schema';
export * from './templateDelete.schema';
export * from './templateEdit.schema';
export * from '@api/integrations/chatbot/chatbot.schema';
export * from '@api/integrations/event/event.schema';