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.

Show phone number when sending a contact in messages (#646)

Júlio Mühlbauer422d1d0d7abe
diff --git a/src/pkg/utils/whatsapp.go b/src/pkg/utils/whatsapp.go
index d06627a4..513f02e2 100644
--- a/src/pkg/utils/whatsapp.go
+++ b/src/pkg/utils/whatsapp.go
@@ -57,9 +57,38 @@ func resolveKnownDocumentExtension(mimeType string) (string, bool) {
}
// ExtractPhoneFromVCard returns the first phone number found in a vCard's TEL field.
-// Splits on "\n" — TrimSpace handles the trailing "\r" from CRLF-terminated vCards.
func ExtractPhoneFromVCard(vcard string) string {
- for _, line := range strings.Split(vcard, "\n") {
+ if vcard == "" {
+ return ""
+ }
+
+ normalized := strings.ReplaceAll(vcard, "\r\n", "\n")
+ normalized = strings.ReplaceAll(normalized, "\r", "\n")
+
+ var lines []string
+ var current strings.Builder
+ for _, rawLine := range strings.Split(normalized, "\n") {
+ line := strings.TrimSpace(rawLine)
+ if line == "" {
+ continue
+ }
+ if strings.HasPrefix(rawLine, " ") || strings.HasPrefix(rawLine, "\t") {
+ if current.Len() > 0 {
+ current.WriteString(line)
+ }
+ continue
+ }
+ if current.Len() > 0 {
+ lines = append(lines, current.String())
+ current.Reset()
+ }
+ current.WriteString(line)
+ }
+ if current.Len() > 0 {
+ lines = append(lines, current.String())
+ }
+
+ for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(strings.ToUpper(line), "TEL") {
if idx := strings.LastIndex(line, ":"); idx >= 0 {
@@ -73,6 +102,9 @@ func ExtractPhoneFromVCard(vcard string) string {
// FormatContactSummary builds a one-liner for a shared contact card.
// Pass plural=true for ContactsArrayMessage to use the "Contacts" prefix.
func FormatContactSummary(name, phone string, plural bool) string {
+ name = strings.TrimSpace(name)
+ phone = strings.TrimSpace(phone)
+
prefix := "Contact"
if plural {
prefix = "Contacts"
diff --git a/src/pkg/utils/whatsapp_test.go b/src/pkg/utils/whatsapp_test.go
index 77c66eee..9906873c 100644
--- a/src/pkg/utils/whatsapp_test.go
+++ b/src/pkg/utils/whatsapp_test.go
@@ -77,6 +77,11 @@ func TestExtractPhoneFromVCard(t *testing.T) {
vcard: "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Bob\r\nTEL:+1 555 0100\r\nEND:VCARD",
want: "+1 555 0100",
},
+ {
+ name: "FoldedLine",
+ vcard: "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Julio\r\nTEL;type=CELL;waid=5511998913283:\r\n +5511998913283\r\nEND:VCARD",
+ want: "+5511998913283",
+ },
{
name: "NoTelLine",
vcard: "BEGIN:VCARD\nVERSION:3.0\nFN:Carol\nEND:VCARD",
@@ -111,6 +116,7 @@ func TestFormatContactSummary(t *testing.T) {
{name: "SingleNameOnly", dName: "Alice", phone: "", plural: false, want: "Contact: Alice"},
{name: "SinglePhoneOnly", dName: "", phone: "+62 812", plural: false, want: "Contact: +62 812"},
{name: "SingleEmpty", dName: "", phone: "", plural: false, want: "Contact shared"},
+ {name: "SingleWhitespaceOnly", dName: " ", phone: " ", plural: false, want: "Contact shared"},
{name: "PluralNameAndPhone", dName: "Alice", phone: "+62 812", plural: true, want: "Contacts: Alice (+62 812)"},
{name: "PluralEmpty", dName: "", phone: "", plural: true, want: "Contacts shared"},
}
@@ -126,19 +132,62 @@ func TestFormatContactSummary(t *testing.T) {
}
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,
+ phoneVCard := "BEGIN:VCARD\r\nVERSION:3.0\r\nFN:Alice\r\nTEL;type=Mobile:\r\n +62 812 3456 7890\r\nEND:VCARD"
+
+ tests := []struct {
+ name string
+ msg *waE2E.Message
+ want string
+ }{
+ {
+ name: "NameAndPhone",
+ msg: &waE2E.Message{
+ ContactMessage: &waE2E.ContactMessage{
+ DisplayName: strPtr("Alice"),
+ Vcard: strPtr(phoneVCard),
+ },
+ },
+ want: "Contact: Alice (+62 812 3456 7890)",
+ },
+ {
+ name: "NameOnly",
+ msg: &waE2E.Message{
+ ContactMessage: &waE2E.ContactMessage{
+ DisplayName: strPtr("Alice"),
+ Vcard: strPtr(""),
+ },
+ },
+ want: "Contact: Alice",
+ },
+ {
+ name: "PhoneOnly",
+ msg: &waE2E.Message{
+ ContactMessage: &waE2E.ContactMessage{
+ DisplayName: strPtr(""),
+ Vcard: strPtr(phoneVCard),
+ },
+ },
+ want: "Contact: +62 812 3456 7890",
+ },
+ {
+ name: "Neither",
+ msg: &waE2E.Message{
+ ContactMessage: &waE2E.ContactMessage{
+ DisplayName: strPtr(""),
+ Vcard: strPtr(""),
+ },
+ },
+ want: "Contact shared",
},
}
- got := ExtractMessageTextFromProto(msg)
- want := "Contact: Alice (+62 812 3456 7890)"
- if got != want {
- t.Fatalf("ExtractMessageTextFromProto() = %q, want %q", got, want)
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := ExtractMessageTextFromProto(tt.msg)
+ if got != tt.want {
+ t.Fatalf("ExtractMessageTextFromProto() = %q, want %q", got, tt.want)
+ }
+ })
}
}
@@ -192,3 +241,7 @@ func TestExtractMessageTextFromProtoContactsArrayMessage(t *testing.T) {
})
}
}
+
+func strPtr(value string) *string {
+ return &value
+}
diff --git a/src/usecase/send.go b/src/usecase/send.go
index ac93c6b5..24578f70 100644
--- a/src/usecase/send.go
+++ b/src/usecase/send.go
@@ -919,10 +919,12 @@ func (service serviceSend) SendContact(ctx context.Context, request domainSend.C
return response, err
}
+ contactName := strings.TrimSpace(request.ContactName)
+ contactPhone := utils.CleanPhoneForWhatsApp(request.ContactPhone)
msgVCard := fmt.Sprintf("BEGIN:VCARD\nVERSION:3.0\nN:;%v;;;\nFN:%v\nTEL;type=CELL;waid=%v:+%v\nEND:VCARD",
- request.ContactName, request.ContactName, request.ContactPhone, request.ContactPhone)
+ contactName, contactName, contactPhone, contactPhone)
msg := &waE2E.Message{ContactMessage: &waE2E.ContactMessage{
- DisplayName: proto.String(request.ContactName),
+ DisplayName: proto.String(contactName),
Vcard: proto.String(msgVCard),
}}
@@ -940,7 +942,10 @@ func (service serviceSend) SendContact(ctx context.Context, request domainSend.C
msg.ContactMessage.ContextInfo.Expiration = proto.Uint32(uint32(*request.BaseRequest.Duration))
}
- content := "👤 " + request.ContactName
+ content := "👤 " + contactName
+ if contactPhone != "" {
+ content = fmt.Sprintf("👤 %s (+%s)", contactName, contactPhone)
+ }
ts, err := service.wrapSendMessage(ctx, client, dataWaRecipient, msg, content)
if err != nil {
diff --git a/src/validations/send_validation.go b/src/validations/send_validation.go
index e744c316..09548f9f 100644
--- a/src/validations/send_validation.go
+++ b/src/validations/send_validation.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sort"
+ "strings"
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
domainSend "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/send"
@@ -39,15 +40,18 @@ func validateDuration(dur *int) error {
// validatePhoneNumber validates that the phone number is in international format (not starting with 0)
func validatePhoneNumber(phone string) error {
- if phone == "" {
+ phoneNumber := strings.TrimSpace(phone)
+ if phoneNumber == "" {
return pkgError.ValidationError("phone number cannot be empty")
}
// Remove + prefix if present for validation
- phoneNumber := phone
if len(phoneNumber) > 0 && phoneNumber[0] == '+' {
phoneNumber = phoneNumber[1:]
}
+ if phoneNumber == "" {
+ return pkgError.ValidationError("phone number cannot be empty")
+ }
// Check if phone number starts with 0 (indicating local format)
if len(phoneNumber) > 0 && phoneNumber[0] == '0' {
diff --git a/src/validations/send_validation_test.go b/src/validations/send_validation_test.go
index 7c2bfe79..bc1eb4ed 100644
--- a/src/validations/send_validation_test.go
+++ b/src/validations/send_validation_test.go
@@ -521,6 +521,17 @@ func TestValidateSendContact(t *testing.T) {
}},
err: pkgError.ValidationError("contact_phone: cannot be blank."),
},
+ {
+ name: "should error with plus-only contact phone",
+ args: args{request: domainSend.ContactRequest{
+ BaseRequest: domainSend.BaseRequest{
+ Phone: "[email protected]",
+ },
+ ContactName: "Aldino",
+ ContactPhone: "+",
+ }},
+ err: pkgError.ValidationError("contact phone number cannot be empty"),
+ },
}
for _, tt := range tests {