-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
69 lines (64 loc) · 1.99 KB
/
client.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
package sms
import (
"errors"
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
)
const endpoint = "dysmsapi.aliyuncs.com"
// RequestSendSmsData 请求发送短信数据
type RequestSendSmsData struct {
AccessKeyId string `json:"access_key_id"`
AccessKeySecret string `json:"access_key_secret"`
RegionId string `json:"region_id,omitempty"`
PhoneNumbers string `json:"phone_numbers"`
SignName string `json:"sign_name"`
TemplateCode string `json:"template_code"`
TemplateParam string `json:"template_param"`
}
// CreateClient 创建Client
func CreateClient(accessKeyId, accessKeySecret string) (result *dysmsapi20170525.Client, err error) {
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
Endpoint: tea.String(endpoint),
}
result = &dysmsapi20170525.Client{}
result, err = dysmsapi20170525.NewClient(config)
return
}
// SendSms 发送短信
func SendSms(data RequestSendSmsData) (err error) {
var client *dysmsapi20170525.Client
client, err = CreateClient(data.AccessKeyId, data.AccessKeySecret)
if err != nil {
return
}
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(data.PhoneNumbers),
SignName: tea.String(data.SignName),
TemplateCode: tea.String(data.TemplateCode),
TemplateParam: tea.String(data.TemplateParam),
}
runtime := &util.RuntimeOptions{}
catchError := func() (e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
e = r
}
}()
_, e = client.SendSmsWithOptions(sendSmsRequest, runtime)
return
}()
if catchError != nil {
var e = &tea.SDKError{}
if t, ok := catchError.(*tea.SDKError); ok {
e = t
} else {
e.Message = tea.String(catchError.Error())
}
err = errors.New("短信发送失败: " + err.Error())
}
return
}