Skip to content

Commit

Permalink
新增互通红包消息
Browse files Browse the repository at this point in the history
  • Loading branch information
NICEXAI committed Feb 24, 2021
1 parent b1f1a86 commit 47b83c7
Show file tree
Hide file tree
Showing 10 changed files with 729 additions and 527 deletions.
36 changes: 18 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_Store

/.idea
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_Store

/.idea
402 changes: 201 additions & 201 deletions LICENSE

Large diffs are not rendered by default.

134 changes: 84 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,84 @@
# WeWorkFinanceSDK
企业微信会话存档SDK(基于企业微信C版官方SDK封装),暂时只支持在`linux`环境下使用当前SDK。

### 使用方式

`clone`项目到自己项目内并`cd``WeWorkFinanceSDK/lib`文件夹内执行`export LD_LIBRARY_PATH=$(pwd)`命令设置动态链接库检索地址,然后在项目内引入当前包即可直接使用。

### Example

```go
package main

import (
"encoding/json"
"fmt"
"github.com/NICEXAI/WeWorkFinanceSDK"
)

func main() {
corpID := "xxxxxxxxxxxx"
corpSecret := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
rsaPrivateKey := `
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
`
//初始化客户端
client, err := WeWorkFinanceSDK.NewClient(corpID, corpSecret, rsaPrivateKey)
if err != nil {
fmt.Printf("SDK 初始化失败:%v \n", err)
return
}
//获取消息
chatDataList, err := client.GetChatData(0, 100, "", "", 3)
if err != nil {
fmt.Printf("消息同步失败:%v \n", err)
return
}
for _, chatData := range chatDataList {
//消息解密
chatInfo, err := client.DecryptData(chatData.EncryptRandomKey, chatData.EncryptChatMsg)
if err != nil {
fmt.Printf("消息解密失败:%v \n", err)
return
}
str, _ := json.Marshal(chatInfo)
fmt.Println(string(str))
}
}
```
# WeWorkFinanceSDK
企业微信会话存档SDK(基于企业微信C版官方SDK封装),暂时只支持在`linux`环境下使用当前SDK。

### 官方文档地址
https://open.work.weixin.qq.com/api/doc/90000/90135/91774

### 使用方式

`clone`项目到自己项目内并`cd``WeWorkFinanceSDK/lib`文件夹内执行`export LD_LIBRARY_PATH=$(pwd)`命令设置动态链接库检索地址,然后在项目内引入当前包即可直接使用。

### Example

```go
package main

import (
"bytes"
"fmt"
"github.com/NICEXAI/WeWorkFinanceSDK"
"io/ioutil"
"os"
"path"
)

func main() {
corpID := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
corpSecret := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rsaPrivateKey := `XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`

//初始化客户端
client, err := WeWorkFinanceSDK.NewClient(corpID, corpSecret, rsaPrivateKey)
if err != nil {
fmt.Printf("SDK 初始化失败:%v \n", err)
return
}

//同步消息
chatDataList, err := client.GetChatData(0, 100, "", "", 3)
if err != nil {
fmt.Printf("消息同步失败:%v \n", err)
return
}

for _, chatData := range chatDataList {
//消息解密
chatInfo, err := client.DecryptData(chatData.EncryptRandomKey, chatData.EncryptChatMsg)
if err != nil {
fmt.Printf("消息解密失败:%v \n", err)
return
}

if chatInfo.Type == "image" {
image := chatInfo.GetImageMessage()
sdkfileid := image.Image.SdkFileId

isFinish := false
buffer := bytes.Buffer{}
for !isFinish {
//获取媒体数据
mediaData, err := client.GetMediaData("", sdkfileid, "", "", 5)
if err != nil {
fmt.Printf("媒体数据拉取失败:%v \n", err)
return
}
buffer.Write(mediaData.Data)
if mediaData.IsFinish {
isFinish = mediaData.IsFinish
}
}
filePath, _ := os.Getwd()
filePath = path.Join(filePath, "test.png")
err := ioutil.WriteFile(filePath, buffer.Bytes(), 0666)
if err != nil {
fmt.Printf("文件存储失败:%v \n", err)
return
}
break
}
}
}



```
148 changes: 146 additions & 2 deletions chatdata.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package WeWorkFinanceSDK

import "encoding/json"

type ChatDataResponse struct {
Error
ChatDataList []ChatData `json:"chatdata,omitempty"`
}

func (this ChatDataResponse) IsError() bool {
return this.ErrCode != 0
func (c ChatDataResponse) IsError() bool {
return c.ErrCode != 0
}

type ChatData struct {
Expand All @@ -16,3 +18,145 @@ type ChatData struct {
EncryptRandomKey string `json:"encrypt_random_key,omitempty"` // 使用publickey_ver指定版本的公钥进行非对称加密后base64加密的内容,需要业务方先base64 decode处理后,再使用指定版本的私钥进行解密,得出内容。
EncryptChatMsg string `json:"encrypt_chat_msg,omitempty"` // 消息密文。需要业务方使用将encrypt_random_key解密得到的内容,与encrypt_chat_msg,传入sdk接口DecryptData,得到消息明文。
}

type ChatMessage struct {
Id string // 消息id,消息的唯一标识,企业可以使用此字段进行消息去重。
Action string // 消息动作,目前有send(发送消息)/recall(撤回消息)/switch(切换企业日志)三种类型。
Type string // 消息类型
originData []byte // 原始消息对象
}

func (c ChatMessage) GetTextMessage() (msg TextMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetImageMessage() (msg ImageMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetRevokeMessage() (msg RevokeMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetAgreeMessage() (msg AgreeMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetVoiceMessage() (msg VoiceMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetVideoMessage() (msg VideoMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetCardMessage() (msg CardMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetLocationMessage() (msg LocationMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetEmotionMessage() (msg EmotionMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetFileMessage() (msg FileMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetLinkMessage() (msg LinkMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetWeappMessage() (msg WeappMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetChatRecordMessage() (msg ChatRecordMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetTodoMessage() (msg TodoMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetVoteMessage() (msg VoteMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetCollectMessage() (msg CollectMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetRedpacketMessage() (msg RedpacketMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetMeetingMessage() (msg MeetingMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetDocMessage() (msg DocMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetMarkdownMessage() (msg MarkdownMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetNewsMessage() (msg NewsMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetCalendarMessage() (msg CalendarMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetMixedMessage() (msg MixedMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetMeetingVoiceCallMessage() (msg MeetingVoiceCallMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetVoipDocShareMessage() (msg VoipDocShareMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetExternalRedPacketMessage() (msg ExternalRedPacketMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}

func (c ChatMessage) GetSwitchMessage() (msg SwitchMessage) {
_ = json.Unmarshal(c.originData, &msg)
return msg
}
14 changes: 8 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ func (s *Client) GetChatData(seq uint64, limit uint64, proxy string, passwd stri
* 0 - 成功
* !=0 - 失败
*/
func (s *Client) DecryptData(encrypt_random_key string, encryptMsg string) (msg map[string]interface{}, err error) {
func (s *Client) DecryptData(encrypt_random_key string, encryptMsg string) (msg ChatMessage, err error) {
encryptKey, err := RSADecryptBase64(s.privateKey, encrypt_random_key)
if err != nil {
return nil, NewSDKErr(10006)
return msg, NewSDKErr(10006)
}
encryptKeyC := C.CString(string(encryptKey))
encryptMsgC := C.CString(encryptMsg)
Expand All @@ -116,16 +116,18 @@ func (s *Client) DecryptData(encrypt_random_key string, encryptMsg string) (msg
retC := C.DecryptData(encryptKeyC, encryptMsgC, msgSlice)
ret := int(retC)
if ret != 0 {
return nil, NewSDKErr(ret)
return msg, NewSDKErr(ret)
}
buf := s.GetContentFromSlice(msgSlice)
var baseMessage BaseMessage
err = json.Unmarshal(buf, &baseMessage)
if err != nil {
return nil, err
return msg, err
}

err = json.Unmarshal(buf, &msg)
msg.Id = baseMessage.MsgId
msg.Action = baseMessage.Action
msg.Type = baseMessage.MsgType
msg.originData = buf
return msg, err
}

Expand Down
Loading

0 comments on commit 47b83c7

Please sign in to comment.