Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

fix(chatwoot): prevent reaction echo send-back (#943)

andersonrfsilva2600ac4fc553
diff --git a/docs/chatwoot.md b/docs/chatwoot.md
index 1cbae0a1..7e80d6d5 100644
--- a/docs/chatwoot.md
+++ b/docs/chatwoot.md
@@ -614,6 +614,8 @@ With `CHATWOOT_MESSAGE_READ=true`, WhatsApp read receipts update Chatwoot `last_
Inbound replies and reactions are threaded onto the message they reference (again via the `WAID:` source-id), so a reply or 👍 lands attached to the right message in the Chatwoot conversation.
+Mirrored reaction notes use the reaction event's own `WAID:<id>` as their source ID. This prevents outgoing reaction echoes from being sent back to WhatsApp when Chatwoot's webhook arrives before the sent-message cache is updated.
+
Agent replies are quoted the other way: when an agent uses **Reply** in Chatwoot, the WhatsApp message quotes the original. The text and every attachment carry the quote, including the audio-to-file fallback; the caption stays on the first attachment only.
- A message mirrored from WhatsApp (`in_reply_to_external_id` is `WAID:<id>`) is quoted only when it is in the sending device's local message store and belongs to the same chat. An `@lid` chat matches its phone JID when the sending device knows the mapping.
diff --git a/src/infrastructure/whatsapp/chatwoot_forward_test.go b/src/infrastructure/whatsapp/chatwoot_forward_test.go
index 851a8636..6ba7cb85 100644
--- a/src/infrastructure/whatsapp/chatwoot_forward_test.go
+++ b/src/infrastructure/whatsapp/chatwoot_forward_test.go
@@ -2,7 +2,10 @@ package whatsapp
import (
"context"
+ "encoding/json"
"errors"
+ "net/http"
+ "net/http/httptest"
"testing"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
@@ -448,3 +451,66 @@ func TestBuildReactionChatwootContent(t *testing.T) {
})
}
}
+
+func TestSyncPayloadToChatwoot_ReactionEchoStampsSourceID(t *testing.T) {
+ var capturedBody map[string]any
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var response any
+ switch {
+ case r.Method == http.MethodGet && r.URL.Path == "/api/v1/accounts/1/contacts/search":
+ response = map[string]any{"payload": []map[string]any{{"id": 7, "name": "Alice", "phone_number": r.URL.Query().Get("q")}}}
+ case r.Method == http.MethodGet && r.URL.Path == "/api/v1/accounts/1/contacts/7/conversations":
+ response = map[string]any{"payload": []map[string]any{{"id": 42, "inbox_id": 1, "status": "open"}}}
+ case r.Method == http.MethodPost && r.URL.Path == "/api/v1/accounts/1/conversations/42/messages":
+ if err := json.NewDecoder(r.Body).Decode(&capturedBody); err != nil {
+ t.Errorf("decode message body: %v", err)
+ }
+ response = map[string]any{"id": 999}
+ default:
+ t.Errorf("unexpected request: %s %s", r.Method, r.URL.String())
+ http.Error(w, "unexpected request", http.StatusNotFound)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ if err := json.NewEncoder(w).Encode(response); err != nil {
+ t.Errorf("encode response: %v", err)
+ }
+ }))
+ defer server.Close()
+
+ cw := &chatwoot.Client{BaseURL: server.URL, APIToken: "t", AccountID: 1, InboxID: 1, HTTPClient: server.Client()}
+ orig := getChatwootClientFn
+ getChatwootClientFn = func(string) (*chatwoot.ResolvedConfig, error) {
+ return &chatwoot.ResolvedConfig{Client: cw}, nil
+ }
+ t.Cleanup(func() { getChatwootClientFn = orig })
+
+ payload := map[string]any{"payload": map[string]any{
+ "id": "REACTION-ID-1",
+ "reacted_message_id": "ORIGINAL-ID-1",
+ "reaction": "👍",
+ "from": "[email protected]",
+ "chat_id": "[email protected]",
+ "from_name": "Alice",
+ "is_from_me": true,
+ }}
+ if err := syncPayloadToChatwoot(context.Background(), payload, "message.reaction", "", nil); err != nil {
+ t.Fatalf("syncPayloadToChatwoot: %v", err)
+ }
+ if capturedBody == nil {
+ t.Fatal("message-create endpoint was never called")
+ }
+ if got := capturedBody["source_id"]; got != "WAID:REACTION-ID-1" {
+ t.Errorf("source_id = %v, want WAID:REACTION-ID-1", got)
+ }
+ if got := capturedBody["message_type"]; got != "outgoing" {
+ t.Errorf("message_type = %v, want outgoing", got)
+ }
+ if got := capturedBody["private"]; got != false {
+ t.Errorf("private = %v, want false", got)
+ }
+ attributes, _ := capturedBody["content_attributes"].(map[string]any)
+ if got := attributes["in_reply_to_external_id"]; got != "WAID:ORIGINAL-ID-1" {
+ t.Errorf("in_reply_to_external_id = %v, want WAID:ORIGINAL-ID-1", got)
+ }
+}
diff --git a/src/infrastructure/whatsapp/webhook_forward.go b/src/infrastructure/whatsapp/webhook_forward.go
index 355e36e7..39340a89 100644
--- a/src/infrastructure/whatsapp/webhook_forward.go
+++ b/src/infrastructure/whatsapp/webhook_forward.go
@@ -1663,6 +1663,9 @@ func syncPayloadToChatwoot(ctx context.Context, payload map[string]any, eventNam
switch eventName {
case "message.reaction":
content = buildReactionChatwootContent(data, info.FromName)
+ if id, _ := data["id"].(string); id != "" {
+ msgOpts.SourceID = "WAID:" + id
+ }
if rid, _ := data["reacted_message_id"].(string); rid != "" {
msgOpts.ContentAttributes = map[string]any{"in_reply_to_external_id": "WAID:" + rid}
}