forked from casdoor/go-sms-sender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quanmsms.go
326 lines (314 loc) · 7.63 KB
/
quanmsms.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package go_sms_sender
import (
"fmt"
"strconv"
"strings"
"gitee.com/chengdu-quanming-network/quanmsms-go"
)
// console: https://dev.quanmwl.com/console
// The use of SDK is to ensure the use of the latest standards
type QuanmSMSClient struct {
openid string
apikey string
templateid string
}
var areaCodes = []string{
"+1", // USA, Canada
"+7", // Russia, Kazakhstan
"+20", // Egypt
"+27", // South Africa
"+30", // Greece
"+31", // Netherlands
"+32", // Belgium
"+33", // France
"+34", // Spain
"+36", // Hungary
"+39", // Italy
"+40", // Romania
"+41", // Switzerland
"+43", // Austria
"+44", // United Kingdom
"+45", // Denmark
"+46", // Sweden
"+47", // Norway
"+48", // Poland
"+49", // Germany
"+51", // Peru
"+52", // Mexico
"+53", // Cuba
"+54", // Argentina
"+55", // Brazil
"+56", // Chile
"+57", // Colombia
"+58", // Venezuela
"+60", // Malaysia
"+61", // Australia
"+62", // Indonesia
"+63", // Philippines
"+64", // New Zealand
"+65", // Singapore
"+66", // Thailand
"+81", // Japan
"+82", // South Korea
"+84", // Vietnam
"+86", // China
"+90", // Turkey
"+91", // India
"+92", // Pakistan
"+93", // Afghanistan
"+94", // Sri Lanka
"+95", // Myanmar
"+98", // Iran
"+211", // South Sudan
"+212", // Morocco
"+213", // Algeria
"+216", // Tunisia
"+218", // Libya
"+220", // Gambia
"+221", // Senegal
"+222", // Mauritania
"+223", // Mali
"+224", // Guinea
"+225", // Ivory Coast
"+226", // Burkina Faso
"+227", // Niger
"+228", // Togo
"+229", // Benin
"+230", // Mauritius
"+231", // Liberia
"+232", // Sierra Leone
"+233", // Ghana
"+234", // Nigeria
"+235", // Chad
"+236", // Central African Republic
"+237", // Cameroon
"+238", // Cape Verde
"+239", // Sao Tome and Principe
"+240", // Equatorial Guinea
"+241", // Gabon
"+242", // Republic of the Congo
"+243", // Democratic Republic of the Congo
"+244", // Angola
"+245", // Guinea-Bissau
"+246", // British Indian Ocean Territory
"+247", // Ascension Island
"+248", // Seychelles
"+249", // Sudan
"+250", // Rwanda
"+251", // Ethiopia
"+252", // Somalia
"+253", // Djibouti
"+254", // Kenya
"+255", // Tanzania
"+256", // Uganda
"+257", // Burundi
"+258", // Mozambique
"+260", // Zambia
"+261", // Madagascar
"+262", // Reunion
"+263", // Zimbabwe
"+264", // Namibia
"+265", // Malawi
"+266", // Lesotho
"+267", // Botswana
"+268", // Swaziland
"+269", // Comoros
"+290", // Saint Helena
"+291", // Eritrea
"+297", // Aruba
"+298", // Faroe Islands
"+299", // Greenland
"+350", // Gibraltar
"+351", // Portugal
"+352", // Luxembourg
"+353", // Ireland
"+354", // Iceland
"+355", // Albania
"+356", // Malta
"+357", // Cyprus
"+358", // Finland
"+359", // Bulgaria
"+370", // Lithuania
"+371", // Latvia
"+372", // Estonia
"+373", // Moldova
"+374", // Armenia
"+375", // Belarus
"+376", // Andorra
"+377", // Monaco
"+378", // San Marino
"+379", // Vatican
"+380", // Ukraine
"+381", // Serbia
"+382", // Montenegro
"+383", // Kosovo
"+385", // Croatia
"+386", // Slovenia
"+387", // Bosnia and Herzegovina
"+389", // Macedonia
"+420", // Czech Republic
"+421", // Slovakia
"+423", // Liechtenstein
"+500", // Falkland Islands
"+501", // Belize
"+502", // Guatemala
"+503", // El Salvador
"+504", // Honduras
"+505", // Nicaragua
"+506", // Costa Rica
"+507", // Panama
"+508", // Saint Pierre and Miquelon
"+509", // Haiti
"+590", // Guadeloupe
"+591", // Bolivia
"+592", // Guyana
"+593", // Ecuador
"+594", // French Guiana
"+595", // Paraguay
"+596", // Martinique
"+597", // Suriname
"+598", // Uruguay
"+599", // Netherlands Antilles
"+670", // East Timor
"+672", // Australian External Territories
"+673", // Brunei
"+674", // Nauru
"+675", // Papua New Guinea
"+676", // Tonga
"+677", // Solomon Islands
"+678", // Vanuatu
"+679", // Fiji
"+680", // Palau
"+681", // Wallis and Futuna
"+682", // Cook Islands
"+683", // Niue
"+685", // Samoa
"+686", // Kiribati
"+687", // New Caledonia
"+688", // Tuvalu
"+689", // French Polynesia
"+690", // Tokelau
"+691", // Micronesia
"+692", // Marshall Islands
"+850", // North Korea
"+852", // Hong Kong
"+853", // Macao
"+855", // Cambodia
"+856", // Laos
"+880", // Bangladesh
"+886", // Taiwan
"+960", // Maldives
"+961", // Lebanon
"+962", // Jordan
"+963", // Syria
"+964", // Iraq
"+965", // Kuwait
"+966", // Saudi Arabia
"+967", // Yemen
"+968", // Oman
"+970", // Palestine
"+971", // United Arab Emirates
"+972", // Israel
"+973", // Bahrain
"+974", // Qatar
"+975", // Bhutan
"+976", // Mongolia
"+977", // Nepal
"+992", // Tajikistan
"+993", // Turkmenistan
"+994", // Azerbaijan
"+995", // Georgia
"+996", // Kyrgyzstan
"+998", // Uzbekistan
}
func GetQuanmSMSClient(openid string, apikey string, templateid string) (*QuanmSMSClient, error) {
return &QuanmSMSClient{
openid: openid,
apikey: apikey,
templateid: templateid,
}, nil
}
func (c *QuanmSMSClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
code, ok := param["code"]
if !ok {
return fmt.Errorf("missing parameter: code")
}
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}
smsSDK := quanmsms.NewSmsSDK(c.openid, c.apikey, "https")
template_args := map[string]interface{}{"code": code}
for _, tel := range targetPhoneNumber {
templateidInt64, intErr := strconv.Atoi(c.templateid)
if intErr != nil {
return fmt.Errorf("template id error")
}
// casdoos会给手机号加上区域代码,需要去除
var phone string = tel
for _, code := range areaCodes {
if strings.HasPrefix(tel, code) {
phone = strings.TrimPrefix(tel, code)
break
}
}
resp, err := smsSDK.Send(phone, int8(templateidInt64), template_args)
if err != nil {
return fmt.Errorf("other error:" + err.Error())
}
switch string(resp.State) {
case "201":
// 表单信息有误或触发限发机制
return fmt.Errorf("refusal to send due to security reasons")
case "202":
// 账户重复
return fmt.Errorf("account repeat")
case "203":
// 服务器异常或限流
return fmt.Errorf("server error,Please try again later")
case "205":
// 请求不安全
return fmt.Errorf("illegal request")
case "207":
// 配额不足
return fmt.Errorf("insufficient balance")
case "208":
// 验签失败
return fmt.Errorf("verification failed")
case "209":
// 账户被禁用接口
return fmt.Errorf("insufficient permissions")
case "210":
// 账户被冻结
return fmt.Errorf("account frozen")
case "211":
// 请求参数超过上限
return fmt.Errorf("parameter too long")
case "212":
// 权限不足或使用了他人模板
return fmt.Errorf("insufficient permissions or using someone else's template")
case "213":
// 调用状态异常
return fmt.Errorf("status error")
case "215":
// 内容受限
return fmt.Errorf("content restricted")
case "216":
// 内容违法
return fmt.Errorf("content violation")
}
}
return nil
}