wechat-corp-account.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package channel
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "message-pusher/common"
  8. "message-pusher/model"
  9. "net/http"
  10. "time"
  11. )
  12. type wechatCorpAccountResponse struct {
  13. ErrorCode int `json:"errcode"`
  14. ErrorMessage string `json:"errmsg"`
  15. AccessToken string `json:"access_token"`
  16. ExpiresIn int `json:"expires_in"`
  17. }
  18. type WeChatCorpAccountTokenStoreItem struct {
  19. CorpId string
  20. CorpSecret string
  21. AgentId string
  22. AccessToken string
  23. }
  24. func (i *WeChatCorpAccountTokenStoreItem) Key() string {
  25. return i.CorpId + i.AgentId + i.CorpSecret
  26. }
  27. func (i *WeChatCorpAccountTokenStoreItem) Token() string {
  28. return i.AccessToken
  29. }
  30. func (i *WeChatCorpAccountTokenStoreItem) Refresh() {
  31. // https://work.weixin.qq.com/api/doc/90000/90135/91039
  32. client := http.Client{
  33. Timeout: 5 * time.Second,
  34. }
  35. req, err := http.NewRequest("GET", fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s",
  36. i.CorpId, i.CorpSecret), nil)
  37. if err != nil {
  38. common.SysError(err.Error())
  39. return
  40. }
  41. responseData, err := client.Do(req)
  42. if err != nil {
  43. common.SysError("failed to refresh access token: " + err.Error())
  44. return
  45. }
  46. defer responseData.Body.Close()
  47. var res wechatCorpAccountResponse
  48. err = json.NewDecoder(responseData.Body).Decode(&res)
  49. if err != nil {
  50. common.SysError("failed to decode wechatCorpAccountResponse: " + err.Error())
  51. return
  52. }
  53. if res.ErrorCode != 0 {
  54. common.SysError(res.ErrorMessage)
  55. return
  56. }
  57. i.AccessToken = res.AccessToken
  58. common.SysLog("access token refreshed")
  59. }
  60. type wechatCorpMessageRequest struct {
  61. MessageType string `json:"msgtype"`
  62. ToUser string `json:"touser"`
  63. AgentId string `json:"agentid"`
  64. TextCard struct {
  65. Title string `json:"title"`
  66. Description string `json:"description"`
  67. URL string `json:"url"`
  68. } `json:"textcard"`
  69. Text struct {
  70. Content string `json:"content"`
  71. } `json:"text"`
  72. Markdown struct {
  73. Content string `json:"content"`
  74. } `json:"markdown"`
  75. }
  76. type wechatCorpMessageResponse struct {
  77. ErrorCode int `json:"errcode"`
  78. ErrorMessage string `json:"errmsg"`
  79. }
  80. func SendWeChatCorpMessage(message *Message, user *model.User) error {
  81. if user.WeChatCorpAccountId == "" {
  82. return errors.New("未配置微信企业号消息推送方式")
  83. }
  84. // https://developer.work.weixin.qq.com/document/path/90236
  85. messageRequest := wechatCorpMessageRequest{
  86. ToUser: user.WeChatCorpAccountUserId,
  87. AgentId: user.WeChatCorpAccountAgentId,
  88. }
  89. if message.Content == "" {
  90. messageRequest.MessageType = "text"
  91. messageRequest.Text.Content = message.Description
  92. } else {
  93. if user.WeChatCorpAccountClientType == "plugin" {
  94. messageRequest.MessageType = "textcard"
  95. messageRequest.TextCard.Title = message.Title
  96. messageRequest.TextCard.Description = message.Description
  97. // TODO: render content and set URL
  98. messageRequest.TextCard.URL = common.ServerAddress
  99. } else {
  100. messageRequest.MessageType = "markdown"
  101. messageRequest.Markdown.Content = message.Content
  102. }
  103. }
  104. jsonData, err := json.Marshal(messageRequest)
  105. if err != nil {
  106. return err
  107. }
  108. key := fmt.Sprintf("%s%s%s", user.WeChatCorpAccountId, user.WeChatCorpAccountAgentId, user.WeChatCorpAccountSecret)
  109. accessToken := TokenStoreGetToken(key)
  110. resp, err := http.Post(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s", accessToken), "application/json",
  111. bytes.NewBuffer(jsonData))
  112. if err != nil {
  113. return err
  114. }
  115. var res wechatCorpMessageResponse
  116. err = json.NewDecoder(resp.Body).Decode(&res)
  117. if err != nil {
  118. return err
  119. }
  120. if res.ErrorCode != 0 {
  121. return errors.New(res.ErrorMessage)
  122. }
  123. return nil
  124. }