wework_audit_linux.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //go:build linux && cgo && msgaudit
  2. // +build linux,cgo,msgaudit
  3. package wework
  4. import (
  5. "errors"
  6. "fmt"
  7. "github.com/silenceper/wechat/v2/work/msgaudit"
  8. "hongze/hongze_open_api/utils"
  9. )
  10. func (w *WeWorkMsgAuditClient) GetMsgAuditContent(seq, limit uint64, timeout int) (list []msgaudit.TextMessage, seqRes uint64, errMsg error) {
  11. client := w.Client
  12. //同步消息
  13. chatDataList, err := client.GetChatData(seq, limit, "", "", timeout)
  14. if err != nil {
  15. utils.FileLog.Info(fmt.Sprintf("企业微信 会议存档消息同步失败:%v \n\n", err))
  16. errMsg = errors.New("企业微信 会议存档消息同步失败:"+err.Error())
  17. return
  18. }
  19. if len(chatDataList) == 0 {
  20. return
  21. }
  22. for _, chatData := range chatDataList {
  23. seqRes = chatData.Seq
  24. //消息解密
  25. chatInfo, err := client.DecryptData(chatData.EncryptRandomKey, chatData.EncryptChatMsg)
  26. if err != nil {
  27. utils.FileLog.Info(fmt.Sprintf("消息解密失败:%v \n", err))
  28. errMsg = errors.New("企业微信 消息解密失败:"+err.Error())
  29. return
  30. }
  31. if chatInfo.Type == "image" {
  32. image, _ := chatInfo.GetImageMessage()
  33. sdkFileID := image.Image.SdkFileID
  34. isFinish := false
  35. buffer := bytes.Buffer{}
  36. indexBuf := ""
  37. for !isFinish {
  38. //获取媒体数据
  39. mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
  40. if err != nil {
  41. utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
  42. errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
  43. return
  44. }
  45. buffer.Write(mediaData.Data)
  46. if mediaData.IsFinish {
  47. isFinish = mediaData.IsFinish
  48. }
  49. indexBuf = mediaData.OutIndexBuf
  50. }
  51. filePath, _ := os.Getwd()
  52. filePath = path.Join(filePath, image.Image.FileName)
  53. err := ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
  54. if err != nil {
  55. utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
  56. errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
  57. return
  58. }
  59. utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
  60. list = append(list, msgaudit.TextMessage{
  61. MsgId: image.MsgID,
  62. Action: image.Action,
  63. From: image.From,
  64. ToList: image.ToList,
  65. RoomId: image.RoomID,
  66. MsgTime: image.MsgTime,
  67. MsgType: image.MsgType,
  68. Content: filePath,
  69. })
  70. continue
  71. }else if chatInfo.Type == "text" {
  72. msg, err := chatInfo.GetTextMessage()
  73. if err != nil {
  74. utils.FileLog.Info(fmt.Sprintf("查询消息类型失败:%v \n", err))
  75. errMsg = errors.New("企业微信 查询消息类型失败:"+err.Error())
  76. return
  77. }
  78. utils.FileLog.Info(fmt.Sprintf("明文:%s \n", msg))
  79. list = append(list, msg)
  80. }
  81. }
  82. //释放SDK实例
  83. client.Free()
  84. return
  85. }