-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.go
192 lines (178 loc) · 4.63 KB
/
sms.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package smscountry
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// Defines the different endpoints in SMS country
const (
SMSCountryScheme = `https`
SMSCountryHost = `api.smscountry.com`
MessagePath = `/SMSCwebservice_bulk.aspx`
BulMessagePath = `/SMSCwebservice_bulk.aspx`
MultiMessagePath = `/SMSCWebservice_MultiMessages.asp`
BulkReportPath = `/smscwebservices_bulk_reports.aspx`
BalancePath = `/SMSCwebservice_User_GetBal.asp`
)
// Defines the keys in requests
const (
User = "User"
Password = "passwd"
SenderID = "sid"
MobileNumber = "mobilenumber"
Message = "message"
MessageType = "mtype"
DeliveryReport = "dr"
MultiMessage = "mno_msg"
)
// Delivery report values
const (
SendDeliveryReport = "Y"
DontSendDeliveryReport = "N"
)
// Message types
const (
NormalMessage = "N"
UnicodeMessage = "OL"
PictureMessage = "P"
Logo = "L"
FlashMessage = "F"
WAPPush = "WP" // Specify the wap url in the parameter wap_url
LongSMS = "LS" // GPRS based
Ringtone = "R"
)
// Defines the different errors
var (
ErrEmptyResponse = errors.New("Empty response from server")
)
// Client defines a sms country client
type Client struct {
User string
Password string
balanceEnquiryURL string
httpClient *http.Client
}
// Balance returns the balance available for the user
func (s Client) Balance() (bal float64, err error) {
if resp, er := s.httpClient.Get(s.balanceEnquiryURL); er != nil {
err = er
} else {
if resp.Body == nil {
err = ErrEmptyResponse
}
defer func(e *error) {
if err := resp.Body.Close(); err != nil {
*e = err
}
}(&err)
if r, er := ioutil.ReadAll(resp.Body); er != nil {
err = er
} else {
if v, er := strconv.ParseFloat(strings.SplitN(string(r), " ", 2)[0], 64); er != nil {
err = er
} else {
bal = v
}
}
}
return bal, err
}
// NewSender returns a new sender
func (s Client) NewSender(senderID string) *Sender {
return &Sender{
Client: s,
SenderID: senderID,
}
}
// Sender defines a sender
type Sender struct {
Client Client
SenderID string
}
func (s *Sender) sendMessage(path string, content io.Reader) (err error) {
if resp, er := s.Client.httpClient.Post((&url.URL{
Host: SMSCountryHost,
Path: path,
Scheme: SMSCountryScheme,
}).String(), "application/x-www-form-urlencoded", content); er != nil {
err = er
} else {
if resp.Body == nil {
return ErrEmptyResponse
}
defer func(e *error) {
if err := resp.Body.Close(); err != nil {
if e == nil || *e == nil {
*e = err
} else {
*e = fmt.Errorf("Error: %v, Body close error: %v", *e, err)
}
}
}(&err)
if r, er := ioutil.ReadAll(resp.Body); er != nil {
err = er
} else {
response := strings.TrimSpace(string(r))
if !strings.HasPrefix(response, "OK:") && response != "SMS message(s) sent" {
return fmt.Errorf("Error sending SMS. Response: %s", response)
}
}
}
return nil
}
// SendSMS sends an SMS to the recipient
func (s *Sender) SendSMS(message, mobileNumber string, deliveryReport bool) (err error) {
query := url.Values{}
query.Add(User, s.Client.User)
query.Add(Password, s.Client.Password)
query.Add(SenderID, s.SenderID)
query.Add(MobileNumber, mobileNumber)
query.Add(Message, message)
query.Add(MessageType, NormalMessage)
if deliveryReport {
query.Add(DeliveryReport, SendDeliveryReport)
} else {
query.Add(DeliveryReport, DontSendDeliveryReport)
}
return s.sendMessage(MessagePath, strings.NewReader(query.Encode()))
}
// SendBulkSMS sends an SMS to the recipient
func (s *Sender) SendBulkSMS(messages, mobileNumbers []string, deliveryReport bool) (err error) {
msgBuf := new(bytes.Buffer)
msgBuf.WriteString(fmt.Sprintf("%s^%s", mobileNumbers[0], messages[0]))
for i := 1; i < len(messages); i++ {
msgBuf.WriteString(fmt.Sprintf("~%s^%s", mobileNumbers[i], messages[i]))
}
query := url.Values{}
query.Add(User, s.Client.User)
query.Add(Password, s.Client.Password)
query.Add(SenderID, s.SenderID)
query.Add(MultiMessage, msgBuf.String())
query.Add(MessageType, NormalMessage)
return s.sendMessage(MultiMessagePath, strings.NewReader(query.Encode()))
}
// New returns a new instance of Client
func New(user, password string) *Client {
s := &Client{
User: user,
Password: password,
balanceEnquiryURL: (&url.URL{
Host: SMSCountryHost,
Path: BalancePath,
Scheme: SMSCountryScheme,
RawQuery: url.Values{
"User": {user},
"passwd": {password},
}.Encode(),
}).String(),
httpClient: &http.Client{Timeout: time.Duration(time.Minute)},
}
return s
}