wework_audit_linux.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if chatInfo.Type == "image" {
  36. image, _ := chatInfo.GetImageMessage()
  37. sdkFileID := image.Image.SdkFileID
  38. isFinish := false
  39. buffer := bytes.Buffer{}
  40. indexBuf := ""
  41. for !isFinish {
  42. //获取媒体数据
  43. mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
  44. if err != nil {
  45. utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
  46. errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
  47. return
  48. }
  49. buffer.Write(mediaData.Data)
  50. if mediaData.IsFinish {
  51. isFinish = mediaData.IsFinish
  52. }
  53. indexBuf = mediaData.OutIndexBuf
  54. }
  55. filePath, _ := os.Getwd()
  56. filePath = path.Join(filePath, image.Image.Md5Sum)
  57. err := ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
  58. if err != nil {
  59. utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
  60. errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
  61. return
  62. }
  63. utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
  64. var textMsg msgaudit.TextMessage
  65. textMsg.MsgID = image.BaseMessage.MsgID
  66. textMsg.Action = image.BaseMessage.Action
  67. textMsg.From = image.BaseMessage.From
  68. textMsg.ToList = image.BaseMessage.ToList
  69. textMsg.RoomID = image.BaseMessage.RoomID
  70. textMsg.MsgTime = image.BaseMessage.MsgTime
  71. textMsg.Text.Content = filePath
  72. list = append(list, textMsg)
  73. continue
  74. }else if chatInfo.Type == "text" {
  75. msg, err := chatInfo.GetTextMessage()
  76. if err != nil {
  77. utils.FileLog.Info(fmt.Sprintf("查询消息类型失败:%v \n", err))
  78. errMsg = errors.New("企业微信 查询消息类型失败:"+err.Error())
  79. return
  80. }
  81. utils.FileLog.Info(fmt.Sprintf("明文:%s \n", msg))
  82. list = append(list, msg)
  83. }
  84. }
  85. //释放SDK实例
  86. client.Free()
  87. return
  88. }