短信发送方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

  1. package sms
  2. import (
  3. "log"
  4. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
  5. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
  6. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
  7. sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
  8. )
  9. /**
  10. * 使用腾讯云发送短信
  11. * @secretId 腾讯云密钥id
  12. * @secretKey 腾讯云密钥key
  13. * @signname 签名
  14. * @templateId 模板id
  15. * @params 参数值
  16. */
  17. func SendByTencent(secretId, secretKey, smsAppId string, signname string, mobiles []string, templateId string, params []string) (*string, []*sms.SendStatus, error) {
  18. credential := common.NewCredential(
  19. secretId,
  20. secretKey,
  21. )
  22. cpf := profile.NewClientProfile()
  23. cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
  24. client, _ := sms.NewClient(credential, "ap-guangzhou", cpf)
  25. request := sms.NewSendSmsRequest()
  26. request.PhoneNumberSet = common.StringPtrs(mobiles) //[]string{"18607552321"}
  27. request.SmsSdkAppId = common.StringPtr(smsAppId)
  28. request.SignName = common.StringPtr(signname)
  29. request.TemplateId = common.StringPtr(templateId)
  30. request.TemplateParamSet = common.StringPtrs(params) //[]string{"param1", "param2"}
  31. response, err := client.SendSms(request)
  32. if _, ok := err.(*errors.TencentCloudSDKError); ok {
  33. log.Println("An API error has returned: ", err)
  34. return nil, nil, err
  35. }
  36. if err != nil {
  37. log.Println(err)
  38. return nil, nil, err
  39. }
  40. return response.Response.RequestId, response.Response.SendStatusSet, nil
  41. }