-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelemsg.go
149 lines (113 loc) · 3.28 KB
/
telemsg.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package telemsg
import "encoding/json"
// import "fmt"
import "net/http"
import "bytes"
import "io/ioutil"
import "errors"
const (
serverUrl = "https://rest.telemessage.com/rest/message/send"
AccountClass = "telemessage.web.services.AuthenticationDetails"
MsgClass = "telemessage.web.services.Message"
RecipientClass = "telemessage.web.services.Recipient"
MsgType = "SMS"
MsgDescription = "-" // an optinal value | read provider docs for more info
)
type Sms struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Body string `json:"textMessage,omitempty"`
To string `json:"-"`
Class string `json:"class,omitempty"`
Recipients []SmsRecipient `json:"recipients,omitempty"`
}
type SmsRecipient struct {
Value string `json:"value,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Class string `json:"class,omitempty"`
}
type StatusResponse struct {
Ok bool `json:"-"`
Class string `json:"class,omitempty"`
MsgId int `json:"messageID,omitempty"`
MsgKey string `json:"messageKey,omitempty"`
ResultCode int `json:"resultCode,omitempty"`
ResultDescription string `json:"resultDescription,omitempty"`
}
func NewClient(user string, pass string) *Sms {
sms := new(Sms)
sms.Username = user
sms.Password = pass
return sms
}
func (this *Sms) NewMsg(to string, body string) *Sms {
this.To = to
this.Body = body
return this
}
func (this *Sms) Send() (StatusResponse, error) {
var statres StatusResponse
statres.Ok = false
jsonByte, eror := this.generateJson()
if eror != nil {
return statres, eror
}
req, err := http.NewRequest("POST", serverUrl, bytes.NewBuffer(jsonByte))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return statres, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var status []StatusResponse
if err := json.Unmarshal(body, &status); err != nil {
return statres, err
}
if len(status) > 0 {
s := status[0]
if s.ResultCode == 100 || s.ResultCode == 0 {
s.Ok = true
return s, nil
} else {
return s, errors.New("FAILED: " + string(s.ResultCode))
}
} else {
return statres, errors.New("FAILED: no response")
}
}
func (this *Sms) generateJson() ([]byte, error) {
var objSms [2]Sms
var rec SmsRecipient
rec.Class = RecipientClass
rec.Description = MsgDescription
rec.Type = MsgType
rec.Value = this.To
this.Recipients = append(this.Recipients, rec)
objSms[0] = Sms{
Class: AccountClass,
Username: this.Username,
Password: this.Password,
}
objSms[1] = Sms{
Class: MsgClass,
Body: this.Body,
Recipients: this.Recipients,
}
d, err := json.Marshal(objSms)
return d, err
}
func (this *Sms) AddRecipients(recipientList []string) *Sms {
rLen := len(recipientList)
recipients := make([]SmsRecipient, rLen)
for i, record := range recipientList {
recipients[i].Class = RecipientClass
recipients[i].Description = MsgDescription
recipients[i].Type = MsgType
recipients[i].Value = record
}
this.Recipients = append(this.Recipients, recipients...)
return this
}