wechat_push.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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"
  11. "pmail/dto/parsemail"
  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 *dto.Context, email *parsemail.Email) {
  28. }
  29. func (w *WeChatPushHook) SendAfter(ctx *dto.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. w.sendUserMsg(nil, w.pushUser, string(email.Text))
  38. }
  39. func (w *WeChatPushHook) getWxAccessToken() string {
  40. if w.tokenExpires > time.Now().Unix() {
  41. return w.token
  42. }
  43. 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))
  44. if err != nil {
  45. return ""
  46. }
  47. body, _ := io.ReadAll(resp.Body)
  48. var ret accessTokenRes
  49. _ = json.Unmarshal(body, &ret)
  50. if ret.AccessToken != "" {
  51. w.token = ret.AccessToken
  52. w.tokenExpires = time.Now().Unix() + cast.ToInt64(ret.ExpiresIn)
  53. }
  54. return ret.AccessToken
  55. }
  56. type sendMsgRequest struct {
  57. Touser string `db:"touser" json:"touser"`
  58. Template_id string `db:"template_id" json:"template_id"`
  59. Url string `db:"url" json:"url"`
  60. Data SendData `db:"data" json:"data"`
  61. }
  62. type SendData struct {
  63. Content DataItem `json:"Content"`
  64. }
  65. type DataItem struct {
  66. Value string `json:"value"`
  67. Color string `json:"color"`
  68. }
  69. func (w *WeChatPushHook) sendUserMsg(ctx *dto.Context, userId string, content string) {
  70. sendMsgReq, _ := json.Marshal(sendMsgRequest{
  71. Touser: userId,
  72. Template_id: w.templateId,
  73. Url: "http://mail." + config.Instance.Domain,
  74. Data: SendData{Content: DataItem{Value: content, Color: "#000000"}},
  75. })
  76. _, err := http.Post("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+w.getWxAccessToken(), "application/json", strings.NewReader(string(sendMsgReq)))
  77. if err != nil {
  78. log.WithContext(ctx).Errorf("wechat push error %+v", err)
  79. }
  80. }
  81. func NewWechatPushHook() *WeChatPushHook {
  82. ret := &WeChatPushHook{
  83. appId: config.Instance.WeChatPushAppId,
  84. secret: config.Instance.WeChatPushSecret,
  85. templateId: config.Instance.WeChatPushTemplateId,
  86. pushUser: config.Instance.WeChatPushUserId,
  87. }
  88. return ret
  89. }