-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwsend.go
145 lines (134 loc) · 3.7 KB
/
gwsend.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"gopkg.in/ini.v1"
)
type Token struct {
Access_token string `json:"access_token"`
Token_type string `json:"token_type"`
Expires_in int `json:"expires_in"`
Scope string `json:"scope"`
}
type R1 struct {
TotalRecord int `json:"totalRecord"`
OutputExt string `json:"outputExt"`
StatusCode string `json:"statusCode"`
ErrReason string `json:"errReason"`
}
type Send struct {
Result R1 `json:"result"`
RespDesc string `json:"respDesc"`
InstanceId string `json:"instanceId"`
RespCode string `json:"respCode"`
}
var (
ConfigPath string
SendData string
xml = `
<soapenv:Envelope xmlns:soapenv="http://xxx" xmlns:sms="http://xxx">
<soapenv:Header/>
`
gwapi = "http://xxx"
contType = "application/json"
api = "http://xxx?"
d = `{
"receiveNums":"%s",
"templateNum":"%s",
"dynaVariablle":%s,
"inputExt":"JSON"
}`
)
func main() {
flag.StringVar(&ConfigPath, "f", "/tmp/config.ini", "请填写配置文件路径,路径一定要加双引号括起来!")
flag.StringVar(&SendData, "d", "", "发送短信的数据,最好加双引号+{}括起来写内容!")
flag.Parse()
cfg, err := ini.Load(ConfigPath)
if err != nil {
fmt.Printf("Fail to read file: %v", err)
return
}
if len(SendData) == 0 {
fmt.Println("请使用-d参数指定发送短信的内容,最好加双引号+{}括起来写内容!")
return
}
number := cfg.Section("config").Key("numberList").String()
templateid := cfg.Section("config").Key("templateId").String()
mesapi := "http://xxx?"
d1 := fmt.Sprintf(d, number, templateid, SendData)
token := GetGwToken(gwapi)
// 拼接url
c := url.Values{"method": {"xxx"}, "format": {"json"}, "appId": {"x"}, "version": {"V1.0"}, "accessToken": {token}, "sign": {"xxx"}, "timestamp": {"xxx"},
"content": {d1}}
tzapi := fmt.Sprint(api, c.Encode())
fmt.Println(tzapi)
code := GetSendCode(tzapi)
fmt.Printf("请求接口返回的状态码%s\n", code)
if code != "00000" {
directSend(number, mesapi)
}
}
// 调用短信接口
func directSend(number, mesapi string) {
s := `<?xml version="1.0" encoding="UTF-8"?><SmsServiceReq><SmsList><FreeCode>000000</FreeCode><Mobile>%s</Mobile><Contents>%s</Contents></SmsList></SmsServiceReq>`
str := fmt.Sprintf(s, number, SendData)
// fmt.Println(str)
md5xml := md5V(fmt.Sprintf("centerhr!12300%s5400", str))
md5xml = fmt.Sprintf(xml, str, md5xml)
// fmt.Println(md5xml)
req, err := http.NewRequest("POST", mesapi, strings.NewReader(md5xml))
req.Header.Add("content-type", "application/xml;charset=utf-8")
fmt.Println("request", err)
response, err := http.DefaultClient.Do(req)
fmt.Println("response", err)
data, err := ioutil.ReadAll(response.Body)
response.Body.Close()
fmt.Println(string(data), err)
}
// 验证md5值
func md5V(str string) string {
h := md5.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// 调用接口
func GetSendCode(tzapi string) string {
resp, err := http.Post(tzapi, contType, nil)
if err != nil {
fmt.Printf("post failed, err:%v\n", err)
return "no"
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("get resp failed, err:%v\n", err)
return "no"
}
objsend := &Send{}
json.Unmarshal(b, objsend)
return objsend.RespCode
}
// 获取gw token
func GetGwToken(gwapi string) string {
r, err := http.Get(gwapi)
if err != nil {
fmt.Printf("get failed, err:%v\n", err)
return "no"
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("get resp failed, err:%v\n", err)
return "no"
}
obj := &Token{}
json.Unmarshal(b, obj)
return obj.Access_token
}