# WuzAPI Reply/Quote System - Technical Documentation > **Last Updated:** 2026-01-24 > **Status:** ✅ Working > **Author:** AI Assistant ## Overview This document explains how the Reply/Quote feature works for WhatsApp messages through WuzAPI integration. When a user replies to a message in WhatsApp, the quoted message should appear in Chatwoot's interface. --- ## Architecture Diagram ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ REPLY FLOW │ └─────────────────────────────────────────────────────────────────────────────┘ WhatsApp User replies to a message │ ▼ ┌────────────────────┐ │ WuzAPI Server │ ──► Sends webhook with contextInfo.stanzaID └────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────────────────┐ │ Chatwoot Backend │ │ │ │ 1. WhatsappController.sanitize_payload_for_sidekiq (preserves stanzaID) │ │ 2. WhatsappEventsJob → IncomingMessageWuzapiService │ │ 3. PayloadParser.in_reply_to_external_id (extracts stanzaID) │ │ 4. build_message → finds original by source_id, sets in_reply_to_id │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────────────────┐ │ Chatwoot Frontend │ │ │ │ MessageList.vue → getInReplyToMessage() → looks up inReplyToId │ │ Base.vue → displays replyToPreview │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` --- ## Key Files | File | Purpose | | ------------------------------------------------------------------- | ---------------------------------------- | | `app/controllers/webhooks/whatsapp_controller.rb` | Sanitizes payload, preserves `stanzaID` | | `app/services/whatsapp/incoming_message_wuzapi_service.rb` | Creates message with `in_reply_to_id` | | `app/services/whatsapp/providers/wuzapi/payload_parser.rb` | Extracts `stanzaID` from contextInfo | | `app/services/whatsapp/providers/wuzapi_service.rb` | Sends messages, returns clean `WAID:xxx` | | `app/javascript/dashboard/components-next/message/MessageList.vue` | Resolves reply reference | | `app/javascript/dashboard/components-next/message/bubbles/Base.vue` | Displays quoted message | --- ## How It Works ### 1. Incoming Reply (WhatsApp → Chatwoot) **Step 1: WuzAPI sends webhook with reply context** ```json { "event": { "Message": { "extendedTextMessage": { "text": "My reply message", "contextInfo": { "stanzaID": "3EB0B12FB7571691E025DD", "participant": "556191544165@s.whatsapp.net", "quotedMessage": { "conversation": "Original message" } } } } } } ``` **Step 2: Sanitizer preserves stanzaID** (`whatsapp_controller.rb` lines 84-90) ```ruby if msg['extendedTextMessage']['contextInfo'].is_a?(Hash) ctx = msg['extendedTextMessage']['contextInfo'] clean_msg['extendedTextMessage']['contextInfo'] = { 'stanzaID' => ctx['stanzaID'] || ctx['stanzaId'], 'participant' => ctx['participant'] }.compact end ``` **Step 3: PayloadParser extracts stanzaID** (`payload_parser.rb`) ```ruby def in_reply_to_external_id msg = unwrap_ephemeral_message(params.dig(:event, :Message)) ctx = msg.dig(:extendedTextMessage, :contextInfo) return ctx[:stanzaID] || ctx[:stanzaId] if ctx.present? # ... also checks imageMessage, videoMessage, etc. end ``` **Step 4: IncomingMessageWuzapiService links messages** (lines 112-124) ```ruby if (reply_id = parser.in_reply_to_external_id).present? clean_reply_id = "WAID:#{reply_id}" original_message = conversation.messages.find_by(source_id: clean_reply_id) if original_message msg_params[:in_reply_to_id] = original_message.id else # Fallback: store for UI display msg_params[:content_attributes] = { in_reply_to_external_id: clean_reply_id } end end ``` ### 2. Outgoing Messages (Chatwoot → WhatsApp) **CRITICAL**: The `source_id` must be saved in format `WAID:xxx` for replies to work! **WuzapiService.send_message** extracts ID from response: ```ruby def send_message(phone_number, message) # ... send message to WuzAPI ... response = client.send_text(...) extract_message_id(response) # Returns "WAID:xxx" end def extract_message_id(response) # WuzAPI returns: {"code" => 200, "data" => {"Id" => "xxx"}} message_id = response.dig('data', 'Id') return nil if message_id.blank? "WAID:#{message_id}" end ``` ### 3. Frontend Display **MessageList.vue** resolves the reply: ```javascript const getInReplyToMessage = parentMessage => { const inReplyToMessageId = parentMessage.inReplyToId ?? parentMessage.contentAttributes?.inReplyTo; if (!inReplyToMessageId) return null; return props.messages?.find(msg => msg.id === inReplyToMessageId); }; ``` **Base.vue** displays the preview: ```vue