|
@@ -0,0 +1,129 @@
|
|
|
+package notify
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ errors2 "errors"
|
|
|
+ "fmt"
|
|
|
+ "iot_manager_service/config"
|
|
|
+ "strings"
|
|
|
+
|
|
|
+ "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
|
+ "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
|
|
+ "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
|
+ sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func SendTencentSms(receiverPhoneNumbers []string, smsTemplateId string, values []string) error {
|
|
|
+ mainConfig := config.Instance().MonitNotice.Sms.Tencent
|
|
|
+
|
|
|
+ * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
|
|
|
+ * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
|
|
|
+ * 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
|
|
|
+ * 以免泄露密钥对危及你的财产安全。
|
|
|
+ * SecretId、SecretKey 查询: https:
|
|
|
+ credential := common.NewCredential(
|
|
|
+ mainConfig.SecretID,
|
|
|
+ mainConfig.SecretKey,
|
|
|
+ )
|
|
|
+
|
|
|
+ * 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
|
|
+ cpf := profile.NewClientProfile()
|
|
|
+
|
|
|
+
|
|
|
+ * 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
|
|
|
+ cpf.HttpProfile.ReqMethod = "POST"
|
|
|
+
|
|
|
+
|
|
|
+ * 如有需要请在代码中查阅以获取最新的默认值 */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
|
|
|
+
|
|
|
+
|
|
|
+ cpf.SignMethod = "HmacSHA1"
|
|
|
+
|
|
|
+
|
|
|
+ * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https:
|
|
|
+ client, _ := sms.NewClient(credential, "ap-guangzhou", cpf)
|
|
|
+
|
|
|
+
|
|
|
+ * 你可以直接查询SDK源码确定接口有哪些属性可以设置
|
|
|
+ * 属性可能是基本类型,也可能引用了另一个数据结构
|
|
|
+ * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
|
|
|
+ request := sms.NewSendSmsRequest()
|
|
|
+
|
|
|
+
|
|
|
+ * SDK采用的是指针风格指定参数,即使对于基本类型你也需要用指针来对参数赋值。
|
|
|
+ * SDK提供对基本类型的指针引用封装函数
|
|
|
+ * 帮助链接:
|
|
|
+ * 短信控制台: https:
|
|
|
+ * 腾讯云短信小助手: https:
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ request.SmsSdkAppId = common.StringPtr(mainConfig.AppID)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ request.SignName = common.StringPtr(mainConfig.SmsKey)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ request.TemplateId = common.StringPtr(smsTemplateId)
|
|
|
+
|
|
|
+
|
|
|
+ request.TemplateParamSet = common.StringPtrs(values)
|
|
|
+
|
|
|
+
|
|
|
+ * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
|
|
|
+ request.PhoneNumberSet = common.StringPtrs(receiverPhoneNumbers)
|
|
|
+
|
|
|
+
|
|
|
+ request.SessionContext = common.StringPtr("")
|
|
|
+
|
|
|
+
|
|
|
+ request.ExtendCode = common.StringPtr("")
|
|
|
+
|
|
|
+
|
|
|
+ request.SenderId = common.StringPtr("")
|
|
|
+
|
|
|
+
|
|
|
+ response, err := client.SendSms(request)
|
|
|
+
|
|
|
+ if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
|
|
+ fmt.Printf("An API error has returned: %s", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ b, _ := json.Marshal(response.Response)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if !strings.Contains(string(b), "OK") {
|
|
|
+ return errors2.New(string(b))
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return nil
|
|
|
+
|
|
|
+ * [FailedOperation.SignatureIncorrectOrUnapproved](https:
|
|
|
+ * [FailedOperation.TemplateIncorrectOrUnapproved](https:
|
|
|
+ * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https:
|
|
|
+ * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https:
|
|
|
+ * 更多错误,可咨询[腾讯云助手](https:
|
|
|
+ */
|
|
|
+}
|