wework_audit_linux.go 2.9 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. 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)
  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. }
  85. }
  86. //释放SDK实例
  87. client.Free()
  88. return
  89. }