forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_bulk_send_request.go
More file actions
59 lines (48 loc) · 1.92 KB
/
message_bulk_send_request.go
File metadata and controls
59 lines (48 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package requests
import (
"time"
"github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/nyaruka/phonenumbers"
"github.com/NdoleStudio/httpsms/pkg/services"
)
// MessageBulkSend is the payload for sending bulk SMS messages
type MessageBulkSend struct {
request
From string `json:"from" example:"+18005550199"`
To []string `json:"to" example:"+18005550100,+18005550100"`
Content string `json:"content" example:"This is a sample text message"`
// Encrypted is used to determine if the content is end-to-end encrypted. Make sure to set the encryption key on the httpSMS mobile app
Encrypted bool `json:"encrypted" example:"false"`
// RequestID is an optional parameter used to track a request from the client's perspective
RequestID string `json:"request_id" example:"153554b5-ae44-44a0-8f4f-7bbac5657ad4" validate:"optional"`
}
// Sanitize sets defaults to MessageReceive
func (input *MessageBulkSend) Sanitize() MessageBulkSend {
var to []string
for _, address := range input.To {
to = append(to, input.sanitizeAddress(address))
}
input.To = to
input.From = input.sanitizeAddress(input.From)
return *input
}
// ToMessageSendParams converts MessageSend to services.MessageSendParams
func (input *MessageBulkSend) ToMessageSendParams(userID entities.UserID, source string) []services.MessageSendParams {
from, _ := phonenumbers.Parse(input.From, phonenumbers.UNKNOWN_REGION)
var result []services.MessageSendParams
for index, to := range input.To {
sendAt := time.Now().UTC().Add(time.Duration(index) * time.Second)
result = append(result, services.MessageSendParams{
Source: source,
Owner: from,
Encrypted: input.Encrypted,
RequestID: input.sanitizeStringPointer(input.RequestID),
UserID: userID,
RequestReceivedAt: time.Now().UTC(),
Contact: to,
SendAt: &sendAt,
Content: input.Content,
})
}
return result
}