-
Notifications
You must be signed in to change notification settings - Fork 0
/
yunsms.go
39 lines (35 loc) · 1.03 KB
/
yunsms.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
package utils
import (
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
"github.com/alibabacloud-go/tea/tea"
)
// 短信实例
type YunSMS struct {
client *dysmsapi.Client
}
// 新建实例
func NewYunSMS(accessKeyId, accessKeySecret string, smsEndpoint ...string) (sms YunSMS, err error) {
endpoint:="dysmsapi.aliyuncs.com"
if len(smsEndpoint) > 0 {
endpoint = smsEndpoint[0]
}
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
Endpoint: tea.String(endpoint),
}
sms.client, err = dysmsapi.NewClient(config)
return
}
// 发送短信
func (a *YunSMS) Send(signname, template, phoneNumber, jsonStr string) (err error) {
sendSmsRequest := &dysmsapi.SendSmsRequest{
PhoneNumbers: tea.String(phoneNumber),
SignName: tea.String(signname),
TemplateCode: tea.String(template),
TemplateParam: tea.String(jsonStr),
}
_, err = a.client.SendSms(sendSmsRequest)
return
}