Skip to content

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

feat: add attachment fields to request, params, and event structs

Acho Arnold578c0de747c0
diff --git a/api/pkg/events/message_phone_received_event.go b/api/pkg/events/message_phone_received_event.go
index abe3a014..04dd6c2e 100644
--- a/api/pkg/events/message_phone_received_event.go
+++ b/api/pkg/events/message_phone_received_event.go
@@ -13,12 +13,13 @@ const EventTypeMessagePhoneReceived = "message.phone.received"
// MessagePhoneReceivedPayload is the payload of the EventTypeMessagePhoneReceived event
type MessagePhoneReceivedPayload struct {
- MessageID uuid.UUID `json:"message_id"`
- UserID entities.UserID `json:"user_id"`
- Owner string `json:"owner"`
- Encrypted bool `json:"encrypted"`
- Contact string `json:"contact"`
- Timestamp time.Time `json:"timestamp"`
- Content string `json:"content"`
- SIM entities.SIM `json:"sim"`
+ MessageID uuid.UUID `json:"message_id"`
+ UserID entities.UserID `json:"user_id"`
+ Owner string `json:"owner"`
+ Encrypted bool `json:"encrypted"`
+ Contact string `json:"contact"`
+ Timestamp time.Time `json:"timestamp"`
+ Content string `json:"content"`
+ SIM entities.SIM `json:"sim"`
+ Attachments []string `json:"attachments"`
}
diff --git a/api/pkg/requests/message_receive_request.go b/api/pkg/requests/message_receive_request.go
index f592761c..be405101 100644
--- a/api/pkg/requests/message_receive_request.go
+++ b/api/pkg/requests/message_receive_request.go
@@ -11,6 +11,16 @@ import (
"github.com/NdoleStudio/httpsms/pkg/services"
)
+// MessageAttachment represents a single MMS attachment in a receive request
+type MessageAttachment struct {
+ // Name is the original filename of the attachment
+ Name string `json:"name" example:"photo.jpg"`
+ // ContentType is the MIME type of the attachment
+ ContentType string `json:"content_type" example:"image/jpeg"`
+ // Content is the base64-encoded attachment data
+ Content string `json:"content" example:"base64data..."`
+}
+
// MessageReceive is the payload for sending and SMS message
type MessageReceive struct {
request
@@ -23,6 +33,8 @@ type MessageReceive struct {
SIM entities.SIM `json:"sim" example:"SIM1"`
// Timestamp is the time when the event was emitted, Please send the timestamp in UTC with as much precision as possible
Timestamp time.Time `json:"timestamp" example:"2022-06-05T14:26:09.527976+03:00"`
+ // Attachments is the list of MMS attachments received with the message
+ Attachments []MessageAttachment `json:"attachments"`
}
// Sanitize sets defaults to MessageReceive
@@ -38,14 +50,25 @@ func (input *MessageReceive) Sanitize() MessageReceive {
// ToMessageReceiveParams converts MessageReceive to services.MessageReceiveParams
func (input *MessageReceive) ToMessageReceiveParams(userID entities.UserID, source string) *services.MessageReceiveParams {
phone, _ := phonenumbers.Parse(input.To, phonenumbers.UNKNOWN_REGION)
+
+ attachments := make([]services.ServiceAttachment, len(input.Attachments))
+ for i, a := range input.Attachments {
+ attachments[i] = services.ServiceAttachment{
+ Name: a.Name,
+ ContentType: a.ContentType,
+ Content: a.Content,
+ }
+ }
+
return &services.MessageReceiveParams{
- Source: source,
- Contact: input.From,
- UserID: userID,
- Timestamp: input.Timestamp,
- Encrypted: input.Encrypted,
- Owner: *phone,
- Content: input.Content,
- SIM: input.SIM,
+ Source: source,
+ Contact: input.From,
+ UserID: userID,
+ Timestamp: input.Timestamp,
+ Encrypted: input.Encrypted,
+ Owner: *phone,
+ Content: input.Content,
+ SIM: input.SIM,
+ Attachments: attachments,
}
}
diff --git a/api/pkg/services/message_service.go b/api/pkg/services/message_service.go
index a667882b..efddaee8 100644
--- a/api/pkg/services/message_service.go
+++ b/api/pkg/services/message_service.go
@@ -19,6 +19,13 @@ import (
"github.com/NdoleStudio/httpsms/pkg/telemetry"
)
+// ServiceAttachment represents attachment data passed to the service layer
+type ServiceAttachment struct {
+ Name string
+ ContentType string
+ Content string // base64-encoded
+}
+
// MessageService is handles message requests
type MessageService struct {
service
@@ -289,14 +296,15 @@ func (service *MessageService) StoreEvent(ctx context.Context, message *entities
// MessageReceiveParams parameters registering a message event
type MessageReceiveParams struct {
- Contact string
- UserID entities.UserID
- Owner phonenumbers.PhoneNumber
- Content string
- SIM entities.SIM
- Timestamp time.Time
- Encrypted bool
- Source string
+ Contact string
+ UserID entities.UserID
+ Owner phonenumbers.PhoneNumber
+ Content string
+ SIM entities.SIM
+ Timestamp time.Time
+ Encrypted bool
+ Source string
+ Attachments []ServiceAttachment
}
// ReceiveMessage handles message received by a mobile phone