wechat_push.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package wechat_push
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cast"
  7. "io"
  8. "net/http"
  9. "pmail/config"
  10. "pmail/dto/parsemail"
  11. "pmail/utils/context"
  12. "strings"
  13. "time"
  14. )
  15. type accessTokenRes struct {
  16. AccessToken string `db:"access_token" json:"access_token"`
  17. ExpiresIn int `db:"expires_in" json:"expires_in"`
  18. }
  19. type WeChatPushHook struct {
  20. appId string
  21. secret string
  22. token string
  23. tokenExpires int64
  24. templateId string
  25. pushUser string
  26. }
  27. func (w *WeChatPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) {
  28. }
  29. func (w *WeChatPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) {
  30. }
  31. func (w *WeChatPushHook) ReceiveParseBefore(email []byte) {
  32. }
  33. func (w *WeChatPushHook) ReceiveParseAfter(email *parsemail.Email) {
  34. if w.appId == "" || w.secret == "" || w.pushUser == "" {
  35. return
  36. }
  37. content := string(email.Text)
  38. if content == "" {
  39. content = email.Subject
  40. }
  41. w.sendUserMsg(nil, w.pushUser, content)
  42. }
  43. func (w *WeChatPushHook) getWxAccessToken() string {
  44. if w.tokenExpires > time.Now().Unix() {
  45. return w.token
  46. }
  47. resp, err := http.Get(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", w.appId, w.secret))
  48. if err != nil {
  49. return ""
  50. }
  51. body, _ := io.ReadAll(resp.Body)
  52. var ret accessTokenRes
  53. _ = json.Unmarshal(body, &ret)
  54. if ret.AccessToken != "" {
  55. w.token = ret.AccessToken
  56. w.tokenExpires = time.Now().Unix() + cast.ToInt64(ret.ExpiresIn)
  57. }
  58. return ret.AccessToken
  59. }
  60. type sendMsgRequest struct {
  61. Touser string `db:"touser" json:"touser"`
  62. Template_id string `db:"template_id" json:"template_id"`
  63. Url string `db:"url" json:"url"`
  64. Data SendData `db:"data" json:"data"`
  65. }
  66. type SendData struct {
  67. Content DataItem `json:"Content"`
  68. }
  69. type DataItem struct {
  70. Value string `json:"value"`
  71. Color string `json:"color"`
  72. }
  73. func (w *WeChatPushHook) sendUserMsg(ctx *context.Context, userId string, content string) {
  74. url := config.Instance.WebDomain
  75. if config.Instance.HttpsEnabled > 1 {
  76. url = "http://" + url
  77. } else {
  78. url = "https://" + url
  79. }
  80. sendMsgReq, _ := json.Marshal(sendMsgRequest{
  81. Touser: userId,
  82. Template_id: w.templateId,
  83. Url: url,
  84. Data: SendData{Content: DataItem{Value: content, Color: "#000000"}},
  85. })
  86. _, err := http.Post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+w.getWxAccessToken(), "application/json", strings.NewReader(string(sendMsgReq)))
  87. if err != nil {
  88. log.WithContext(ctx).Errorf("wechat push error %+v", err)
  89. }
  90. }
  91. func NewWechatPushHook() *WeChatPushHook {
  92. ret := &WeChatPushHook{
  93. appId: config.Instance.WeChatPushAppId,
  94. secret: config.Instance.WeChatPushSecret,
  95. templateId: config.Instance.WeChatPushTemplateId,
  96. pushUser: config.Instance.WeChatPushUserId,
  97. }
  98. return ret
  99. }