master v1.0.0
kanade 4 years ago
commit c2dc33357b

@ -0,0 +1 @@
# 腾讯sdk工具

@ -0,0 +1,5 @@
module git.oa00.com/go/tencent
go 1.16
require github.com/tencentcloud/tencentcloud-sdk-go v1.0.143

@ -0,0 +1,2 @@
github.com/tencentcloud/tencentcloud-sdk-go v1.0.143 h1:Q5sSP5UHW30tdyJrH2GK78CFD4a8K8cIxw55UfSOn+k=
github.com/tencentcloud/tencentcloud-sdk-go v1.0.143/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=

@ -0,0 +1,74 @@
package tencent
import (
"errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20190711" //引入sms
"log"
)
var Sms = &smsCli{}
type SmsConfig struct {
SecretId string
SecretKey string
AppID string
Sign string
}
type smsCli struct {
config *SmsConfig
client *sms.Client
}
// InitSms @Title 初始化连接工具
func InitSms(config *SmsConfig) {
Sms.config = config
credential := common.NewCredential(config.SecretId, config.SecretKey)
/* :
* */
cpf := profile.NewClientProfile()
/* SDK 访
* SMS sms.ap-shanghai-fsi.tencentcloudapi.com */
//cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
/* SDK 默认用 TC3-HMAC-SHA256 进行签名,非必要请不要修改该字段 */
//cpf.SignMethod = "HmacSHA1"
/* SMS client
* ap-guangzhou */
var err error
Sms.client, err = sms.NewClient(credential, "ap-guangzhou", cpf)
if err != nil {
log.Fatalln("短信接口错误")
}
}
// SendSms @Title 发送短信
func (s *smsCli) SendSms(templateID string, phones []string, params []string) error {
request := sms.NewSendSmsRequest()
/* 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID例如1400006666 */
request.SmsSdkAppid = &s.config.AppID
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息 */
request.Sign = &s.config.Sign
/* 模板参数: 若无模板参数,则设置为空*/
request.TemplateParamSet = common.StringPtrs(params)
/* 模板 ID: 必须填写已审核通过的模板 ID可登录 [短信控制台] 查看模板 ID */
request.TemplateID = &templateID
/* e.164 +[][]
* +8613711112222 + 8613711112222200*/
for _, phone := range phones {
wordPhone := "+86" + phone
request.PhoneNumberSet = append(request.PhoneNumberSet, &wordPhone)
}
// 通过 client 对象调用想要访问的接口,需要传入请求对象
_, err := s.client.SendSms(request)
// 处理异常
if err != nil {
return errors.New("发送失败")
}
return nil
}
Loading…
Cancel
Save