Skip to content

bububa/WeWorkFinanceSDK

Repository files navigation

企业微信会话内容存档SDK golang版

Go Reference Go goreleaser GitHub go.mod Go version of a Go module GoReportCard GitHub license GitHub release

用于拉取企业聊天记录与媒体消息,该SDK是对官方linux c库的封装

Usage

package main

import (
    "log"
    "fmt"
    "bytes"

    sdk "github.com/bububa/WeWorkFinanceSDK"
)

func main() {
    corpId := "企业ID"
    corpSecret := "secret"
    clt, err := sdk.NewClient(corpId, corpSecret)
    if err != nil {
        log.Fatalln(err)
    }
    defer clt.Destroy()
    var (
        seq uint64 
        limit uint64 = 1000
        proxy = ""
        passwd = ""
        timeout = 300
    )
    for {
        chatsData, err := clt.GetChatData(seq, limit, proxy, passwd, timeout)
        if err != nil {
            log.Fatalln(err)
        }
        if len(chatsData) == 0 {
            break
        }
        // iterate chatsData
        for _, data := range chatsData {
            // update seq with chatdata.Seq for next loop
            seq = data.Seq
            // decrypt chat data
            msg, err := clt.DecryptData(data.EncryptRandomKey, data.EncryptChatMsg)
            if err != nil {
                log.Fatalln(err)
                continue
            }
            fmt.Printf("msg: %+v\n", msg)
            if msg.MessageType() == sdk.IMG_MSG {
                var (
                    w = new(bytes.Buffer) 
                    sdkField = msg.(sdk.ImageMessage).SdkFileId
                )
                err := clt.DownloadMedia(w, sdkField, proxy, passwd, timeout)
                if err != nil {
                    log.Fatalln(err)
                    continue
                }
                log.Printf("download mediadata, len:%d bytes\n", w.Len())
            }
        }
    }
}

Reference

企业微信获取会话内容文档