tencent_yun.go 5.5 KB

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