wework_audit_linux.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "github.com/silenceper/wechat/v2/work/msgaudit"
  13. )
  14. func (w *WeWorkMsgAuditClient) GetMsgAuditContent(seq, limit uint64, timeout int) (list []msgaudit.TextMessage, seqRes uint64, errMsg error) {
  15. client := w.Client
  16. //同步消息
  17. chatDataList, err := client.GetChatData(seq, limit, "", "", timeout)
  18. if err != nil {
  19. utils.FileLog.Info(fmt.Sprintf("企业微信 会议存档消息同步失败:%v \n\n", err))
  20. errMsg = errors.New("企业微信 会议存档消息同步失败:"+err.Error())
  21. return
  22. }
  23. if len(chatDataList) == 0 {
  24. return
  25. }
  26. for _, chatData := range chatDataList {
  27. seqRes = chatData.Seq
  28. //消息解密
  29. chatInfo, err := client.DecryptData(chatData.EncryptRandomKey, chatData.EncryptChatMsg)
  30. if err != nil {
  31. utils.FileLog.Info(fmt.Sprintf("消息解密失败:%v \n", err))
  32. errMsg = errors.New("企业微信 消息解密失败:"+err.Error())
  33. return
  34. }
  35. utils.FileLog.Info(fmt.Sprintf("消息解密成功:%v \n", chatInfo))
  36. if chatInfo.Type == "image" {
  37. image, _ := chatInfo.GetImageMessage()
  38. sdkFileID := image.Image.SdkFileID
  39. isFinish := false
  40. buffer := bytes.Buffer{}
  41. indexBuf := ""
  42. for !isFinish {
  43. //获取媒体数据
  44. mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
  45. if err != nil {
  46. utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
  47. errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
  48. return
  49. }
  50. buffer.Write(mediaData.Data)
  51. if mediaData.IsFinish {
  52. isFinish = mediaData.IsFinish
  53. }
  54. indexBuf = mediaData.OutIndexBuf
  55. }
  56. filePath, _ := os.Getwd()
  57. filePath = path.Join(filePath, image.Image.Md5Sum+".jpg")
  58. err := ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
  59. if err != nil {
  60. utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
  61. errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
  62. return
  63. }
  64. utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
  65. var textMsg msgaudit.TextMessage
  66. textMsg.MsgID = image.BaseMessage.MsgID
  67. textMsg.Action = image.BaseMessage.Action
  68. textMsg.From = image.BaseMessage.From
  69. textMsg.ToList = image.BaseMessage.ToList
  70. textMsg.RoomID = image.BaseMessage.RoomID
  71. textMsg.MsgTime = image.BaseMessage.MsgTime
  72. textMsg.Text.Content = filePath
  73. list = append(list, textMsg)
  74. continue
  75. }else if chatInfo.Type == "text" {
  76. msg, err := chatInfo.GetTextMessage()
  77. if err != nil {
  78. utils.FileLog.Info(fmt.Sprintf("查询消息类型失败:%v \n", err))
  79. errMsg = errors.New("企业微信 查询消息类型失败:"+err.Error())
  80. return
  81. }
  82. utils.FileLog.Info(fmt.Sprintf("明文:%s \n", msg))
  83. list = append(list, msg)
  84. }else if chatInfo.Type == "file" {
  85. file, err := chatInfo.GetFileMessage()
  86. if err != nil {
  87. utils.FileLog.Info(fmt.Sprintf("查询文件消息类型失败:%v \n", err))
  88. errMsg = errors.New("企业微信 查询文件消息类型失败:"+err.Error())
  89. return
  90. }
  91. // 保存文件到本地
  92. filePath, _ := os.Getwd()
  93. filePath = path.Join(filePath, file.File.FileName+"."+file.File.FileExt)
  94. err := ioutil.WriteFile(filePath, file.File.Data, 0666)
  95. if err != nil {
  96. utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
  97. errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
  98. return
  99. }
  100. utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
  101. var textMsg msgaudit.TextMessage
  102. textMsg.MsgID = file.BaseMessage.MsgID
  103. textMsg.Action = file.BaseMessage.Action
  104. textMsg.From = file.BaseMessage.From
  105. textMsg.ToList = file.BaseMessage.ToList
  106. textMsg.RoomID = file.BaseMessage.RoomID
  107. textMsg.MsgTime = file.BaseMessage.MsgTime
  108. textMsg.Text.Content = filePath
  109. list = append(list, textMsg)
  110. }
  111. }
  112. //释放SDK实例
  113. client.Free()
  114. return
  115. }