wework_audit_linux.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //go:build linux && cgo && msgaudit
  2. // +build linux,cgo,msgaudit
  3. package wework
  4. import (
  5. "bytes"
  6. "errors"
  7. "fmt"
  8. "hongze/hongze_open_api/utils"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "time"
  13. "github.com/silenceper/wechat/v2/work/msgaudit"
  14. )
  15. func (w *WeWorkMsgAuditClient) GetMsgAuditContent(seq, limit uint64, timeout int) (list []msgaudit.TextMessage, seqRes uint64, errMsg error) {
  16. client := w.Client
  17. //同步消息
  18. chatDataList, err := client.GetChatData(seq, limit, "", "", timeout)
  19. if err != nil {
  20. utils.FileLog.Info(fmt.Sprintf("企业微信 会议存档消息同步失败:%v \n\n", err))
  21. errMsg = errors.New("企业微信 会议存档消息同步失败:"+err.Error())
  22. return
  23. }
  24. if len(chatDataList) == 0 {
  25. return
  26. }
  27. filePath, _ := os.Getwd()
  28. // 创建日期文件夹
  29. date := time.Now().Format("2006-01-02")
  30. filePath = path.Join(filePath, date)
  31. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  32. os.MkdirAll(filePath, 0755)
  33. }
  34. for _, chatData := range chatDataList {
  35. seqRes = chatData.Seq
  36. //消息解密
  37. chatInfo, err := client.DecryptData(chatData.EncryptRandomKey, chatData.EncryptChatMsg)
  38. if err != nil {
  39. utils.FileLog.Info(fmt.Sprintf("消息解密失败:%v \n", err))
  40. errMsg = errors.New("企业微信 消息解密失败:"+err.Error())
  41. return
  42. }
  43. utils.FileLog.Info(fmt.Sprintf("消息解密成功:%v \n", chatInfo))
  44. if chatInfo.Type == "image" {
  45. image, _ := chatInfo.GetImageMessage()
  46. sdkFileID := image.Image.SdkFileID
  47. isFinish := false
  48. buffer := bytes.Buffer{}
  49. indexBuf := ""
  50. for !isFinish {
  51. //获取媒体数据
  52. mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
  53. if err != nil {
  54. utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
  55. errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
  56. return
  57. }
  58. buffer.Write(mediaData.Data)
  59. if mediaData.IsFinish {
  60. isFinish = mediaData.IsFinish
  61. }
  62. indexBuf = mediaData.OutIndexBuf
  63. }
  64. filePath = path.Join(filePath, image.Image.Md5Sum+".jpg")
  65. err := ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
  66. if err != nil {
  67. utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
  68. errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
  69. return
  70. }
  71. utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
  72. var textMsg msgaudit.TextMessage
  73. textMsg.MsgID = image.BaseMessage.MsgID
  74. textMsg.Action = image.BaseMessage.Action
  75. textMsg.From = image.BaseMessage.From
  76. textMsg.ToList = image.BaseMessage.ToList
  77. textMsg.RoomID = image.BaseMessage.RoomID
  78. textMsg.MsgTime = image.BaseMessage.MsgTime
  79. textMsg.Text.Content = filePath
  80. list = append(list, textMsg)
  81. continue
  82. }else if chatInfo.Type == "text" {
  83. msg, err := chatInfo.GetTextMessage()
  84. if err != nil {
  85. utils.FileLog.Info(fmt.Sprintf("查询消息类型失败:%v \n", err))
  86. errMsg = errors.New("企业微信 查询消息类型失败:"+err.Error())
  87. return
  88. }
  89. utils.FileLog.Info(fmt.Sprintf("明文:%s \n", msg))
  90. list = append(list, msg)
  91. }else if chatInfo.Type == "file" {
  92. file, err := chatInfo.GetFileMessage()
  93. sdkFileID := file.File.SdkFileID
  94. if err != nil {
  95. utils.FileLog.Info(fmt.Sprintf("查询文件消息类型失败:%v \n", err))
  96. errMsg = errors.New("企业微信 查询文件消息类型失败:"+err.Error())
  97. return
  98. }
  99. isFinish := false
  100. buffer := bytes.Buffer{}
  101. indexBuf := ""
  102. for !isFinish {
  103. //获取媒体数据
  104. mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
  105. if err != nil {
  106. utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
  107. errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
  108. return
  109. }
  110. buffer.Write(mediaData.Data)
  111. if mediaData.IsFinish {
  112. isFinish = mediaData.IsFinish
  113. }
  114. indexBuf = mediaData.OutIndexBuf
  115. }
  116. // 保存文件到本地
  117. filePath = path.Join(filePath, file.File.FileName)
  118. err = ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
  119. if err != nil {
  120. utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
  121. errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
  122. return
  123. }
  124. utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
  125. var textMsg msgaudit.TextMessage
  126. textMsg.MsgID = file.BaseMessage.MsgID
  127. textMsg.Action = file.BaseMessage.Action
  128. textMsg.From = file.BaseMessage.From
  129. textMsg.ToList = file.BaseMessage.ToList
  130. textMsg.RoomID = file.BaseMessage.RoomID
  131. textMsg.MsgTime = file.BaseMessage.MsgTime
  132. textMsg.Text.Content = filePath
  133. list = append(list, textMsg)
  134. }
  135. }
  136. //释放SDK实例
  137. client.Free()
  138. return
  139. }