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): forward WhatsApp reactions to Chatwoot (#623)

Aldino Kemal2d27ea8402dd
diff --git a/src/infrastructure/whatsapp/chatwoot_forward_test.go b/src/infrastructure/whatsapp/chatwoot_forward_test.go
index 3bf996a6..cf17372f 100644
--- a/src/infrastructure/whatsapp/chatwoot_forward_test.go
+++ b/src/infrastructure/whatsapp/chatwoot_forward_test.go
@@ -1,6 +1,10 @@
package whatsapp
-import "testing"
+import (
+ "testing"
+
+ "github.com/aldinokemal/go-whatsapp-web-multidevice/config"
+)
func TestChatwootMessageTypeFromPayload(t *testing.T) {
tests := []struct {
@@ -43,3 +47,127 @@ func TestChatwootMessageTypeFromPayload(t *testing.T) {
})
}
}
+
+func TestShouldForwardEventToChatwoot(t *testing.T) {
+ tests := []struct {
+ name string
+ eventName string
+ expected bool
+ }{
+ {name: "message supported", eventName: "message", expected: true},
+ {name: "message reaction supported", eventName: "message.reaction", expected: true},
+ {name: "message edited unsupported", eventName: "message.edited", expected: false},
+ {name: "message revoked unsupported", eventName: "message.revoked", expected: false},
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := shouldForwardEventToChatwoot(tc.eventName); got != tc.expected {
+ t.Fatalf("expected %v, got %v", tc.expected, got)
+ }
+ })
+ }
+}
+
+func TestIsEventWhitelistedForChatwoot(t *testing.T) {
+ originalEvents := config.WhatsappWebhookEvents
+ defer func() { config.WhatsappWebhookEvents = originalEvents }()
+
+ t.Run("empty whitelist allows all", func(t *testing.T) {
+ config.WhatsappWebhookEvents = nil
+ if !isEventWhitelistedForChatwoot("message.reaction") {
+ t.Fatal("expected message.reaction to be allowed when whitelist is empty")
+ }
+ })
+
+ t.Run("explicit reaction whitelist allowed", func(t *testing.T) {
+ config.WhatsappWebhookEvents = []string{"message.reaction"}
+ if !isEventWhitelistedForChatwoot("message.reaction") {
+ t.Fatal("expected message.reaction to be allowed when explicitly whitelisted")
+ }
+ })
+
+ t.Run("message whitelist also allows reactions for chatwoot", func(t *testing.T) {
+ config.WhatsappWebhookEvents = []string{"message"}
+ if !isEventWhitelistedForChatwoot("message.reaction") {
+ t.Fatal("expected message.reaction to be allowed for Chatwoot when message is whitelisted")
+ }
+ })
+
+ t.Run("unrelated whitelist blocks reaction", func(t *testing.T) {
+ config.WhatsappWebhookEvents = []string{"message.ack"}
+ if isEventWhitelistedForChatwoot("message.reaction") {
+ t.Fatal("expected message.reaction to be blocked for Chatwoot when not covered by whitelist")
+ }
+ })
+}
+
+func TestBuildReactionChatwootContent(t *testing.T) {
+ tests := []struct {
+ name string
+ payload map[string]interface{}
+ isGroup bool
+ fromName string
+ expected string
+ }{
+ {
+ name: "group reaction with sender name and target id",
+ payload: map[string]interface{}{
+ "reaction": "πŸ‘",
+ "reacted_message_id": "wamid-123",
+ },
+ isGroup: true,
+ fromName: "Alice",
+ expected: "Alice reacted πŸ‘ to message wamid-123",
+ },
+ {
+ name: "direct reaction falls back to phone",
+ payload: map[string]interface{}{
+ "reaction": "πŸ”₯",
+ "reacted_message_id": "wamid-456",
+ "from": "[email protected]",
+ },
+ isGroup: false,
+ fromName: "",
+ expected: "628123456789 reacted πŸ”₯ to message wamid-456",
+ },
+ {
+ name: "reaction removal",
+ payload: map[string]interface{}{
+ "reaction": "",
+ "reacted_message_id": "wamid-789",
+ },
+ isGroup: true,
+ fromName: "Bob",
+ expected: "Bob removed a reaction from message wamid-789",
+ },
+ {
+ name: "group reaction falls back to sender jid when pushname missing",
+ payload: map[string]interface{}{
+ "reaction": "πŸ˜‚",
+ "reacted_message_id": "wamid-999",
+ "from": "[email protected]",
+ },
+ isGroup: true,
+ fromName: "",
+ expected: "628777000111 reacted πŸ˜‚ to message wamid-999",
+ },
+ {
+ name: "missing target id still produces readable text",
+ payload: map[string]interface{}{
+ "reaction": "❀️",
+ },
+ isGroup: true,
+ fromName: "Carol",
+ expected: "Carol reacted ❀️",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := buildReactionChatwootContent(tc.payload, tc.isGroup, tc.fromName); got != tc.expected {
+ t.Fatalf("expected %q, got %q", tc.expected, got)
+ }
+ })
+ }
+}
diff --git a/src/infrastructure/whatsapp/webhook_forward.go b/src/infrastructure/whatsapp/webhook_forward.go
index 6eb1331d..e956804b 100644
--- a/src/infrastructure/whatsapp/webhook_forward.go
+++ b/src/infrastructure/whatsapp/webhook_forward.go
@@ -72,18 +72,23 @@ func getContactMutex(phone string) *sync.Mutex {
// It only returns an error when all webhook deliveries fail. Partial failures are logged and suppressed so
// successful targets still receive the event.
func forwardPayloadToConfiguredWebhooks(ctx context.Context, payload map[string]any, eventName string) error {
- // Check if event is whitelisted (if whitelist is configured)
- if len(config.WhatsappWebhookEvents) > 0 {
- if !isEventWhitelisted(eventName) {
- logrus.Debugf("Skipping event %s - not in webhook events whitelist", eventName)
- return nil
- }
+ webhookAllowed := len(config.WhatsappWebhookEvents) == 0 || isEventWhitelisted(eventName)
+ chatwootAllowed := config.ChatwootEnabled && shouldForwardEventToChatwoot(eventName) && isEventWhitelistedForChatwoot(eventName)
+
+ if !webhookAllowed && !chatwootAllowed {
+ logrus.Debugf("Skipping event %s - not allowed for webhooks or Chatwoot", eventName)
+ return nil
}
- err := forwardToWebhooks(ctx, payload, eventName)
+ var err error
+ if webhookAllowed {
+ err = forwardToWebhooks(ctx, payload, eventName)
+ } else {
+ logrus.Debugf("Skipping event %s for configured webhooks, but allowing Chatwoot", eventName)
+ }
- if eventName == "message" && config.ChatwootEnabled {
- go forwardToChatwoot(ctx, payload)
+ if chatwootAllowed {
+ go forwardToChatwoot(ctx, payload, eventName)
}
return err
@@ -215,6 +220,49 @@ func buildChatwootMessageContent(data map[string]interface{}, isGroup bool, from
return content, attachments
}
+func shouldForwardEventToChatwoot(eventName string) bool {
+ switch eventName {
+ case "message", "message.reaction":
+ return true
+ default:
+ return false
+ }
+}
+
+func isEventWhitelistedForChatwoot(eventName string) bool {
+ if len(config.WhatsappWebhookEvents) == 0 {
+ return true
+ }
+ if isEventWhitelisted(eventName) {
+ return true
+ }
+ return eventName == "message.reaction" && isEventWhitelisted("message")
+}
+
+func buildReactionChatwootContent(data map[string]interface{}, isGroup bool, fromName string) string {
+ reaction, _ := data["reaction"].(string)
+ reactedMessageID, _ := data["reacted_message_id"].(string)
+
+ actor := "Someone"
+ if fromName != "" {
+ actor = fromName
+ } else if from, ok := data["from"].(string); ok && from != "" {
+ actor = utils.ExtractPhoneFromJID(from)
+ }
+
+ if reactedMessageID != "" {
+ if reaction == "" {
+ return fmt.Sprintf("%s removed a reaction from message %s", actor, reactedMessageID)
+ }
+ return fmt.Sprintf("%s reacted %s to message %s", actor, reaction, reactedMessageID)
+ }
+
+ if reaction == "" {
+ return fmt.Sprintf("%s removed a reaction", actor)
+ }
+ return fmt.Sprintf("%s reacted %s", actor, reaction)
+}
+
func chatwootMessageTypeFromPayload(data map[string]interface{}) string {
if isFromMe, ok := data["is_from_me"].(bool); ok && isFromMe {
return "outgoing"
@@ -337,8 +385,8 @@ func syncMessageToChatwoot(cw *chatwoot.Client, info *chatwootContactInfo, conte
return nil
}
-func forwardToChatwoot(ctx context.Context, payload map[string]any) {
- logrus.Info("Chatwoot: Attempting to forward message...")
+func forwardToChatwoot(ctx context.Context, payload map[string]any, eventName string) {
+ logrus.Infof("Chatwoot: Attempting to forward %s...", eventName)
cw := chatwoot.GetDefaultClient()
if !cw.IsConfigured() {
logrus.Warn("Chatwoot: Client is not configured (check CHATWOOT_* env vars)")
@@ -359,7 +407,16 @@ func forwardToChatwoot(ctx context.Context, payload map[string]any) {
}
// Build message content
- content, attachments := buildChatwootMessageContent(data, info.IsGroup, info.FromName)
+ var (
+ content string
+ attachments []string
+ )
+ switch eventName {
+ case "message.reaction":
+ content = buildReactionChatwootContent(data, info.IsGroup, info.FromName)
+ default:
+ content, attachments = buildChatwootMessageContent(data, info.IsGroup, info.FromName)
+ }
info.IsFromMe = chatwootMessageTypeFromPayload(data) == "outgoing"
// Sync to Chatwoot