forked from Topface/smsonline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmsonline.go
82 lines (70 loc) · 1.98 KB
/
smsonline.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
package smsonline
import (
"io/ioutil"
"net/http"
"strings"
)
const (
baseURL = "https://bulk.sms-online.com"
)
// SmsOnline is sms online client
type SmsOnline struct {
username string
secret string
charset string
baseURL string
}
// NewSmsOnlineClient initializes new SmsOnline client
func NewSmsOnlineClient(username, secret, charset string) *SmsOnline {
if charset == "" {
charset = defaultCharset
}
return &SmsOnline{
username: username,
secret: secret,
charset: charset,
baseURL: baseURL,
}
}
// NewSmsOnlineClientCustom initializes new SmsOnline client with custom BaseUrl
func NewSmsOnlineClientCustom(username, secret, charset, url string) *SmsOnline {
client := NewSmsOnlineClient(username, secret, charset)
client.baseURL = url
return client
}
// SendSimpleSms send simple sms
func (client *SmsOnline) SendSimpleSms(from, to, text, charset string) (*SmsResponse, error) {
return client.SendSms(from, to, text, charset, 0, false, false)
}
// SendSms send sms with some additional options such as delay, ack, binary
func (client *SmsOnline) SendSms(from, to, text, charset string, delay int, ack, binary bool) (*SmsResponse, error) {
message := makeSms(from, text, to)
message.setAck(ack)
message.setDelay(delay)
message.setBinaryType(binary)
if charset == "" {
charset = client.charset
}
message.setCharset(charset)
response, err := client.send(message)
if err != nil {
return nil, err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
smsResponse, err := makeResponse(responseBody)
return smsResponse, err
}
// send data to sms online
func (client *SmsOnline) send(m message) (*http.Response, error) {
messageData := m.getMessageData(client.username, client.secret).Encode()
req, err := http.NewRequest("POST", client.baseURL, strings.NewReader(messageData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/text")
return http.DefaultClient.Do(req)
}