Browse Source

增加阿里云短信发送接口

master v1.0.1
wuyongyuan 2 years ago
parent
commit
3c315096e3
4 changed files with 94 additions and 0 deletions
  1. +54
    -0
      aliyun.go
  2. +20
    -0
      aliyun_test.go
  3. +1
    -0
      go.mod
  4. +19
    -0
      go.sum

+ 54
- 0
aliyun.go View File

@ -0,0 +1,54 @@
package sms
import (
"errors"
"log"
"git.tetele.net/tgo/helper"
dysmsapi "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)
/**
* 企业信息服务平台--阿里云短信九江食堂用
* @accessKeyId 用来验证用户身份的keyId
* @accessKeySecret 用来验证用户身份的secret
* @signName 短信签名名称
* @templateCode 短信模板CODE
* @phoneNumbers 手机号多个用逗号隔开
* @teplateParam 短信内容
*/
func SendByALiYun(accessKeyId string, accessKeySecret string, phoneNumbers string, signName string, templateCode string, teplateParam string) (bool, error) {
if accessKeyId == "" || accessKeySecret == "" || phoneNumbers == "" || signName == "" || templateCode == "" || teplateParam == "" {
return false, errors.New("参数不全")
}
client, err := dysmsapi.NewClientWithAccessKey("cn-qingdao", accessKeyId, accessKeySecret)
/* use STS Token
client, err := dysmsapi.NewClientWithStsToken("cn-qingdao", "<your-access-key-id>", "<your-access-key-secret>", "<your-sts-token>")
*/
request := dysmsapi.CreateSendSmsRequest()
request.Scheme = "https"
request.PhoneNumbers = phoneNumbers //"1368846****" //接收短信的手机号码
request.SignName = signName //"阿里云" //短信签名名称
request.TemplateCode = templateCode //"SMS_20933****" //短信模板ID
request.TemplateParam = teplateParam
response, err := client.SendSms(request)
if err != nil {
log.Println("post err", err)
return false, err
}
// var reply_data map[string]interface{}
// err = json.Unmarshal(response, &reply_data)
// if err != nil {
// log.Println(string(response), err)
// return false, err
// }
if helper.ToStr(response.Code) != "OK" {
return false, errors.New("短信发送失败," + helper.ToStr(response.Message))
}
return true, nil
}

+ 20
- 0
aliyun_test.go View File

@ -0,0 +1,20 @@
package sms
import (
"testing"
)
func Test_SendByALiYun(t *testing.T) {
accessKeyId := "LTAI5tCxxykQfnZtmYD8tm2x"
accessKeySecret := "RgbY2Z9ciVc3ljXPr6OXw1ycn3rCIu"
signName := "鄱阳湖生态科技城"
phoneNumbers := "13790717280"
templateCode := "SMS_224500006"
params := make(map[string]string, 0)
params["code"] = "今晚打球吗"
TemplateParam, _ := json.Marshal(params)
ret, err := SendByALiYun(accessKeyId, accessKeySecret, phoneNumbers, signName, templateCode, string(TemplateParam))
t.Log(ret)
t.Log(err)
}

+ 1
- 0
go.mod View File

@ -5,6 +5,7 @@ go 1.16
require (
git.tetele.net/tgo/crypter v0.2.2
git.tetele.net/tgo/helper v0.3.2
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1716
github.com/json-iterator/go v1.1.12
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.0.365


+ 19
- 0
go.sum View File

@ -4,6 +4,8 @@ git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6x
git.tetele.net/tgo/helper v0.3.2 h1:3Pd3Ih3Ux+zFSjNZZv+4MSsVQiZ5HOfQmSus5+7rKR0=
git.tetele.net/tgo/helper v0.3.2/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1716 h1:Cr+/V/Sbfz/dAifi6BlLQ8zdizs67nfRvIeFAc86eNY=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1716/go.mod h1:RcDobYh8k5VP6TNybz9m++gL3ijVI5wueVr0EM10VsU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
@ -13,6 +15,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@ -29,10 +32,19 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -41,6 +53,7 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365 h1:WBjY/1zAWHINPh8UB90HY/cMkbfUTtnb/H4TDqGwI/Q=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.0.365 h1:akX9xDCzYK75pQLrbZIxb/MTPoBF0QVJDARNtWaLzs8=
@ -91,5 +104,11 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/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=

Loading…
Cancel
Save