chore: 初始开源发布
iotlcm-sms:独立短信 HTTP/gRPC 服务,支持阿里云与腾讯云。 - Go module 迁移至 git.5iot.com/iotlcm-public/iotlcm-sms - 补 Apache-2.0 LICENSE,重写 README(特性/配置/接口/部署/目录/测试) - 配置改为 config.example.yaml + SMS_* 环境变量注入,仓库不含真实凭证 - 清理内部标识(私有镜像仓库 / 公司名 / 客户签名 / 真实模板ID / 真实手机号) - 统一 LF 行尾并加 .gitattributes
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package tencent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
tencentsdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20190711"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-sms/internal/sms"
|
||||
)
|
||||
|
||||
type Provider struct{}
|
||||
|
||||
func New() *Provider { return &Provider{} }
|
||||
|
||||
func (p *Provider) Send(_ context.Context, config sms.Config, message sms.Message) (sms.Result, error) {
|
||||
credential := common.NewCredential(config.AccessKeyID, config.AccessKeySecret)
|
||||
clientProfile := profile.NewClientProfile()
|
||||
clientProfile.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
|
||||
client, err := tencentsdk.NewClient(credential, "ap-beijing", clientProfile)
|
||||
if err != nil {
|
||||
return failed(err)
|
||||
}
|
||||
|
||||
request := tencentsdk.NewSendSmsRequest()
|
||||
request.SmsSdkAppid = common.StringPtr(config.SDKAppID)
|
||||
request.Sign = common.StringPtr(config.SignName)
|
||||
request.TemplateID = common.StringPtr(config.TemplateID)
|
||||
request.TemplateParamSet = common.StringPtrs(sms.SplitAndTrim(message.MsgContent, "|"))
|
||||
request.PhoneNumberSet = common.StringPtrs(sms.SplitAndTrim(message.PhoneNumber, ","))
|
||||
|
||||
response, err := client.SendSms(request)
|
||||
if err != nil {
|
||||
return failed(err)
|
||||
}
|
||||
if response.Response == nil || len(response.Response.SendStatusSet) == 0 {
|
||||
err = fmt.Errorf("腾讯云未返回短信发送状态")
|
||||
return failed(err)
|
||||
}
|
||||
|
||||
status := response.Response.SendStatusSet[0]
|
||||
data := map[string]any{
|
||||
"provider": "tencent",
|
||||
"requestId": stringValue(response.Response.RequestId),
|
||||
"serialNo": stringValue(status.SerialNo),
|
||||
"phoneNumber": stringValue(status.PhoneNumber),
|
||||
"providerCode": stringValue(status.Code),
|
||||
}
|
||||
if stringValue(status.Code) != "Ok" {
|
||||
err = fmt.Errorf("腾讯云短信发送失败: %s", stringValue(status.Message))
|
||||
return sms.Result{Code: 502, Message: err.Error(), Data: data}, err
|
||||
}
|
||||
return sms.Result{Code: 0, Message: "发送成功", Data: data}, nil
|
||||
}
|
||||
|
||||
func failed(err error) (sms.Result, error) {
|
||||
return sms.Result{Code: 502, Message: err.Error()}, err
|
||||
}
|
||||
|
||||
func stringValue(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return *value
|
||||
}
|
||||
Reference in New Issue
Block a user