Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ali sms #9

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions conf/confTemplate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ senders:
# appKey: xxxx
# appSecret: xxxx
# robotCode: xxxx
aliSms:
# - name: yourSenderName8
# accessKey: xxxx
# accessSecret: xxxx
# templateCode: SMS_123456789
# signName: xxxx
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gin-gonic/gin v1.9.1
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moul/http2curl v1.0.0
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/samber/lo v1.38.1
github.com/spf13/cast v1.5.1
golang.org/x/sync v0.3.0
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
moul.io/http2curl v1.0.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
Expand Down Expand Up @@ -522,5 +524,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
88 changes: 88 additions & 0 deletions send/aliSms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package send

import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"math/rand"
"net/url"
"sort"
"strings"
"time"

"github.com/samber/lo"
"github.com/spf13/cast"
)

const (
aliSmsUrl = "http://dysmsapi.aliyuncs.com"
)

func init() {
registered["aliSms"] = func(conf map[string]string) sender {
return &aliSms{conf: conf}
}
}

type aliSms struct {
conf map[string]string
}

// send ali sms
//
// https://help.aliyun.com/zh/sms/developer-reference/api-dysmsapi-2017-05-25-sendsms
func (a *aliSms) send(msg *message) error {
body := map[string]string{
"PhoneNumbers": strings.Join(msg.Tos, ","),
"SignName": a.conf["signName"],
"TemplateCode": a.conf["templateCode"],
"TemplateParam": msg.Content,
}
req := rc.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetQueryParams(map[string]string{
"Action": "SendSms",
"Version": "2017-05-25",
"Format": "JSON",
"AccessKeyId": a.conf["accessKey"],
"SignatureNonce": cast.ToString(rand.Int63()),
"Timestamp": time.Now().UTC().Format("2006-01-02T15:04:05Z"),
"SignatureMethod": "HMAC-SHA1",
"SignatureVersion": "1.0",
"AcceptLanguage": "zh-CN",
}).
SetFormData(body)
ks := lo.Keys(body)
for k := range req.QueryParam {
ks = append(ks, k)
}
sort.Strings(ks)
encodeParams := make([]string, 0)
for _, k := range ks {
v, ok := body[k]
if !ok {
v = req.QueryParam.Get(k)
}
encodeParams = append(encodeParams, fmt.Sprintf("%s=%s", url.QueryEscape(k), url.QueryEscape(v)))
}
CanonicalizedQueryString := strings.Join(encodeParams, "&")
stringToSign := fmt.Sprintf("%s&%s&%s",
"POST",
url.QueryEscape("/"),
url.QueryEscape(CanonicalizedQueryString),
)
hashed := hmac.New(sha1.New, []byte(a.conf["accessSecret"]+"&"))
hashed.Write([]byte(stringToSign))

signature := base64.StdEncoding.EncodeToString(hashed.Sum(nil))
req.SetQueryParam("Signature", signature)

resp, err := req.Post(aliSmsUrl)

return handleErr("send to ali sms failed", err, resp, func(dt map[string]any) bool { return dt["Code"] == "OK" })
}

func (a *aliSms) getConf() map[string]string {
return a.conf
}
2 changes: 1 addition & 1 deletion send/dingdingBot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type dingdingBot struct {
conf map[string]string
}

// send dingtal bot message
// send dingtalk bot message
//
// https://open.dingtalk.com/document/orgapp/custom-bot-creation-and-installation
func (d *dingdingBot) send(msg *message) error {
Expand Down
2 changes: 1 addition & 1 deletion send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
msgCh = make(chan *message, 10000)
confCh = make(chan struct{}, 1)
name2sender = make(map[string]sender)
rc = resty.New()
rc = resty.NewWithClient(&http.Client{})
)

type sender interface {
Expand Down