2.3 KiB
2.3 KiB
WuzAPI Reliability & Sync Plan (Phase 9)
Goal
Fix outbound message failures (remove 24h restriction) and enable 2-way sync so messages sent from the mobile device appear in Chatwoot.
Issues to Fix
-
Outbound Failure ("Falha ao enviar"):
- Cause A (Likely): Chatwoot UI prevents sending due to "24h Window" restriction (Red banner in screenshot).
- Cause B:
WuzapiServicefailing with API error (Auth/URL). - Cause C: Phone number formatting mismatch.
-
Mobile Sync (Missing Replies):
- Cause: We strictly ignore
from_memessages inIncomingMessageWuzapiService. - Requirement: We must process
from_memessages asmessage_type: :outgoingto reflect phone-initiated replies.
- Cause: We strictly ignore
Implementation Steps
1. Disable 24h Window for WuzAPI
The 24h window is a WhatsApp Business Policy constraint. For WuzAPI (User App API), this restriction does not exist technically. We must tell Chatwoot to ignore it.
- Action: Override
messaging_window_enabled?inChannel::Whatsapp. - Logic: Return
falseifprovider == 'wuzapi'.
2. Debug & Fix Outbound Service
- Action: Verify
WuzapiService#send_messageactually works. - Action: Enhance
send_messageto capture the WuzAPIidfrom the response (if available) and update theMessagerecordsource_id. This is critical for deduplication.
3. Implement Mobile Sync (Two-Way)
- File:
app/services/whatsapp/incoming_message_wuzapi_service.rb - Logic Change:
- Remove:
return if parser.from_me?loop protection (the "dumb" one). - Add: "Smart" Echo Handling.
- If
parser.from_me?, setmessage_type = :outgoing. - Crucial: Rely on
Strong Dedupe(checkingsource_id). - Scenario: Chatwoot creates message -> Sends to WuzAPI -> WuzAPI returns ID key -> Chatwoot updates
source_id. -> Webhook arrives with sameID-> Ignored (Correct). - Scenario: Phone sends message -> Webhook arrives with
ID-> Chatwoot doesn't have it -> Created asoutgoing(Correct).
- If
- Remove:
Verification Plan
- Console Test: Send message via
Rails clogic to confirm API works. - UI Test: Refresh page, check if Red Banner is gone. Try sending.
- Sync Test: Send "Teste do Celular" from WhatsApp mobile. Check if it appears in Chatwoot as an agent message.