-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
118 lines (95 loc) · 2.75 KB
/
api.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
package mitake
import (
"io/ioutil"
"net/url"
"strconv"
"strings"
)
// SendBatch sends multiple SMS.
func (c *Client) SendBatch(messages []Message) (*MessageResponse, error) {
q := c.buildDefaultQuery()
q.Set("encoding", "UTF8")
u, _ := url.Parse("SmSendPost.asp")
u.RawQuery = q.Encode()
var ini string
for i, message := range messages {
ini += "[" + strconv.Itoa(i) + "]\n"
ini += message.ToINI()
}
ini = strings.TrimSpace(ini)
resp, err := c.Post(u.String(), "text/plain", strings.NewReader(ini))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parseMessageResponse(resp.Body)
}
// SendLongMessageBatch sends multiple long message SMS.
func (c *Client) SendLongMessageBatch(messages []Message) (*MessageResponse, error) {
q := c.buildDefaultQuery()
q.Set("Encoding_PostIn", "UTF8")
u := *c.LongMessageBaseURL
u.Path = "SpLmPost"
u.RawQuery = q.Encode()
var ini string
for _, message := range messages {
ini += message.ID + "$$"
ini += message.ToLongMessage()
}
ini = strings.TrimSpace(ini)
resp, err := c.Post(u.String(), "text/plain", strings.NewReader(ini))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parseLongMessageResponse(resp.Body)
}
// Send an SMS.
func (c *Client) Send(message Message) (*MessageResponse, error) {
return c.SendBatch([]Message{message})
}
// SendLongMessage sends a long SMS.
func (c *Client) SendLongMessage(message Message) (*MessageResponse, error) {
return c.SendLongMessageBatch([]Message{message})
}
// QueryAccountPoint retrieves your account balance.
func (c *Client) QueryAccountPoint() (int, error) {
u, _ := url.Parse("SmQueryGet.asp")
u.RawQuery = c.buildDefaultQuery().Encode()
resp, err := c.Get(u.String())
if err != nil {
return 0, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
return strconv.Atoi(strings.Split(string(data), "=")[1])
}
// QueryMessageStatus fetch the status of specific messages.
func (c *Client) QueryMessageStatus(messageIds []string) (*MessageStatusResponse, error) {
q := c.buildDefaultQuery()
q.Set("msgid", strings.Join(messageIds, ","))
u, _ := url.Parse("SmQueryGet.asp")
u.RawQuery = q.Encode()
resp, err := c.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parseMessageStatusResponse(resp.Body)
}
// CancelMessageStatus cancel the specific messages.
func (c *Client) CancelMessageStatus(messageIds []string) (*MessageStatusResponse, error) {
q := c.buildDefaultQuery()
q.Set("msgid", strings.Join(messageIds, ","))
u, _ := url.Parse("SmCancel.asp")
u.RawQuery = q.Encode()
resp, err := c.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parseCancelMessageStatusResponse(resp.Body)
}