forked from xluohome/smscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_hywx.go
58 lines (50 loc) · 1.03 KB
/
f_hywx.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
/**
互亿无线 短信通道
http://www.ihuyi.cn/
**/
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
log "github.com/golang/glog"
)
type Hywx struct {
sms *SMS
XMLName xml.Name `xml:"SubmitResult"`
Code int `xml:"code"`
Msg string `xml:"msg"`
}
func init() {
SenderMap["hywx"] = func() Sender {
return &Hywx{}
}
}
func (h *Hywx) Send(sms *SMS) error {
h.sms = sms
var data = make(url.Values)
data.Set("account", config.Vendors["hywx"]["account"])
data.Set("password", config.Vendors["hywx"]["password"])
data.Set("mobile", h.sms.Mobile)
data.Set("content", strings.Replace(h.sms.Config.Tpl, "{code}", h.sms.Code, -1))
res, err := http.PostForm(config.Vendors["hywx"]["RestURL"], data)
if err != nil {
return err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if err := xml.Unmarshal(body, h); err != nil {
return err
}
if h.Code != 2 {
log.Errorf("%v", h)
return fmt.Errorf("%s", h.Msg)
}
return nil
}