forked from levpay/infobip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
127 lines (111 loc) · 2.98 KB
/
message.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package infobip
import "regexp"
// BulkMessage contains the body request for multiple messages
type BulkMessage struct {
ID string `json:"bulkId,omitempty"`
Messages []Message `json:"messages"`
}
// Message contains the body request
type Message struct {
From string `json:"from,omitempty"`
Destinations []Destination `json:"destinations,omitempty"`
To string `json:"to,omitempty"`
Text string `json:"text"`
Transliteration string `json:"transliteration,omitempty"`
LanguageCode string `json:"languageCode,omitempty"`
NotifyURL string `json:"notifyUrl,omitempty"`
}
// Destination contains the recipient
type Destination struct {
ID string `json:"messageId,omitempty"`
To string `json:"to"`
}
// MessageResponse body response
type MessageResponse struct {
BulkID string `json:"bulkId,omitempty"`
Messages []MessageInfo `json:"messages"`
}
// MessageInfo ...
type MessageInfo struct {
ID string `json:"messageId"`
To string `json:"to"`
Status MessageStatus `json:"status"`
SMSCount int `json:"smsCount"`
}
// MessageStatus ...
type MessageStatus struct {
ID int `json:"id"`
Action string `json:"action,omitempty"`
GroupID int `json:"groupId"`
GroupName string `json:"groupName"`
Name string `json:"name"`
Description string `json:"description"`
}
// MessageStatusWithID ...
type MessageStatusWithID struct {
BulkID string `json:"bulkId,omitempty"`
ID string `json:"messageId"`
To string `json:"to"`
Status MessageStatus `json:"status"`
}
// MessageStatusResponse ...
type MessageStatusResponse struct {
Results []MessageStatusWithID `json:"results"`
}
// Validate validates the entire message values
func (b BulkMessage) Validate() (err error) {
for _, m := range b.Messages {
if err = m.Validate(); err != nil {
break
}
}
return
}
// Validate validates the body request values
func (m Message) Validate() (err error) {
if err = m.validateFromValue(); err != nil {
return
}
if err = m.validateDestinationValues(); err != nil {
return
}
err = m.validateToValue()
return
}
func (m Message) validateFromValue() (err error) {
if isNumeric(m.From) && !isValidRange(m.From, 3, 14) {
err = ErrForFromNonAlphanumeric
return
}
if !isValidRange(m.From, 3, 13) {
err = ErrForFromAlphanumeric
return
}
return
}
func (m Message) validateDestinationValues() (err error) {
for _, d := range m.Destinations {
if isNumeric(d.To) && !isValidRange(d.To, 3, 14) {
err = ErrForDestinationNonAlphanumeric
break
}
}
return
}
func (m Message) validateToValue() (err error) {
if m.To == "" {
return
}
if isNumeric(m.To) && !isValidRange(m.To, 3, 14) {
err = ErrForToNonAlphanumeric
return
}
return
}
func isNumeric(s string) bool {
return regexp.MustCompile(`^[\d]*$`).MatchString(s)
}
func isValidRange(s string, a, b int) bool {
l := len(s)
return l > a && l <= b
}