-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyunsms.go
65 lines (58 loc) · 1.43 KB
/
aliyunsms.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
package aliyunsms
import (
"time"
"github.com/herb-go/fetcher"
"github.com/herb-go/providers/alibaba/aliyun"
)
type Result struct {
BizId string
Code string
Message string
RequestId string
}
var Host = "https://dysmsapi.aliyuncs.com"
func NewSmsParams(accesskey *aliyun.AccessKey) aliyun.Params {
p := aliyun.NewParams((accesskey))
p.
WithAction("SendSms").
With("RegionId", "cn-hangzhou").
WithVersion("2017-05-25").
WithTimestamp(time.Now().Unix()).
WithNonce()
return p
}
type Message struct {
PhoneNumbers string
SignName string
TemplateCode string
TemplateParam string
SmsUpExtendCode string
OutID string
}
func NewMessage() *Message {
return &Message{}
}
func Send(accesskey *aliyun.AccessKey, msg *Message) (*Result, error) {
p := NewSmsParams(accesskey)
p.With("PhoneNumbers", msg.PhoneNumbers)
p.With("SignName", msg.SignName)
p.With("TemplateCode", msg.TemplateCode)
p.With("TemplateParam", msg.TemplateParam)
p.With("SmsUpExtendCode", msg.SmsUpExtendCode)
p.With("OutId", msg.OutID)
q := p.SignedQuery("GET", accesskey.AccessKeySecret)
preset := fetcher.NewPreset().With(
&accesskey.Client,
fetcher.URL(Host),
fetcher.Params(q),
)
var result = &Result{}
resp, err := preset.FetchAndParse(fetcher.Should200(fetcher.AsJSON(&result)))
if err != nil {
return nil, err
}
if result.Code != "OK" {
return nil, resp.NewAPICodeErr(result.Code)
}
return result, nil
}