短信发送方法
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.

59 lines
1.7 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. * @region 区域, ap-guangzhou|ap-beijing|ap-shanghai
  17. * @return *requestId,[]*sendStatus,error
  18. *
  19. */
  20. func SendByTencent(secretId, secretKey, smsAppId string, signname string, mobiles []string, templateId string, params []string, region ...string) (*string, []*sms.SendStatus, error) {
  21. credential := common.NewCredential(
  22. secretId,
  23. secretKey,
  24. )
  25. cpf := profile.NewClientProfile()
  26. cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
  27. region_default := "ap-guangzhou"
  28. if len(region) > 0 {
  29. region_default = region[0]
  30. }
  31. client, _ := sms.NewClient(credential, region_default, cpf)
  32. request := sms.NewSendSmsRequest()
  33. request.PhoneNumberSet = common.StringPtrs(mobiles) //[]string{"18607552321"}
  34. request.SmsSdkAppId = common.StringPtr(smsAppId)
  35. request.SignName = common.StringPtr(signname)
  36. request.TemplateId = common.StringPtr(templateId)
  37. request.TemplateParamSet = common.StringPtrs(params) //[]string{"param1", "param2"}
  38. response, err := client.SendSms(request)
  39. if _, ok := err.(*errors.TencentCloudSDKError); ok {
  40. log.Println("An API error has returned: ", err)
  41. return nil, nil, err
  42. }
  43. if err != nil {
  44. log.Println(err)
  45. return nil, nil, err
  46. }
  47. return response.Response.RequestId, response.Response.SendStatusSet, nil
  48. }