-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
105 lines (92 loc) · 2.12 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
package main
import (
"errors"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
func GetSMSBalance() (int, error) {
if !Config.SMSOn {
return 0, nil
}
resp, err := http.PostForm(
Config.SMSServer,
url.Values{
"username": {Config.SMSUser},
"password": {Config.SMSPasswd},
"action": {"balance"},
})
if err != nil {
log.Println("HTTP Post Error", err.Error())
return 0, err
}
/* log.Println(resp)
log.Println("status", resp.Status)
log.Println("status code", resp.StatusCode)
log.Println("proto", resp.Proto)
log.Println("major", resp.ProtoMajor)
log.Println("minor", resp.ProtoMinor)
log.Println("header", resp.Header)
log.Println("content length", resp.ContentLength)
log.Println("transfer", resp.TransferEncoding)
log.Println("trailer", resp.Trailer)
log.Println("close", resp.Close)
log.Println("req", resp.Request)
log.Println("tls", resp.TLS)
log.Println("body", resp.Body)
*/
// read the response
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println(err.Error())
return 0, err
}
isok := string(body[:3])
if isok != "OK:" {
return 0, errors.New("Status Code is" + isok)
}
s := string(body[3:])
return strconv.Atoi(s)
}
func SendSMS(number string, message string, ref string) error {
if !Config.SMSOn {
return nil
}
log.Println("Sending SMS to", number, ":", message)
resp, err := http.PostForm(
Config.SMSServer,
url.Values{
"username": {Config.SMSUser},
"password": {Config.SMSPasswd},
"to": {number},
"from": {"SBS Intl"},
"ref": {ref},
"message": {message},
})
if err != nil {
log.Println("HTTP Post Error", err.Error())
return err
}
// read the response
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
lines := strings.Split(string(body), "\n")
for _, v := range lines {
p := strings.Split(v, ":")
switch p[0] {
case "OK":
log.Println("SMS OK", p[1], "ref", p[2])
case "BAD":
log.Println("SMS BAD", p[1], "reason", p[2])
return errors.New(p[2])
case "ERROR":
log.Println("SMS ERROR", p[1])
return errors.New(p[1])
}
}
return nil
}