sync message

This commit is contained in:
jiangyong27 2024-01-23 14:48:03 +08:00
parent dfca7748b5
commit 2af9f91228
1 changed files with 134 additions and 0 deletions

View File

@ -1,5 +1,32 @@
package qyweixin
import (
"encoding/json"
"fmt"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"gitlab.batiao8.com/open/gosdk/util"
"gitlab.batiao8.com/open/gosdk/wechat/message"
"time"
)
type CustomerAccount struct {
Name string
KfId string
}
type CustomerMessage struct {
Msgid string
Msgtype string
ExternalUserid string
ServicerUserid string
OpenKfid string
OpenKfname string
Origin int
SendTime int64
SyncTime int64
Content string
}
type AppCustomer struct {
App
}
@ -9,3 +36,110 @@ func NewAppCustomer(cfg *AppConfig) *AppCustomer {
App: *NewApp(cfg),
}
}
func (a *AppCustomer) AccountList() ([]*CustomerAccount, error) {
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/kf/account/list?access_token=%s", a.GetToken())
rspBody, err := util.HttpGet(reqUrl, nil)
if err != nil {
return nil, err
}
result, err := a.GetResult(rspBody)
if err != nil {
return nil, err
}
data := cast.ToSlice(result["account_list"])
accounts := make([]*CustomerAccount, 0)
for _, ac := range data {
acc := cast.ToStringMap(ac)
account := new(CustomerAccount)
account.Name = cast.ToString(acc["name"])
account.KfId = cast.ToString(acc["open_kfid"])
accounts = append(accounts, account)
}
return accounts, nil
}
func (a *AppCustomer) SyncMessage(cursor, token, openKfId string) ([]*CustomerMessage, string, error) {
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token=%s", a.GetToken())
params := make(map[string]interface{})
params["token"] = token
params["token"] = token
params["limit"] = 1000
params["voice_format"] = 0
params["open_kfid"] = openKfId
message := make([]*CustomerMessage, 0)
for {
params["cursor"] = cursor
reqBody, _ := json.Marshal(params)
rspBody, err := util.HttpPostJson(reqUrl, nil, reqBody)
if err != nil {
return nil, "", err
}
result, err := a.GetResult(rspBody)
if err != nil {
return nil, "", err
}
msgList := cast.ToSlice(result["msg_list"])
for _, msg := range msgList {
m := new(CustomerMessage)
m.From(cast.ToStringMap(msg))
message = append(message, m)
}
cursor = cast.ToString(result["next_cursor"])
hasMore := cast.ToInt(result["has_more"])
if hasMore == 0 {
break
}
}
return message, cursor, nil
}
func (m *CustomerMessage) From(data map[string]interface{}) {
m.SyncTime = time.Now().Unix()
m.SendTime = cast.ToInt64(data["send_time"])
m.Msgid = cast.ToString(data["msgid"])
m.Msgtype = cast.ToString(data["msgtype"])
m.Origin = cast.ToInt(data["origin"])
m.ExternalUserid = cast.ToString(data["external_userid"])
m.ServicerUserid = cast.ToString(data["servicer_userid"])
m.OpenKfid = cast.ToString(data["open_kfid"])
if m.Msgtype == message.MsgTypeEvent {
m.parseEvent(data)
} else {
m.parseMessage(m.Msgtype, data)
}
}
func (m *CustomerMessage) parseEvent(data map[string]interface{}) error {
event, err := cast.ToStringMapE(data["event"])
if err != nil {
return err
}
externalUserid := cast.ToString(event["external_userid"])
if externalUserid != "" {
m.ExternalUserid = externalUserid
}
openKfid := cast.ToString(event["open_kfid"])
if openKfid != "" {
m.OpenKfid = openKfid
}
m.Content = goutil.EncodeJSON(event)
return nil
}
func (m *CustomerMessage) parseMessage(msgKey string, data map[string]interface{}) error {
image, err := cast.ToStringMapE(data[msgKey])
if err != nil {
return err
}
m.Content = goutil.EncodeJSON(image)
return nil
}