kefu/lib/wechat_kf_sdk/kf_test.go

169 lines
5.6 KiB
Go

package wechat_kf_sdk
import (
"fmt"
"kefu/tools"
"log"
"testing"
"time"
)
func TestGetAccessToken(t *testing.T) {
corpid := "wwa4266261c4ea2c08"
corpsecret := "SPU2s8t5cmI3GfkAuggiTyPk5LsuPY22hgep4cGBzlQ"
// 创建微信客服API的封装结构体实例
kefuWework := NewKefuWework(corpid, corpsecret, "", "")
// 获取access_token
accessToken, err := kefuWework.GetAccessToken()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(accessToken)
}
func TestCheckSign(t *testing.T) {
corpid := "wx5823bf96d3bd56c7"
corpsecret := "your_corpsecret"
token := "QDG6eK"
encodingAeskey := "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"
verifyMsgSign := "5c45ff5e21c57e6ad56bac8758b79b1d9ac89fd3"
// verifyTimestamp := HttpUtils.ParseUrl("timestamp")
verifyTimestamp := "1409659589"
// verifyNonce := HttpUtils.ParseUrl("nonce")
verifyNonce := "263014780"
// verifyEchoStr := HttpUtils.ParseUrl("echoStr")
verifyEchoStr := "P9nAzCzyDtyTWESHep1vC5X9xho/qYX3Zpb4yKa9SKld1DsH3Iyt3tP3zNdtp+4RPcs8TgAE7OaBO+FZXvnaqQ=="
// 创建微信客服API的封装结构体实例
wxService := NewKefuWework(corpid, corpsecret, token, encodingAeskey)
res, err := wxService.CheckSign(verifyMsgSign, verifyTimestamp, verifyNonce, verifyEchoStr)
log.Println(res, err)
}
func TestReceiveMsg(t *testing.T) {
corpid := "wx5823bf96d3bd56c7"
corpsecret := "your_corpsecret"
token := "QDG6eK"
encodingAeskey := "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"
reqMsgSign := "477715d11cdb4164915debcba66cb864d751f3e6"
// reqTimestamp := HttpUtils.ParseUrl("timestamp")
reqTimestamp := "1409659813"
// reqNonce := HttpUtils.ParseUrl("nonce")
reqNonce := "1372623149"
// post请求的密文数据
// reqData = HttpUtils.PostData()
reqData := []byte("<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt><AgentID><![CDATA[218]]></AgentID></xml>")
wxService := NewKefuWework(corpid, corpsecret, token, encodingAeskey)
msg, cryptErr := wxService.DecryptMsg(reqMsgSign, reqTimestamp, reqNonce, string(reqData))
log.Println(msg, cryptErr)
}
func TestSyncMsg(t *testing.T) {
corpid := "wwa4266261c4ea2c08"
corpsecret := "SPU2s8t5cmI3GfkAuggiTyPk5LsuPY22hgep4cGBzlQ"
// 创建微信客服API的封装结构体实例
wxService := NewKefuWework(corpid, corpsecret, "", "")
// 构建请求数据
reqData := map[string]interface{}{
//"limit": 1,
//"cursor": "4gw7MepFLfgF2VC5nou",
}
list, err := wxService.SyncMsg(reqData)
size := len(list.MsgList)
log.Println(list.MsgList[size-1], err)
}
func TestDownloadTempFileByMediaID(t *testing.T) {
corpid := "wwa4266261c4ea2c08"
corpsecret := "SPU2s8t5cmI3GfkAuggiTyPk5LsuPY22hgep4cGBzlQ"
// 创建微信客服API的封装结构体实例
mediaId := "1KJIrgILRuqG2rkDLW4Zg5vXjlUuuDfI10REf6NJ7t3GrUxUGIUVmR1Y8KlsnS89U"
wxService := NewKefuWework(corpid, corpsecret, "", "")
fileName := tools.Md5(fmt.Sprintf("%s%s", mediaId, time.Now().String())) + ".jpg"
//fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
//isExist, _ := tools.IsFileExist(fildDir)
//if !isExist {
// os.Mkdir(fildDir, os.ModePerm)
//}
filepath := fmt.Sprintf("%s", fileName)
res, err := wxService.DownloadTempFileByMediaID(mediaId, filepath)
log.Println(res, err)
}
func TestGetAccountList(t *testing.T) {
corpid := "wwa4266261c4ea2c08"
corpsecret := "SPU2s8t5cmI3GfkAuggiTyPk5LsuPY22hgep4cGBzlQ"
// 创建微信客服API的封装结构体实例
wxService := NewKefuWework(corpid, corpsecret, "", "")
// 调用GetAccountList函数
accountList, err := wxService.GetAccountList(0, 1000)
if err != nil {
t.Errorf("GetAccountList failed: %v", err)
return
}
t.Logf("GetAccountList result: %v", accountList)
}
func TestBatchGet(t *testing.T) {
corpid := "wwa4266261c4ea2c08"
corpsecret := "SPU2s8t5cmI3GfkAuggiTyPk5LsuPY22hgep4cGBzlQ"
// 创建微信客服API的封装结构体实例
wxService := NewKefuWework(corpid, corpsecret, "", "")
// 构建请求数据
reqData := map[string]interface{}{
"external_userid_list": []string{
"wmPowHDwAAnmJ72xZNnZrNz578Aqx6Kg",
},
}
list, err := wxService.BatchGet(reqData)
log.Println(list, err)
}
func TestSendMsg(t *testing.T) {
corpid := "wwa4266261c4ea2c08"
corpsecret := "SPU2s8t5cmI3GfkAuggiTyPk5LsuPY22hgep4cGBzlQ"
// 创建微信客服API的封装结构体实例
kefuWework := NewKefuWework(corpid, corpsecret, "", "")
// 构建回复消息
reply2 := SendMsgText{
Touser: "wmPowHDwAAnmJ72xZNnZrNz578Aqx6Kg",
OpenKfid: "wkPowHDwAAqbR670TBHFPPhCbOQkCnSw",
Msgid: "",
Msgtype: "text",
Text: struct {
Content string `json:"content,omitempty"`
}{
Content: "Hello, this is a test message",
},
}
//构建图片消息
/*
mediaId := "1eJFu2-vMr3Q5QQhadCjdR88HR6DD46rJ0LBHgRxZ8iEjIw5pzUZ8C8DRKbQGvWS4"
reply2 := SendMsgText{
Touser: "wmPowHDwAAnmJ72xZNnZrNz578Aqx6Kg",
OpenKfid: "wkPowHDwAAqbR670TBHFPPhCbOQkCnSw",
Msgid: "",
Msgtype: "image",
Image: struct {
MediaId string `json:"media_id,omitempty"`
}{
MediaId: mediaId,
},
}
*/
// 调用SendMsg方法发送消息
err := kefuWework.SendMsg(reply2)
if err != nil {
t.Errorf("SendMsg failed: %v", err)
return
}
t.Logf("SendMsg succeeded")
}