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 send link storage (#661)

Júlio Mühlbauer4ef5c4fa4816
diff --git a/src/usecase/send.go b/src/usecase/send.go
index b415afda..91c90377 100644
--- a/src/usecase/send.go
+++ b/src/usecase/send.go
@@ -983,9 +983,11 @@ func (service serviceSend) SendLink(ctx context.Context, request domainSend.Link
logrus.Debugf("Image dimensions: Square image or dimensions not available")
}
+ messageText := buildLinkMessageText(request.Caption, request.Link)
+
// Create the message
msg := &waE2E.Message{ExtendedTextMessage: &waE2E.ExtendedTextMessage{
- Text: proto.String(fmt.Sprintf("%s\n%s", request.Caption, request.Link)),
+ Text: proto.String(messageText),
Title: proto.String(metadata.Title),
MatchedText: proto.String(request.Link),
Description: proto.String(metadata.Description),
@@ -1027,11 +1029,7 @@ func (service serviceSend) SendLink(ctx context.Context, request domainSend.Link
}
}
- content := "🔗 " + request.Link
- if request.Caption != "" {
- content = "🔗 " + request.Caption
- }
- ts, err := service.wrapSendMessage(ctx, client, dataWaRecipient, msg, content)
+ ts, err := service.wrapSendMessage(ctx, client, dataWaRecipient, msg, messageText)
if err != nil {
return response, err
}
@@ -1041,6 +1039,17 @@ func (service serviceSend) SendLink(ctx context.Context, request domainSend.Link
return response, nil
}
+func buildLinkMessageText(caption, link string) string {
+ caption = strings.TrimSpace(caption)
+ link = strings.TrimSpace(link)
+
+ if caption == "" {
+ return link
+ }
+
+ return fmt.Sprintf("%s\n%s", caption, link)
+}
+
func (service serviceSend) SendLocation(ctx context.Context, request domainSend.LocationRequest) (response domainSend.GenericResponse, err error) {
err = validations.ValidateSendLocation(ctx, request)
if err != nil {
diff --git a/src/usecase/send_test.go b/src/usecase/send_test.go
index 81c283fb..21a0e67c 100644
--- a/src/usecase/send_test.go
+++ b/src/usecase/send_test.go
@@ -85,6 +85,42 @@ func TestResolveDocumentMIME(t *testing.T) {
}
}
+func TestBuildLinkMessageText(t *testing.T) {
+ tests := []struct {
+ name string
+ caption string
+ link string
+ want string
+ }{
+ {
+ name: "returns link when caption is empty",
+ link: "https://example.com",
+ want: "https://example.com",
+ },
+ {
+ name: "joins caption and link with newline",
+ caption: "Check this out",
+ link: "https://example.com",
+ want: "Check this out\nhttps://example.com",
+ },
+ {
+ name: "ignores blank caption",
+ caption: " ",
+ link: "https://example.com",
+ want: "https://example.com",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := buildLinkMessageText(tt.caption, tt.link)
+ if got != tt.want {
+ t.Fatalf("buildLinkMessageText() = %q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
func TestNormalizeSendErrorMapsReachoutTimelock(t *testing.T) {
err := normalizeSendError(errors.Join(whatsmeow.ErrServerReturnedError, errors.New("server returned error 463")))