tencent_yun.go 5.4 KB

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