forked from minchao/go-mitake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
88 lines (71 loc) · 2.02 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
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")
url, _ := url.Parse("SmSendPost.asp")
url.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(url.String(), "text/plain", strings.NewReader(ini))
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parseMessageResponse(resp.Body)
}
// Send an SMS.
func (c *Client) Send(message Message) (*MessageResponse, error) {
return c.SendBatch([]Message{message})
}
// QueryAccountPoint retrieves your account balance.
func (c *Client) QueryAccountPoint() (int, error) {
url, _ := url.Parse("SmQueryGet.asp")
url.RawQuery = c.buildDefaultQuery().Encode()
resp, err := c.Get(url.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, ","))
url, _ := url.Parse("SmQueryGet.asp")
url.RawQuery = q.Encode()
resp, err := c.Get(url.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, ","))
url, _ := url.Parse("SmCancel.asp")
url.RawQuery = q.Encode()
resp, err := c.Get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
return parseCancelMessageStatusResponse(resp.Body)
}