-
Notifications
You must be signed in to change notification settings - Fork 26
/
sms.go
276 lines (226 loc) · 6.89 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package qcloudsms
import (
"encoding/json"
"errors"
"strings"
)
/*
SMSSingleReq 发送单条短信请求结构
将单发短信和模板短信统一到了一个结构体里,构造请求时为对应请求的必传字段赋值即可
单发 https://cloud.tencent.com/document/product/382/5808
模板 https://cloud.tencent.com/document/product/382/5976
*/
type SMSSingleReq struct {
Tel SMSTel `json:"tel"`
Type int `json:"type,omitempty"`
Sign string `json:"sign,omitempty"`
TplID int `json:"tpl_id,omitempty"`
Params []string `json:"params"`
Msg string `json:"msg,omitempty"`
Sig string `json:"sig"`
Time int64 `json:"time"`
Extend string `json:"extend"`
Ext string `json:"ext"`
}
// SMSTel 国家码,手机号
type SMSTel struct {
Nationcode string `json:"nationcode"`
Mobile string `json:"mobile"`
}
// SMSResult 发送短信返回结构
type SMSResult struct {
Result uint `json:"result"`
Errmsg string `json:"errmsg"`
Ext string `json:"ext"`
Sid string `json:"sid,omitempty"`
Fee uint `json:"fee,omitempty"`
}
// SendSMSSingle 发送单条短信
func (c *QcloudSMS) SendSMSSingle(ss SMSSingleReq) (bool, error) {
c = c.NewSig(ss.Tel.Mobile).NewURL(SENDSMS)
ss.Time = c.ReqTime
ss.Sig = c.Sig
resp, err := c.NewRequest(ss)
if err != nil {
return false, err
}
var res SMSResult
json.Unmarshal([]byte(resp), &res)
if res.Result == SUCCESS {
return true, nil
}
return false, errors.New(res.Errmsg)
}
/*
SMSMultiReq 群发短信请求结构
将普通短信和模板短信统一到了一个结构体里,构造请求时为对应请求的必传字段赋值即可
普通 https://cloud.tencent.com/document/product/382/5806
模板 https://cloud.tencent.com/document/product/382/5977
*/
type SMSMultiReq struct {
Tel []SMSTel `json:"tel"`
Type uint `json:"type,omitempty"`
Sign string `json:"sign,omitempty"`
TplID uint `json:"tpl_id,omitempty"`
Params []string `json:"params"`
Msg string `json:"msg,omitempty"`
Sig string `json:"sig"`
Time int64 `json:"time"`
Extend string `json:"extend"`
Ext string `json:"ext"`
}
// SMSMultiResult 群发短信返回结构
type SMSMultiResult struct {
Result uint `json:"result"`
Errmsg string `json:"errmsg"`
Ext string `json:"ext"`
Detail []struct {
Result uint `json:"result"`
Errmsg string `json:"errmsg"`
Mobile string `json:"mobile"`
Nationcode string `json:"nationcode"`
Sid string `json:"sid,omitempty"`
Fee uint `json:"fee,omitempty"`
} `json:"detail"`
}
// SendSMSMulti 群发短信
func (c *QcloudSMS) SendSMSMulti(sms SMSMultiReq) (bool, error) {
var sigMobile []string
if len(sms.Tel) > MULTISMSMAX {
return false, ErrMultiCount
}
for _, m := range sms.Tel {
sigMobile = append(sigMobile, m.Mobile)
}
mobileStr := strings.Join(sigMobile, ",")
c = c.NewSig(mobileStr).NewURL(MULTISMS)
sms.Time = c.ReqTime
sms.Sig = c.Sig
resp, err := c.NewRequest(sms)
if err != nil {
return false, err
}
var res SMSMultiResult
json.Unmarshal([]byte(resp), &res)
if res.Result == SUCCESS {
return true, nil
}
return false, errors.New(res.Errmsg)
}
// StatusMobileReq 拉取单个手机短信状态请求结构
type StatusMobileReq struct {
Sig string `json:"sig"`
Time int64 `json:"time"`
// 0 1分别代表 短信下发状态,短信回复
Type int `json:"type"`
// 最大条数 最多100
Max int `json:"max"`
BeginTime int64 `json:"begin_time"`
EndTime int64 `json:"end_time"`
Nationcode string `json:"nationcode"`
Mobile string `json:"mobile"`
}
// StatusMobileResult 拉取单个手机短信状态的返回结构
type StatusMobileResult struct {
Result int `json:"result"`
Errmsg string `json:"errmsg"`
Count int `json:"count"`
Data []SMSStatusResult `json:"data"`
}
// StatusReplyResult 短信回复结构
type StatusReplyResult struct {
Result int `json:"result"`
Errmsg string `json:"errmsg"`
Count int `json:"count"`
Data []SMSReplyResult `json:"data"`
}
// SMSStatusResult 单个手机短信状态结构
type SMSStatusResult struct {
UserReceiveTime string `json:"user_receive_time"`
Nationcode string `json:"nationcode"`
Mobile string `json:"mobile"`
ReportStatus string `json:"report_status"`
Errmsg string `json:"errmsg"`
Description string `json:"description"`
Sid string `json:"sid"`
}
// SMSReplyResult 短信回复列表结构
type SMSReplyResult struct {
Nationcode string `json:"nationcode"`
Mobile string `json:"mobile"`
Text string `json:"text"`
Sign string `json:"sign"`
Time int64 `json:"time"`
Extend string `json:"extend"`
}
// GetStatusForMobile 拉取单个手机短信状态(下发状态)
//
// https://cloud.tencent.com/document/product/382/5811
func (c *QcloudSMS) GetStatusForMobile(smr StatusMobileReq) (StatusMobileResult, error) {
c = c.NewSig("").NewURL(MOBILESTATUS)
smr.Time = c.ReqTime
smr.Sig = c.Sig
var res StatusMobileResult
resp, err := c.NewRequest(smr)
if err != nil {
return res, err
}
json.Unmarshal([]byte(resp), &res)
return res, nil
}
// GetReplyForMobile 拉取单个手机短信状态(短信回复)
//
// https://cloud.tencent.com/document/product/382/5811
func (c *QcloudSMS) GetReplyForMobile(smr StatusMobileReq) (StatusReplyResult, error) {
c = c.NewSig("").NewURL(MOBILESTATUS)
smr.Time = c.ReqTime
smr.Sig = c.Sig
var res StatusReplyResult
resp, err := c.NewRequest(smr)
if err != nil {
return res, err
}
json.Unmarshal([]byte(resp), &res)
return res, nil
}
// PullStatusReq 拉取短信状态请求结构
type PullStatusReq struct {
Sig string `json:"sig"`
Time int64 `json:"time"`
// 0 1分别代表 短信下发状态,短信回复
Type int `json:"type"`
// 最大条数 最多100
Max int `json:"max"`
}
// PullStatusResult 拉取下发状态返回数据结构
type PullStatusResult struct {
Result int `json:"result"`
Errmsg string `json:"errmsg"`
// result为0时有效,返回的信息条数
Count int `json:"count"`
Data []SMSStatusResult `json:"data"`
}
// PullReplyResult 拉取短信回复返回数据结构
type PullReplyResult struct {
Result int `json:"result"`
Errmsg string `json:"errmsg"`
// result为0时有效,返回的信息条数
Count int `json:"count"`
Data []SMSReplyResult `json:"data"`
}
// GetStatusMQ 拉取短信状态(下发状态,短信回复)
// 已拉取过的数据将不会再返回
//
// https://cloud.tencent.com/document/product/382/5810
func (c *QcloudSMS) GetStatusMQ(psr PullStatusReq) (StatusMobileResult, error) {
c = c.NewSig("").NewURL(PULLSTATUS)
psr.Time = c.ReqTime
psr.Sig = c.Sig
resp, err := c.NewRequest(psr)
if err != nil {
return StatusMobileResult{}, err
}
var res StatusMobileResult
json.Unmarshal([]byte(resp), &res)
return res, nil
}