70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
//go:build plus
|
||
|
||
package smssenders
|
||
|
||
import (
|
||
"errors"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||
"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/v20210111" // 引入sms
|
||
)
|
||
|
||
// 相关文档:https://cloud.tencent.com/document/product/382/43199
|
||
|
||
// TencentSMSSender 腾讯云短信
|
||
type TencentSMSSender struct {
|
||
params *userconfigs.SMSSenderTencentSMSParams
|
||
}
|
||
|
||
// NewTencentSMSSender 获取新对象
|
||
func NewTencentSMSSender() *TencentSMSSender {
|
||
return &TencentSMSSender{}
|
||
}
|
||
|
||
// Init 初始化
|
||
func (this *TencentSMSSender) Init(params any) error {
|
||
if params == nil {
|
||
return errors.New("'params' should not be nil")
|
||
}
|
||
var ok bool
|
||
this.params, ok = params.(*userconfigs.SMSSenderTencentSMSParams)
|
||
if !ok {
|
||
return errors.New("invalid params type")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// Send 发送短信
|
||
func (this *TencentSMSSender) Send(mobile string, body string, code string) (resp string, isOk bool, err error) {
|
||
var credential = common.NewCredential(this.params.AccessKeyId, this.params.AccessKeySecret)
|
||
var cpf = profile.NewClientProfile()
|
||
cpf.HttpProfile.ReqMethod = "POST"
|
||
// cpf.HttpProfile.ReqTimeout = 5
|
||
cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
|
||
cpf.SignMethod = "HmacSHA1"
|
||
|
||
client, err := sms.NewClient(credential, "ap-guangzhou", cpf)
|
||
if err != nil {
|
||
return "", false, err
|
||
}
|
||
|
||
var request = sms.NewSendSmsRequest()
|
||
request.SmsSdkAppId = common.StringPtr(this.params.SDKAppId)
|
||
request.SignName = common.StringPtr(this.params.Sign)
|
||
request.TemplateId = common.StringPtr(this.params.TemplateId)
|
||
request.TemplateParamSet = common.StringPtrs([]string{code})
|
||
request.PhoneNumberSet = common.StringPtrs([]string{"+86" + mobile}) // TODO 支持海外手机号?
|
||
request.SessionContext = common.StringPtr("")
|
||
request.ExtendCode = common.StringPtr("")
|
||
request.SenderId = common.StringPtr("")
|
||
|
||
// 通过client对象调用想要访问的接口,需要传入请求对象
|
||
_, err = client.SendSms(request)
|
||
if err != nil {
|
||
return "", false, err
|
||
}
|
||
|
||
return "", true, nil
|
||
}
|