/******************************************************************************* * Copyright 2017. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. *******************************************************************************/ package sms import ( "encoding/json" "fmt" "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" "github.com/winc-link/hummingbird/internal/pkg/logger" ) import ( sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" // 引入sms ) type SmsClient struct { lc logger.LoggingClient client *sms.Client appId string } func NewSmsClient(lc logger.LoggingClient, secretId, secretKey string, appId string) *SmsClient { credential := common.NewCredential( secretId, secretKey, ) /* 非必要步骤: * 实例化一个客户端配置对象,可以指定超时时间等配置 */ cpf := profile.NewClientProfile() /* SDK默认使用POST方法。 * 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */ cpf.HttpProfile.ReqMethod = "POST" /* SDK有默认的超时时间,非必要请不要进行调整 * 如有需要请在代码中查阅以获取最新的默认值 */ // cpf.HttpProfile.ReqTimeout = 5 /* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */ cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com" /* 实例化要请求产品(以sms为例)的client对象 * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */ client, _ := sms.NewClient(credential, "ap-guangzhou", cpf) return &SmsClient{ client: client, appId: appId, lc: lc, } } func (s *SmsClient) Send(templateId string, templateParamSet []string, phoneNumber []string) { request := sms.NewSendSmsRequest() /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */ // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看 request.SmsSdkAppId = common.StringPtr(s.appId) /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */ // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看 request.SignName = common.StringPtr("") /* 模板 ID: 必须填写已审核通过的模板 ID */ // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看 request.TemplateId = common.StringPtr(templateId) /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/ request.TemplateParamSet = common.StringPtrs(templateParamSet) /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/ request.PhoneNumberSet = common.StringPtrs(phoneNumber) response, err := s.client.SendSms(request) // 处理异常 if _, ok := err.(*errors.TencentCloudSDKError); ok { fmt.Printf("An API error has returned: %s", err) s.lc.Error("sms send error: ", err) return } // 非SDK异常,直接失败。实际代码中可以加入其他的处理。 if err != nil { panic(err) } b, _ := json.Marshal(response.Response) // 打印返回的json字符串 s.lc.Infof("sms send %+v", string(b)) }