wechat_push.go 2.8 KB

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