tencent_yun.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package services
  2. import (
  3. "encoding/json"
  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. ses "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses/v20201002"
  8. "hongze/hongze_yb_en_api/services/english_report"
  9. "time"
  10. )
  11. const (
  12. TencentSDKSecretId = "AKIDa4WvJar361NZXkogKoHgtyxvkVDqKclq" // 腾讯云主账号SecretId
  13. TencentSDKSecretKey = "61sdMzVvMejjZFj2KTQ9tgR4adYMzR1a" // 腾讯云主账号SecretKey
  14. TencentEmailFromEmailAddress = "email@txhzmail.hzinsights.com" // 腾讯云邮件发信地址
  15. TencentEmailTemplateID = 54181 // 腾讯云邮件模板ID
  16. )
  17. // TencentEmail 腾讯云邮件
  18. type TencentEmail struct {
  19. Client *ses.Client
  20. }
  21. // NewClient
  22. func (te *TencentEmail) NewClient() (err error) {
  23. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
  24. // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
  25. credential := common.NewCredential(
  26. TencentSDKSecretId,
  27. TencentSDKSecretKey,
  28. )
  29. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  30. cpf := profile.NewClientProfile()
  31. cpf.HttpProfile.Endpoint = "ses.tencentcloudapi.com"
  32. // 实例化要请求产品的client对象,clientProfile是可选的
  33. client, _err := ses.NewClient(credential, "ap-hongkong", cpf)
  34. te.Client = client
  35. err = _err
  36. return
  37. }
  38. // TencentEmailTemplateData 邮箱模板替换数据
  39. type TencentEmailTemplateData struct {
  40. REPORT_TITLE string `description:"报告标题"`
  41. REPORT_ABSTRACT string `description:"报告摘要"`
  42. REPORT_CONTENT string `description:"报告内容"`
  43. REPORT_SHARE_LINK string `description:"报告分享链接"`
  44. REPORT_TIME string `description:"报告时间"`
  45. }
  46. // TencentEmailResult 邮件推送响应体
  47. type TencentEmailResult struct {
  48. Code string `description:"错误码"`
  49. Message string `description:"响应信息"`
  50. RequestId string `description:"请求成功-请求ID"`
  51. }
  52. // SendEmail 推送模板邮件
  53. func (te *TencentEmail) SendEmail(req *english_report.EnglishReportSendEmailRequest) (ok bool, result string, err error) {
  54. if te.Client == nil {
  55. if e := te.NewClient(); e != nil {
  56. err = e
  57. return
  58. }
  59. }
  60. // 实例化一个请求对象,每个接口都会对应一个request对象
  61. request := ses.NewSendEmailRequest()
  62. // 模板数据替换
  63. tmpData := &TencentEmailTemplateData{
  64. REPORT_TITLE: req.ReportTitle,
  65. REPORT_ABSTRACT: req.ReportAbstract,
  66. REPORT_CONTENT: req.ReportContent,
  67. REPORT_SHARE_LINK: req.ReportShareLink,
  68. REPORT_TIME: req.ReportTime,
  69. }
  70. tmpDataByte, e := json.Marshal(tmpData)
  71. if e != nil {
  72. err = e
  73. return
  74. }
  75. tmpDataStr := string(tmpDataByte)
  76. request.Template = &ses.Template{
  77. TemplateID: common.Uint64Ptr(TencentEmailTemplateID),
  78. TemplateData: common.StringPtr(tmpDataStr),
  79. }
  80. request.FromEmailAddress = common.StringPtr(TencentEmailFromEmailAddress)
  81. request.Destination = common.StringPtrs([]string{req.Email})
  82. request.Subject = common.StringPtr(req.Subject)
  83. // 返回的resp是一个SendEmailResponse的实例,与请求对象对应
  84. response, e := te.Client.SendEmail(request)
  85. if _, o := e.(*errors.TencentCloudSDKError); o {
  86. errByte, _ := json.Marshal(e)
  87. result = string(errByte)
  88. return
  89. }
  90. if e != nil {
  91. err = e
  92. return
  93. }
  94. ok = true
  95. result = response.ToJsonString()
  96. return
  97. }
  98. // BatchSendEmail 批量推送邮件
  99. func (te *TencentEmail) BatchSendEmail(list []*english_report.EnglishReportSendEmailRequest) (results []*english_report.EnglishReportSendEmailResult, err error) {
  100. results = make([]*english_report.EnglishReportSendEmailResult, 0)
  101. if len(list) == 0 {
  102. return
  103. }
  104. if e := te.NewClient(); e != nil {
  105. err = e
  106. return
  107. }
  108. // SendEmail接口有QPS20的限制, 保险起见每秒只请求10次接口
  109. max := 10
  110. c := 0
  111. for i := range list {
  112. if c >= max {
  113. time.Sleep(time.Second)
  114. c = 0
  115. }
  116. c += 1
  117. dataByte, _ := json.Marshal(list[i])
  118. ok, result, _ := te.SendEmail(list[i])
  119. results = append(results, &english_report.EnglishReportSendEmailResult{
  120. ReportId: list[i].ReportId,
  121. EmailId: list[i].EmailId,
  122. Email: list[i].Email,
  123. Ok: ok,
  124. SendData: string(dataByte),
  125. ResultData: result,
  126. Source: 1,
  127. })
  128. }
  129. return
  130. }
  131. // TencentEmailCallBack 回调请求体
  132. type TencentEmailCallBack struct {
  133. Event string `description:"事件类型"`
  134. Email string `description:"收件人地址"`
  135. Link string `description:"用户点击的邮件中的链接 URL,仅在event=click时生效"`
  136. BulkId string `description:"SendEmail 接口返回的 MessageId"`
  137. Timestamp int `description:"事件产生的时间戳"`
  138. Reason string `description:"邮件递送失败的原因"`
  139. BounceType string `description:"如果收件人邮件服务商拒信,拒信类型,取值:soft_bounce | hard_bounce,仅在event=bounce的时候生效"`
  140. Username string `description:"腾讯云账号对应的 appId"`
  141. From string `description:"发信地址(不带发件人别名)"`
  142. FromDomain string `description:"发信域名"`
  143. TemplateId int `description:"模板 Id"`
  144. }