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.

feat: improve issue ghost reply (#386)

Aldino Kemalfc2506d79a01
diff --git a/src/infrastructure/whatsapp/init.go b/src/infrastructure/whatsapp/init.go
index 9998da3e..a2ad9238 100644
--- a/src/infrastructure/whatsapp/init.go
+++ b/src/infrastructure/whatsapp/init.go
@@ -532,56 +532,109 @@ func handleAutoMarkRead(_ context.Context, evt *events.Message) {
}
func handleAutoReply(ctx context.Context, evt *events.Message, chatStorageRepo domainChatStorage.IChatStorageRepository) {
- if config.WhatsappAutoReplyMessage != "" &&
- !utils.IsGroupJID(evt.Info.Chat.String()) &&
- !evt.Info.IsIncomingBroadcast() &&
- !evt.Info.IsFromMe {
-
- // Check if the incoming message has text content using the utility function
- messageText := utils.ExtractMessageTextFromEvent(evt)
- if messageText == "" {
- return // Don't auto-reply to non-text messages
- }
+ if config.WhatsappAutoReplyMessage == "" {
+ return
+ }
- // Format recipient JID
- recipientJID := utils.FormatJID(evt.Info.Sender.String())
+ // Skip groups, broadcasts, and self messages
+ if utils.IsGroupJID(evt.Info.Chat.String()) || evt.Info.IsIncomingBroadcast() || evt.Info.IsFromMe {
+ return
+ }
- // Send the auto-reply message
- response, err := cli.SendMessage(
- ctx,
- recipientJID,
- &waE2E.Message{Conversation: proto.String(config.WhatsappAutoReplyMessage)},
- )
+ // Only reply to direct 1:1 chats (e.g., *@s.whatsapp.net)
+ if evt.Info.Chat.Server != types.DefaultUserServer {
+ return
+ }
- if err != nil {
- log.Errorf("Failed to send auto-reply message: %v", err)
- return
- }
+ // Extra safety: skip any broadcast/status contexts
+ source := evt.Info.SourceString()
+ if strings.Contains(source, "broadcast") ||
+ strings.HasSuffix(evt.Info.Chat.String(), "@broadcast") ||
+ strings.HasPrefix(evt.Info.Chat.String(), "status@") {
+ return
+ }
- // Store the auto-reply message in chat storage if send was successful
- if chatStorageRepo != nil {
- // Get our own JID as sender
- senderJID := ""
- if cli.Store.ID != nil {
- senderJID = cli.Store.ID.String()
- }
+ // Require actual typed text (not captions or synthetic labels)
+ hasText := false
- // Store the sent auto-reply message
- if err := chatStorageRepo.StoreSentMessageWithContext(
- ctx,
- response.ID, // Message ID from WhatsApp response
- senderJID, // Our JID as sender
- recipientJID.String(), // Recipient JID
- config.WhatsappAutoReplyMessage, // Auto-reply content
- response.Timestamp, // Timestamp from response
- ); err != nil {
- // Log storage error but don't fail the auto-reply
- log.Errorf("Failed to store auto-reply message in chat storage: %v", err)
- } else {
- log.Debugf("Auto-reply message %s stored successfully in chat storage", response.ID)
+ // Unwrap FutureProof wrappers to access the inner message content first
+ innerMsg := evt.Message
+ for i := 0; i < 3; i++ { // safeguard against excessively nested wrappers
+ if vm := innerMsg.GetViewOnceMessage(); vm != nil && vm.GetMessage() != nil {
+ innerMsg = vm.GetMessage()
+ continue
+ }
+ if em := innerMsg.GetEphemeralMessage(); em != nil && em.GetMessage() != nil {
+ innerMsg = em.GetMessage()
+ continue
+ }
+ if vm2 := innerMsg.GetViewOnceMessageV2(); vm2 != nil && vm2.GetMessage() != nil {
+ innerMsg = vm2.GetMessage()
+ continue
+ }
+ if vm2e := innerMsg.GetViewOnceMessageV2Extension(); vm2e != nil && vm2e.GetMessage() != nil {
+ innerMsg = vm2e.GetMessage()
+ continue
+ }
+ break
+ }
+
+ // Check for genuine typed text on the unwrapped content
+ if conv := innerMsg.GetConversation(); conv != "" {
+ hasText = true
+ } else if ext := innerMsg.GetExtendedTextMessage(); ext != nil && ext.GetText() != "" {
+ hasText = true
+ } else if protoMsg := innerMsg.GetProtocolMessage(); protoMsg != nil {
+ if edited := protoMsg.GetEditedMessage(); edited != nil {
+ if ext := edited.GetExtendedTextMessage(); ext != nil && ext.GetText() != "" {
+ hasText = true
+ } else if conv := edited.GetConversation(); conv != "" {
+ hasText = true
}
}
}
+ if !hasText {
+ return
+ }
+
+ // Format recipient JID
+ recipientJID := utils.FormatJID(evt.Info.Sender.String())
+
+ // Send the auto-reply message
+ response, err := cli.SendMessage(
+ ctx,
+ recipientJID,
+ &waE2E.Message{Conversation: proto.String(config.WhatsappAutoReplyMessage)},
+ )
+
+ if err != nil {
+ log.Errorf("Failed to send auto-reply message: %v", err)
+ return
+ }
+
+ // Store the auto-reply message in chat storage if send was successful
+ if chatStorageRepo != nil {
+ // Get our own JID as sender
+ senderJID := ""
+ if cli.Store.ID != nil {
+ senderJID = cli.Store.ID.String()
+ }
+
+ // Store the sent auto-reply message
+ if err := chatStorageRepo.StoreSentMessageWithContext(
+ ctx,
+ response.ID, // Message ID from WhatsApp response
+ senderJID, // Our JID as sender
+ recipientJID.String(), // Recipient JID
+ config.WhatsappAutoReplyMessage, // Auto-reply content
+ response.Timestamp, // Timestamp from response
+ ); err != nil {
+ // Log storage error but don't fail the auto-reply
+ log.Errorf("Failed to store auto-reply message in chat storage: %v", err)
+ } else {
+ log.Debugf("Auto-reply message %s stored successfully in chat storage", response.ID)
+ }
+ }
}
func handleWebhookForward(ctx context.Context, evt *events.Message) {