123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //go:build linux && cgo && msgaudit
- // +build linux,cgo,msgaudit
- package wework
- import (
- "bytes"
- "errors"
- "fmt"
- "hongze/hongze_open_api/utils"
- "io/ioutil"
- "os"
- "path"
- "time"
- "github.com/silenceper/wechat/v2/work/msgaudit"
- )
- func (w *WeWorkMsgAuditClient) GetMsgAuditContent(seq, limit uint64, timeout int) (list []msgaudit.TextMessage, seqRes uint64, errMsg error) {
- client := w.Client
- //同步消息
- chatDataList, err := client.GetChatData(seq, limit, "", "", timeout)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("企业微信 会议存档消息同步失败:%v \n\n", err))
- errMsg = errors.New("企业微信 会议存档消息同步失败:"+err.Error())
- return
- }
- if len(chatDataList) == 0 {
- return
- }
- filePath, _ := os.Getwd()
- // 创建日期文件夹
- date := time.Now().Format("2006-01-02")
- filePath = path.Join(filePath, date)
- if _, err := os.Stat(filePath); os.IsNotExist(err) {
- os.MkdirAll(filePath, 0755)
- }
- for _, chatData := range chatDataList {
- seqRes = chatData.Seq
- //消息解密
- chatInfo, err := client.DecryptData(chatData.EncryptRandomKey, chatData.EncryptChatMsg)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("消息解密失败:%v \n", err))
- errMsg = errors.New("企业微信 消息解密失败:"+err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("消息解密成功:%v \n", chatInfo))
- if chatInfo.Type == "image" {
- image, _ := chatInfo.GetImageMessage()
- sdkFileID := image.Image.SdkFileID
- isFinish := false
- buffer := bytes.Buffer{}
- indexBuf := ""
- for !isFinish {
- //获取媒体数据
- mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
- errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
- return
- }
- buffer.Write(mediaData.Data)
- if mediaData.IsFinish {
- isFinish = mediaData.IsFinish
- }
- indexBuf = mediaData.OutIndexBuf
- }
-
- filePath = path.Join(filePath, image.Image.Md5Sum+".jpg")
- err := ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
- errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
- var textMsg msgaudit.TextMessage
- textMsg.MsgID = image.BaseMessage.MsgID
- textMsg.Action = image.BaseMessage.Action
- textMsg.From = image.BaseMessage.From
- textMsg.ToList = image.BaseMessage.ToList
- textMsg.RoomID = image.BaseMessage.RoomID
- textMsg.MsgTime = image.BaseMessage.MsgTime
- textMsg.Text.Content = filePath
- list = append(list, textMsg)
-
- continue
- }else if chatInfo.Type == "text" {
- msg, err := chatInfo.GetTextMessage()
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("查询消息类型失败:%v \n", err))
- errMsg = errors.New("企业微信 查询消息类型失败:"+err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("明文:%s \n", msg))
- list = append(list, msg)
- }else if chatInfo.Type == "file" {
- file, err := chatInfo.GetFileMessage()
- sdkFileID := file.File.SdkFileID
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("查询文件消息类型失败:%v \n", err))
- errMsg = errors.New("企业微信 查询文件消息类型失败:"+err.Error())
- return
- }
- isFinish := false
- buffer := bytes.Buffer{}
- indexBuf := ""
- for !isFinish {
- //获取媒体数据
- mediaData, err := client.GetMediaData(indexBuf, sdkFileID, "", "", 5)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("媒体数据拉取失败:%v \n", err))
- errMsg = errors.New("企业微信 媒体数据拉取失败:"+err.Error())
- return
- }
- buffer.Write(mediaData.Data)
- if mediaData.IsFinish {
- isFinish = mediaData.IsFinish
- }
- indexBuf = mediaData.OutIndexBuf
- }
- // 保存文件到本地
- filePath = path.Join(filePath, file.File.FileName)
- err = ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("文件存储失败:%v \n", err))
- errMsg = errors.New("企业微信 文件存储失败:"+err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("文件存储成功:%s \n", filePath))
- var textMsg msgaudit.TextMessage
- textMsg.MsgID = file.BaseMessage.MsgID
- textMsg.Action = file.BaseMessage.Action
- textMsg.From = file.BaseMessage.From
- textMsg.ToList = file.BaseMessage.ToList
- textMsg.RoomID = file.BaseMessage.RoomID
- textMsg.MsgTime = file.BaseMessage.MsgTime
- textMsg.Text.Content = filePath
- list = append(list, textMsg)
- }
- }
- //释放SDK实例
- client.Free()
- return
- }
|