Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Added attachmenturls to the BulkMessage struct for csv support and a basic file type check

Jake Daynes34b2cfb5d67f
diff --git a/api/pkg/requests/bulk_message_request.go b/api/pkg/requests/bulk_message_request.go
index ffb3f35c..0ab6c024 100644
--- a/api/pkg/requests/bulk_message_request.go
+++ b/api/pkg/requests/bulk_message_request.go
@@ -18,6 +18,7 @@ type BulkMessage struct {
ToPhoneNumber string `csv:"ToPhoneNumber"`
Content string `csv:"Content"`
SendTime *time.Time `csv:"SendTime(optional)"`
+ AttachmentURLs string `csv:"AttachmentURLs(optional)" validate:"optional"` // Comma separated list of URLs
}
// Sanitize sets defaults to BulkMessage
@@ -25,12 +26,43 @@ func (input *BulkMessage) Sanitize() *BulkMessage {
input.ToPhoneNumber = input.sanitizeAddress(input.ToPhoneNumber)
input.Content = strings.TrimSpace(input.Content)
input.FromPhoneNumber = input.sanitizeAddress(input.FromPhoneNumber)
+ input.AttachmentURLs = strings.TrimSpace(input.AttachmentURLs)
return input
}
// ToMessageSendParams converts BulkMessage to services.MessageSendParams
func (input *BulkMessage) ToMessageSendParams(userID entities.UserID, requestID uuid.UUID, source string) services.MessageSendParams {
from, _ := phonenumbers.Parse(input.FromPhoneNumber, phonenumbers.UNKNOWN_REGION)
+
+ var attachments []entities.MessageAttachment
+ if input.AttachmentURLs != "" {
+ urls := strings.Split(input.AttachmentURLs, ",")
+ for _, u := range urls {
+ cleanURL := strings.TrimSpace(u)
+ if cleanURL == "" {
+ continue
+ }
+
+ // 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
+ contentType := "application/octet-stream"
+ lowerURL := strings.ToLower(cleanURL)
+ if strings.HasSuffix(lowerURL, ".jpg") || strings.HasSuffix(lowerURL, ".jpeg") {
+ contentType = "image/jpeg"
+ } else if strings.HasSuffix(lowerURL, ".png") {
+ contentType = "image/png"
+ } else if strings.HasSuffix(lowerURL, ".gif") {
+ contentType = "image/gif"
+ } else if strings.HasSuffix(lowerURL, ".mp4") {
+ contentType = "video/mp4"
+ }
+
+ attachments = append(attachments, entities.MessageAttachment{
+ ContentType: contentType,
+ URL: cleanURL,
+ })
+ }
+ }
+
return services.MessageSendParams{
Source: source,
Owner: from,
@@ -40,5 +72,6 @@ func (input *BulkMessage) ToMessageSendParams(userID entities.UserID, requestID
RequestReceivedAt: time.Now().UTC(),
Contact: input.sanitizeAddress(input.ToPhoneNumber),
Content: input.Content,
+ Attachments: attachments,
}
}