Skip to content

Commit 34b2cfb

Browse files
Added attachmenturls to the BulkMessage struct for csv support and a basic file type check
1 parent 53112cb commit 34b2cfb

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

api/pkg/requests/bulk_message_request.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,51 @@ type BulkMessage struct {
1818
ToPhoneNumber string `csv:"ToPhoneNumber"`
1919
Content string `csv:"Content"`
2020
SendTime *time.Time `csv:"SendTime(optional)"`
21+
AttachmentURLs string `csv:"AttachmentURLs(optional)" validate:"optional"` // Comma separated list of URLs
2122
}
2223

2324
// Sanitize sets defaults to BulkMessage
2425
func (input *BulkMessage) Sanitize() *BulkMessage {
2526
input.ToPhoneNumber = input.sanitizeAddress(input.ToPhoneNumber)
2627
input.Content = strings.TrimSpace(input.Content)
2728
input.FromPhoneNumber = input.sanitizeAddress(input.FromPhoneNumber)
29+
input.AttachmentURLs = strings.TrimSpace(input.AttachmentURLs)
2830
return input
2931
}
3032

3133
// ToMessageSendParams converts BulkMessage to services.MessageSendParams
3234
func (input *BulkMessage) ToMessageSendParams(userID entities.UserID, requestID uuid.UUID, source string) services.MessageSendParams {
3335
from, _ := phonenumbers.Parse(input.FromPhoneNumber, phonenumbers.UNKNOWN_REGION)
36+
37+
var attachments []entities.MessageAttachment
38+
if input.AttachmentURLs != "" {
39+
urls := strings.Split(input.AttachmentURLs, ",")
40+
for _, u := range urls {
41+
cleanURL := strings.TrimSpace(u)
42+
if cleanURL == "" {
43+
continue
44+
}
45+
46+
// Since there's no easy way to set a type in the CSV, defaulting to octet-stream and then just checking the file extension in the URL
47+
contentType := "application/octet-stream"
48+
lowerURL := strings.ToLower(cleanURL)
49+
if strings.HasSuffix(lowerURL, ".jpg") || strings.HasSuffix(lowerURL, ".jpeg") {
50+
contentType = "image/jpeg"
51+
} else if strings.HasSuffix(lowerURL, ".png") {
52+
contentType = "image/png"
53+
} else if strings.HasSuffix(lowerURL, ".gif") {
54+
contentType = "image/gif"
55+
} else if strings.HasSuffix(lowerURL, ".mp4") {
56+
contentType = "video/mp4"
57+
}
58+
59+
attachments = append(attachments, entities.MessageAttachment{
60+
ContentType: contentType,
61+
URL: cleanURL,
62+
})
63+
}
64+
}
65+
3466
return services.MessageSendParams{
3567
Source: source,
3668
Owner: from,
@@ -40,5 +72,6 @@ func (input *BulkMessage) ToMessageSendParams(userID entities.UserID, requestID
4072
RequestReceivedAt: time.Now().UTC(),
4173
Contact: input.sanitizeAddress(input.ToPhoneNumber),
4274
Content: input.Content,
75+
Attachments: attachments,
4376
}
4477
}

0 commit comments

Comments
 (0)