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.

Persist incoming contact messages in chatstorage (#654)

Júlio Mühlbauer381c3815c957
diff --git a/src/pkg/utils/whatsapp.go b/src/pkg/utils/whatsapp.go
index 2883d3ef..c1e8e4d5 100644
--- a/src/pkg/utils/whatsapp.go
+++ b/src/pkg/utils/whatsapp.go
@@ -56,6 +56,18 @@ func resolveKnownDocumentExtension(mimeType string) (string, bool) {
return ext, ok
}
+func extractPhoneFromVCard(vcard string) string {
+ for _, line := range strings.Split(vcard, "\n") {
+ line = strings.TrimSpace(line)
+ if strings.HasPrefix(strings.ToUpper(line), "TEL") {
+ if idx := strings.LastIndex(line, ":"); idx >= 0 {
+ return strings.TrimSpace(line[idx+1:])
+ }
+ }
+ }
+ return ""
+}
+
// KnownDocumentMIMEByExtension returns a known MIME type for a given Office document extension.
func KnownDocumentMIMEByExtension(ext string) (string, bool) {
return resolveKnownDocumentMIME(ext)
@@ -138,6 +150,43 @@ func ExtractMessageTextFromProto(msg *waE2E.Message) string {
return templateButtonReply.GetSelectedDisplayText()
}
+ // Check for shared contact card
+ if contact := msg.GetContactMessage(); contact != nil {
+ name := contact.GetDisplayName()
+ phone := extractPhoneFromVCard(contact.GetVcard())
+ switch {
+ case name != "" && phone != "":
+ return fmt.Sprintf("Contact: %s (%s)", name, phone)
+ case name != "":
+ return "Contact: " + name
+ case phone != "":
+ return "Contact: " + phone
+ default:
+ return "Contact shared"
+ }
+ }
+
+ // Check for shared multiple contact cards
+ if contactsArray := msg.GetContactsArrayMessage(); contactsArray != nil {
+ contacts := contactsArray.GetContacts()
+ if len(contacts) > 0 {
+ first := contacts[0]
+ if first != nil {
+ name := first.GetDisplayName()
+ phone := extractPhoneFromVCard(first.GetVcard())
+ switch {
+ case name != "" && phone != "":
+ return fmt.Sprintf("Contacts: %s (%s)", name, phone)
+ case name != "":
+ return "Contacts: " + name
+ case phone != "":
+ return "Contacts: " + phone
+ }
+ }
+ }
+ return "Contacts shared"
+ }
+
return ""
}
diff --git a/src/pkg/utils/whatsapp_test.go b/src/pkg/utils/whatsapp_test.go
index 301c2502..9ece804e 100644
--- a/src/pkg/utils/whatsapp_test.go
+++ b/src/pkg/utils/whatsapp_test.go
@@ -1,6 +1,10 @@
package utils
-import "testing"
+import (
+ "testing"
+
+ "go.mau.fi/whatsmeow/proto/waE2E"
+)
func TestDetermineMediaExtension(t *testing.T) {
tests := []struct {
@@ -56,3 +60,29 @@ func TestDetermineMediaExtension(t *testing.T) {
})
}
}
+
+func TestExtractPhoneFromVCard(t *testing.T) {
+ vcard := "BEGIN:VCARD\nVERSION:3.0\nN:;Alice;;;\nFN:Alice\nTEL;type=Mobile:+62 812 3456 7890\nEND:VCARD"
+
+ got := extractPhoneFromVCard(vcard)
+ if got != "+62 812 3456 7890" {
+ t.Fatalf("extractPhoneFromVCard() = %q, want %q", got, "+62 812 3456 7890")
+ }
+}
+
+func TestExtractMessageTextFromProtoContactMessage(t *testing.T) {
+ name := "Alice"
+ vcard := "BEGIN:VCARD\nVERSION:3.0\nN:;Alice;;;\nFN:Alice\nTEL;type=Mobile:+62 812 3456 7890\nEND:VCARD"
+ msg := &waE2E.Message{
+ ContactMessage: &waE2E.ContactMessage{
+ DisplayName: &name,
+ Vcard: &vcard,
+ },
+ }
+
+ got := ExtractMessageTextFromProto(msg)
+ want := "Contact: Alice (+62 812 3456 7890)"
+ if got != want {
+ t.Fatalf("ExtractMessageTextFromProto() = %q, want %q", got, want)
+ }
+}