1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-23 13:42:25 +08:00

Compare commits

..

9 Commits

Author SHA1 Message Date
markwang
49c4cfaf54 fix:GetAccessTokenContext从cache中获取字符窜为空时,从微信服务器获取 (#721)
Co-authored-by: markwang <www.wang61@qq.com>
2023-09-24 10:47:43 +08:00
曹晶
ead8a6fadb fix(work): fix UserGet api error userid not found (#723)
fix UserGet api error userid not found
2023-09-24 10:46:38 +08:00
曹晶
ae40639b56 feat(work): add DepartmentGet api (#718)
get single department detail
2023-09-24 10:46:18 +08:00
曹晶
8bb145155e fix(work): fix GroupChatMember struct without State (#717)
fix GroupChatMember struct without State in joinway config
2023-09-24 10:45:43 +08:00
febelery
85bf989242 feat: 添加发放红包接口 (#726)
* feat: 添加发放红包接口

* feat: 添加发放红包接口

* chore: golang ci lint

---------

Co-authored-by: ross <ross@ross.ross>
2023-09-24 10:44:18 +08:00
Feng
b4f2d1793c feat: Add Checkin (#719)
* feat: Add Checkin

- Implement 'getcheckindata' API

* refactor: Change variable names

* refactor: Change struct name
2023-09-24 10:43:41 +08:00
曹晶
4a2c44c7c8 Feature/upload attachment (#720)
* feat(work): add UploadAttachment API

add UploadAttachment API

* feat(work): add UploadAttachment API

add UploadAttachment API

* feat(work): add UploadAttachment API

add UploadAttachment API
2023-09-24 10:43:11 +08:00
markwang
9e810be88a feat:企业微信-微信客服-知识库 (#715)
* feat:企业微信-微信客服=知识库

* fix:golangci-lint

* fix:移除非必要的err判断

---------

Co-authored-by: markwang <www.wang61@qq.com>
2023-09-04 20:15:13 +08:00
markwang
06719f77b7 feat:企业微信-微信客服-统计管理 (#707)
Co-authored-by: markwang <www.wang61@qq.com>
2023-08-31 14:36:22 +08:00
14 changed files with 787 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ jobs:
golangci:
strategy:
matrix:
go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21' ]
go-version: [ '1.16','1.17','1.18','1.19','1.20' ]
name: golangci-lint
runs-on: ubuntu-latest
steps:
@@ -42,7 +42,7 @@ jobs:
# strategy set
strategy:
matrix:
go: [ '1.16','1.17','1.18','1.19','1.20','1.21' ]
go: [ '1.16','1.17','1.18','1.19','1.20' ]
steps:
- uses: actions/checkout@v3

10
cache/redis.go vendored
View File

@@ -8,8 +8,6 @@ import (
)
// Redis .redis cache
// Deprecated: user defined implementation cache、ContextCache interface
// The implementation was officially removed in v2.1.6
type Redis struct {
ctx context.Context
conn redis.UniversalClient
@@ -37,12 +35,12 @@ func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
return &Redis{ctx: ctx, conn: conn}
}
// SetConn 设置 conn
// SetConn 设置conn
func (r *Redis) SetConn(conn redis.UniversalClient) {
r.conn = conn
}
// SetRedisCtx 设置 redis ctx 参数
// SetRedisCtx 设置redis ctx 参数
func (r *Redis) SetRedisCtx(ctx context.Context) {
r.ctx = ctx
}
@@ -71,12 +69,12 @@ func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, tim
return r.conn.SetEX(ctx, key, val, timeout).Err()
}
// IsExist 判断 key 是否存在
// IsExist 判断key是否存在
func (r *Redis) IsExist(key string) bool {
return r.IsExistContext(r.ctx, key)
}
// IsExistContext 判断 key 是否存在
// IsExistContext 判断key是否存在
func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
result, _ := r.conn.Exists(ctx, key).Result()

View File

@@ -66,8 +66,9 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (accessToken string, err error) {
// 先从cache中取
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID)
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
return val.(string), nil
val := ak.cache.Get(accessTokenCacheKey)
if accessToken = val.(string); accessToken != "" {
return
}
// 加上lock是为了防止在并发获取token时cache刚好失效导致从微信服务器上获取到不同token
@@ -75,8 +76,9 @@ func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (access
defer ak.accessTokenLock.Unlock()
// 双检,防止重复从微信服务器获取
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
return val.(string), nil
val = ak.cache.Get(accessTokenCacheKey)
if accessToken = val.(string); accessToken != "" {
return
}
// cache失效从微信服务器获取

View File

@@ -4,6 +4,7 @@ import (
"github.com/silenceper/wechat/v2/pay/config"
"github.com/silenceper/wechat/v2/pay/notify"
"github.com/silenceper/wechat/v2/pay/order"
"github.com/silenceper/wechat/v2/pay/redpacket"
"github.com/silenceper/wechat/v2/pay/refund"
"github.com/silenceper/wechat/v2/pay/transfer"
)
@@ -37,3 +38,8 @@ func (pay *Pay) GetRefund() *refund.Refund {
func (pay *Pay) GetTransfer() *transfer.Transfer {
return transfer.NewTransfer(pay.cfg)
}
// GetRedpacket 红包
func (pay *Pay) GetRedpacket() *redpacket.Redpacket {
return redpacket.NewRedpacket(pay.cfg)
}

131
pay/redpacket/redpacket.go Normal file
View File

@@ -0,0 +1,131 @@
package redpacket
import (
"encoding/xml"
"fmt"
"strconv"
"github.com/silenceper/wechat/v2/pay/config"
"github.com/silenceper/wechat/v2/util"
)
// redpacketGateway 发放红包接口
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
var redpacketGateway = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"
// Redpacket struct extends context
type Redpacket struct {
*config.Config
}
// NewRedpacket return an instance of Redpacket package
func NewRedpacket(cfg *config.Config) *Redpacket {
return &Redpacket{cfg}
}
// Params 调用参数
type Params struct {
MchBillno string // 商户订单号
SendName string // 商户名称
ReOpenID string
TotalAmount int
TotalNum int
Wishing string
ClientIP string
ActName string
Remark string
RootCa string // ca证书
}
// request 接口请求参数
type request struct {
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
MchID string `xml:"mch_id"`
MchBillno string `xml:"mch_billno"`
Wxappid string `xml:"wxappid"`
SendName string `xml:"send_name"`
ReOpenID string `xml:"re_openid"`
TotalAmount int `xml:"total_amount"`
TotalNum int `xml:"total_num"`
Wishing string `xml:"wishing"`
ClientIP string `xml:"client_ip"`
ActName string `xml:"act_name"`
Remark string `xml:"remark"`
}
// Response 接口返回
type Response struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
ResultCode string `xml:"result_code,omitempty"`
ErrCode string `xml:"err_code,omitempty"`
ErrCodeDes string `xml:"err_code_des,omitempty"`
MchBillno string `xml:"mch_billno,omitempty"`
MchID string `xml:"mch_id,omitempty"`
Wxappid string `xml:"wxappid"`
ReOpenID string `xml:"re_openid"`
TotalAmount int `xml:"total_amount"`
SendListid string `xml:"send_listid"`
}
// SendRedpacket 发放红包
func (redpacket *Redpacket) SendRedpacket(p *Params) (rsp *Response, err error) {
nonceStr := util.RandomStr(32)
param := make(map[string]string)
param["nonce_str"] = nonceStr
param["mch_id"] = redpacket.MchID
param["wxappid"] = redpacket.AppID
param["mch_billno"] = p.MchBillno
param["send_name"] = p.SendName
param["re_openid"] = p.ReOpenID
param["total_amount"] = strconv.Itoa(p.TotalAmount)
param["total_num"] = strconv.Itoa(p.TotalNum)
param["wishing"] = p.Wishing
param["client_ip"] = p.ClientIP
param["act_name"] = p.ActName
param["remark"] = p.Remark
//param["scene_id"] = "PRODUCT_2"
sign, err := util.ParamSign(param, redpacket.Key)
if err != nil {
return
}
req := request{
NonceStr: nonceStr,
Sign: sign,
MchID: redpacket.MchID,
Wxappid: redpacket.AppID,
MchBillno: p.MchBillno,
SendName: p.SendName,
ReOpenID: p.ReOpenID,
TotalAmount: p.TotalAmount,
TotalNum: p.TotalNum,
Wishing: p.Wishing,
ClientIP: p.ClientIP,
ActName: p.ActName,
Remark: p.Remark,
}
rawRet, err := util.PostXMLWithTLS(redpacketGateway, req, p.RootCa, redpacket.MchID)
if err != nil {
return
}
err = xml.Unmarshal(rawRet, &rsp)
if err != nil {
return
}
if rsp.ReturnCode == "SUCCESS" {
if rsp.ResultCode == "SUCCESS" {
err = nil
return
}
err = fmt.Errorf("send redpacket error, errcode=%s,errmsg=%s", rsp.ErrCode, rsp.ErrCodeDes)
return
}
err = fmt.Errorf("[msg : xmlUnmarshalError] [rawReturn : %s] [sign : %s]", string(rawRet), sign)
return
}

View File

@@ -13,6 +13,8 @@ const (
departmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
// departmentListURL 获取部门列表
departmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s"
// departmentGetURL 获取单个部门详情 https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID
departmentGetURL = "https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=%s&id=%d"
)
type (
@@ -121,3 +123,24 @@ func (r *Client) DepartmentList() ([]*Department, error) {
// 返回数据
return result.Department, err
}
// DepartmentGet 获取单个部门详情
// see https://developer.work.weixin.qq.com/document/path/95351
func (r *Client) DepartmentGet(departmentID int) (*Department, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.HTTPGet(fmt.Sprintf(departmentGetURL, accessToken, departmentID)); err != nil {
return nil, err
}
result := &Department{}
if err = util.DecodeWithError(response, result, "DepartmentGet"); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -221,7 +221,7 @@ type UserGetResponse struct {
} `json:"external_profile"` // 成员对外属性,字段详情见对外属性;代开发自建应用需要管理员授权才返回;第三方仅通讯录应用可获取;对于非第三方创建的成员,第三方通讯录应用也不可获取;上游企业不可获取下游企业成员该字段
}
// UserGet 获取部门成员
// UserGet 读取成员
// @see https://developer.work.weixin.qq.com/document/path/90196
func (r *Client) UserGet(UserID string) (*UserGetResponse, error) {
var (
@@ -237,8 +237,8 @@ func (r *Client) UserGet(UserID string) (*UserGetResponse, error) {
strings.Join([]string{
userGetURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
"department_id": UserID,
"access_token": accessToken,
"userid": UserID,
}),
}, "?")); err != nil {
return nil, err

69
work/checkin/checkin.go Normal file
View File

@@ -0,0 +1,69 @@
package checkin
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// getCheckinDataURL 获取打卡记录数据
getCheckinDataURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=%s"
)
type (
// GetCheckinDataRequest 获取打卡记录数据请求
GetCheckinDataRequest struct {
OpenCheckinDataType int64 `json:"opencheckindatatype"`
StartTime int64 `json:"starttime"`
EndTime int64 `json:"endtime"`
UserIDList []string `json:"useridlist"`
}
// GetCheckinDataResponse 获取打卡记录数据响应
GetCheckinDataResponse struct {
util.CommonError
CheckinData []*GetCheckinDataItem `json:"checkindata"`
}
// GetCheckinDataItem 打卡记录数据
GetCheckinDataItem struct {
UserID string `json:"userid"`
GroupName string `json:"groupname"`
CheckinType string `json:"checkin_type"`
ExceptionType string `json:"exception_type"`
CheckinTime int64 `json:"checkin_time"`
LocationTitle string `json:"location_title"`
LocationDetail string `json:"location_detail"`
WifiName string `json:"wifiname"`
Notes string `json:"notes"`
WifiMac string `json:"wifimac"`
MediaIDs []string `json:"mediaids"`
SchCheckinTime int64 `json:"sch_checkin_time"`
GroupID int64 `json:"groupid"`
ScheduleID int64 `json:"schedule_id"`
TimelineID int64 `json:"timeline_id"`
Lat int64 `json:"lat,omitempty"`
Lng int64 `json:"lng,omitempty"`
DeviceID string `json:"deviceid,omitempty"`
}
)
// GetCheckinData 获取打卡记录数据
// @see https://developer.work.weixin.qq.com/document/path/90262
func (r *Client) GetCheckinData(req *GetCheckinDataRequest) (*GetCheckinDataResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getCheckinDataURL, accessToken), req); err != nil {
return nil, err
}
result := &GetCheckinDataResponse{}
if err = util.DecodeWithError(response, result, "GetCheckinData"); err != nil {
return nil, err
}
return result, nil
}

17
work/checkin/client.go Normal file
View File

@@ -0,0 +1,17 @@
package checkin
import (
"github.com/silenceper/wechat/v2/work/context"
)
// Client 打卡接口实例
type Client struct {
*context.Context
}
// NewClient 初始化实例
func NewClient(ctx *context.Context) *Client {
return &Client{
ctx,
}
}

View File

@@ -70,6 +70,7 @@ type (
GroupNickname string `json:"group_nickname"` //在群里的昵称
Name string `json:"name"` //名字。仅当 need_name = 1 时返回 如果是微信用户,则返回其在微信中设置的名字 如果是企业微信联系人,则返回其设置对外展示的别名或实名
UnionID string `json:"unionid,omitempty"` //外部联系人在微信开放平台的唯一身份标识微信unionid通过此字段企业可将外部联系人与公众号/小程序用户关联起来。仅当群成员类型是微信用户包括企业成员未添加好友且企业绑定了微信开发者ID有此字段查看绑定方法。第三方不可获取上游企业不可获取下游企业客户的unionid字段
State string `json:"state,omitempty"` //如果在配置入群方式时配置了state参数那么在获取客户群详情时通过该方式入群的成员会额外获取到相应的state参数
}
//GroupChatAdmin 群管理员
GroupChatAdmin struct {

359
work/kf/knowledge.go Normal file
View File

@@ -0,0 +1,359 @@
package kf
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// addKnowledgeGroupURL 知识库分组添加
addKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/add_group?access_token=%s"
// delKnowledgeGroupURL 知识库分组删除
delKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/del_group?access_token=%s"
// modKnowledgeGroupURL 知识库分组修改
modKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/mod_group?access_token=%s"
// listKnowledgeGroupURL 知识库分组列表
listKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/list_group?access_token=%s"
// addKnowledgeIntentURL 知识库问答添加
addKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/add_intent?access_token=%s"
// delKnowledgeIntentURL 知识库问答删除
delKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/del_intent?access_token=%s"
// modKnowledgeIntentURL 知识库问答修改
modKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/mod_intent?access_token=%s"
// listKnowledgeIntentURL 知识库问答列表
listKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/list_intent?access_token=%s"
)
// AddKnowledgeGroupRequest 知识库分组添加请求
type AddKnowledgeGroupRequest struct {
Name string `json:"name"`
}
// AddKnowledgeGroupResponse 知识库分组添加响应
type AddKnowledgeGroupResponse struct {
util.CommonError
GroupID string `json:"group_id"`
}
// AddKnowledgeGroup 知识库分组添加
// see https://developer.work.weixin.qq.com/document/path/95971#%E6%B7%BB%E5%8A%A0%E5%88%86%E7%BB%84
func (r *Client) AddKnowledgeGroup(req *AddKnowledgeGroupRequest) (*AddKnowledgeGroupResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(addKnowledgeGroupURL, accessToken), req); err != nil {
return nil, err
}
result := &AddKnowledgeGroupResponse{}
err = util.DecodeWithError(response, result, "AddKnowledgeGroup")
return result, err
}
// DelKnowledgeGroupRequest 知识库分组删除请求
type DelKnowledgeGroupRequest struct {
GroupID string `json:"group_id"`
}
// DelKnowledgeGroup 知识库分组删除
// see https://developer.work.weixin.qq.com/document/path/95971#%E5%88%A0%E9%99%A4%E5%88%86%E7%BB%84
func (r *Client) DelKnowledgeGroup(req *DelKnowledgeGroupRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(delKnowledgeGroupURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "DelKnowledgeGroup")
}
// ModKnowledgeGroupRequest 知识库分组修改请求
type ModKnowledgeGroupRequest struct {
GroupID string `json:"group_id"`
Name string `json:"name"`
}
// ModKnowledgeGroup 知识库分组修改
// see https://developer.work.weixin.qq.com/document/path/95971#%E4%BF%AE%E6%94%B9%E5%88%86%E7%BB%84
func (r *Client) ModKnowledgeGroup(req *ModKnowledgeGroupRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(modKnowledgeGroupURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "ModKnowledgeGroup")
}
// ListKnowledgeGroupRequest 知识库分组列表请求
type ListKnowledgeGroupRequest struct {
Cursor string `json:"cursor"`
Limit int `json:"limit"`
GroupID string `json:"group_id"`
}
// ListKnowledgeGroupResponse 知识库分组列表响应
type ListKnowledgeGroupResponse struct {
util.CommonError
NextCursor string `json:"next_cursor"`
HasMore int `json:"has_more"`
GroupList []KnowledgeGroup `json:"group_list"`
}
// KnowledgeGroup 知识库分组
type KnowledgeGroup struct {
GroupID string `json:"group_id"`
Name string `json:"name"`
IsDefault int `json:"is_default"`
}
// ListKnowledgeGroup 知识库分组列表
// see https://developer.work.weixin.qq.com/document/path/95971#%E8%8E%B7%E5%8F%96%E5%88%86%E7%BB%84%E5%88%97%E8%A1%A8
func (r *Client) ListKnowledgeGroup(req *ListKnowledgeGroupRequest) (*ListKnowledgeGroupResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(listKnowledgeGroupURL, accessToken), req); err != nil {
return nil, err
}
result := &ListKnowledgeGroupResponse{}
err = util.DecodeWithError(response, result, "ListKnowledgeGroup")
return result, err
}
// AddKnowledgeIntentRequest 知识库问答添加请求
type AddKnowledgeIntentRequest struct {
GroupID string `json:"group_id"`
Question IntentQuestion `json:"question"`
SimilarQuestions IntentSimilarQuestions `json:"similar_questions"`
Answers []IntentAnswerReq `json:"answers"`
}
// IntentQuestion 主问题
type IntentQuestion struct {
Text IntentQuestionText `json:"text"`
}
// IntentQuestionText 问题文本
type IntentQuestionText struct {
Content string `json:"content"`
}
// IntentSimilarQuestions 相似问题
type IntentSimilarQuestions struct {
Items []IntentQuestion `json:"items"`
}
// IntentAnswerReq 回答请求
type IntentAnswerReq struct {
Text IntentAnswerText `json:"text"`
Attachments []IntentAnswerAttachmentReq `json:"attachments"`
}
// IntentAnswerText 回答文本
type IntentAnswerText struct {
Content string `json:"content"`
}
// IntentAnswerAttachmentReq 回答附件请求
type IntentAnswerAttachmentReq struct {
MsgType string `json:"msgtype"`
Image IntentAnswerAttachmentImgReq `json:"image,omitempty"`
Video IntentAnswerAttachmentVideoReq `json:"video,omitempty"`
Link IntentAnswerAttachmentLink `json:"link,omitempty"`
MiniProgram IntentAnswerAttachmentMiniProgramReq `json:"miniprogram,omitempty"`
}
// IntentAnswerAttachmentImgReq 图片类型回答附件请求
type IntentAnswerAttachmentImgReq struct {
MediaID string `json:"media_id"`
}
// IntentAnswerAttachmentVideoReq 视频类型回答附件请求
type IntentAnswerAttachmentVideoReq struct {
MediaID string `json:"media_id"`
}
// IntentAnswerAttachmentLink 链接类型回答附件
type IntentAnswerAttachmentLink struct {
Title string `json:"title"`
PicURL string `json:"picurl"`
Desc string `json:"desc"`
URL string `json:"url"`
}
// IntentAnswerAttachmentMiniProgramReq 小程序类型回答附件请求
type IntentAnswerAttachmentMiniProgramReq struct {
Title string `json:"title"`
ThumbMediaID string `json:"thumb_media_id"`
AppID string `json:"appid"`
PagePath string `json:"pagepath"`
}
// AddKnowledgeIntentResponse 知识库问答添加响应
type AddKnowledgeIntentResponse struct {
util.CommonError
IntentID string `json:"intent_id"`
}
// AddKnowledgeIntent 知识库问答添加
// see https://developer.work.weixin.qq.com/document/path/95972#%E6%B7%BB%E5%8A%A0%E9%97%AE%E7%AD%94
func (r *Client) AddKnowledgeIntent(req *AddKnowledgeIntentRequest) (*AddKnowledgeIntentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(addKnowledgeIntentURL, accessToken), req); err != nil {
return nil, err
}
result := &AddKnowledgeIntentResponse{}
err = util.DecodeWithError(response, result, "AddKnowledgeIntent")
return result, err
}
// DelKnowledgeIntentRequest 知识库问答删除请求
type DelKnowledgeIntentRequest struct {
IntentID string `json:"intent_id"`
}
// DelKnowledgeIntent 知识库问答删除
// see https://developer.work.weixin.qq.com/document/path/95972#%E5%88%A0%E9%99%A4%E9%97%AE%E7%AD%94
func (r *Client) DelKnowledgeIntent(req *DelKnowledgeIntentRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(delKnowledgeIntentURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "DelKnowledgeIntent")
}
// ModKnowledgeIntentRequest 知识库问答修改请求
type ModKnowledgeIntentRequest struct {
IntentID string `json:"intent_id"`
Question IntentQuestion `json:"question"`
SimilarQuestions IntentSimilarQuestions `json:"similar_questions"`
Answers []IntentAnswerReq `json:"answers"`
}
// ModKnowledgeIntent 知识库问答修改
// see https://developer.work.weixin.qq.com/document/path/95972#%E4%BF%AE%E6%94%B9%E9%97%AE%E7%AD%94
func (r *Client) ModKnowledgeIntent(req *ModKnowledgeIntentRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(modKnowledgeIntentURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "ModKnowledgeIntent")
}
// ListKnowledgeIntentRequest 知识库问答列表请求
type ListKnowledgeIntentRequest struct {
Cursor string `json:"cursor"`
Limit int `json:"limit"`
GroupID string `json:"group_id"`
IntentID string `json:"intent_id"`
}
// ListKnowledgeIntentResponse 知识库问答列表响应
type ListKnowledgeIntentResponse struct {
util.CommonError
NextCursor string `json:"next_cursor"`
HasMore int `json:"has_more"`
IntentList []KnowledgeIntent `json:"intent_list"`
}
// KnowledgeIntent 问答摘要
type KnowledgeIntent struct {
GroupID string `json:"group_id"`
IntentID string `json:"intent_id"`
Question IntentQuestion `json:"question"`
SimilarQuestions IntentSimilarQuestions `json:"similar_questions"`
Answers []IntentAnswerRes `json:"answers"`
}
// IntentAnswerRes 回答返回
type IntentAnswerRes struct {
Text IntentAnswerText `json:"text"`
Attachments []IntentAnswerAttachmentRes `json:"attachments"`
}
// IntentAnswerAttachmentRes 回答附件返回
type IntentAnswerAttachmentRes struct {
MsgType string `json:"msgtype"`
Image IntentAnswerAttachmentImgRes `json:"image,omitempty"`
Video IntentAnswerAttachmentVideoRes `json:"video,omitempty"`
Link IntentAnswerAttachmentLink `json:"link,omitempty"`
MiniProgram IntentAnswerAttachmentMiniProgramRes `json:"miniprogram,omitempty"`
}
// IntentAnswerAttachmentImgRes 图片类型回答附件返回
type IntentAnswerAttachmentImgRes struct {
Name string `json:"name"`
}
// IntentAnswerAttachmentVideoRes 视频类型回答附件返回
type IntentAnswerAttachmentVideoRes struct {
Name string `json:"name"`
}
// IntentAnswerAttachmentMiniProgramRes 小程序类型回答附件返回
type IntentAnswerAttachmentMiniProgramRes struct {
Title string `json:"title"`
AppID string `json:"appid"`
PagePath string `json:"pagepath"`
}
// ListKnowledgeIntent 知识库问答列表
// see https://developer.work.weixin.qq.com/document/path/95972#%E8%8E%B7%E5%8F%96%E9%97%AE%E7%AD%94%E5%88%97%E8%A1%A8
func (r *Client) ListKnowledgeIntent(req *ListKnowledgeIntentRequest) (*ListKnowledgeIntentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(listKnowledgeIntentURL, accessToken), req); err != nil {
return nil, err
}
result := &ListKnowledgeIntentResponse{}
err = util.DecodeWithError(response, result, "ListKnowledgeIntent")
return result, err
}

127
work/kf/statistic.go Normal file
View File

@@ -0,0 +1,127 @@
package kf
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// getCorpStatisticURL 获取「客户数据统计」企业汇总数据
getCorpStatisticURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/get_corp_statistic?access_token=%s"
// getServicerStatisticURL 获取「客户数据统计」接待人员明细数据
getServicerStatisticURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/get_servicer_statistic?access_token=%s"
)
// GetCorpStatisticRequest 获取「客户数据统计」企业汇总数据请求
type GetCorpStatisticRequest struct {
OpenKfID string `json:"open_kfid"`
StartTime int64 `json:"start_time"`
EndTime int64 `json:"end_time"`
}
// GetCorpStatisticResponse 获取「客户数据统计」企业汇总数据响应
type GetCorpStatisticResponse struct {
util.CommonError
StatisticList []CorpStatisticList `json:"statistic_list"`
}
// CorpStatisticList 企业汇总统计数据列表
type CorpStatisticList struct {
StatTime int64 `json:"stat_time"`
Statistic CorpStatistic `json:"statistic"`
}
// CorpStatistic 企业汇总统计一天的统计数据
type CorpStatistic struct {
SessionCnt int64 `json:"session_cnt"`
CustomerCnt int64 `json:"customer_cnt"`
CustomerMsgCnt int64 `json:"customer_msg_cnt"`
UpgradeServiceCustomerCnt int64 `json:"upgrade_service_customer_cnt"`
AiSessionReplyCnt int64 `json:"ai_session_reply_cnt"`
AiTransferRate float64 `json:"ai_transfer_rate"`
AiKnowledgeHitRate float64 `json:"ai_knowledge_hit_rate"`
MsgRejectedCustomerCnt int64 `json:"msg_rejected_customer_cnt"`
}
// GetCorpStatistic 获取「客户数据统计」企业汇总数据
// see https://developer.work.weixin.qq.com/document/path/95489
func (r *Client) GetCorpStatistic(req *GetCorpStatisticRequest) (*GetCorpStatisticResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getCorpStatisticURL, accessToken), req); err != nil {
return nil, err
}
result := &GetCorpStatisticResponse{}
if err = util.DecodeWithError(response, result, "GetCorpStatistic"); err != nil {
return nil, err
}
return result, nil
}
// GetServicerStatisticRequest 获取「客户数据统计」接待人员明细数据请求
type GetServicerStatisticRequest struct {
OpenKfID string `json:"open_kfid"`
ServicerUserID string `json:"servicer_userid"`
StartTime int64 `json:"start_time"`
EndTime int64 `json:"end_time"`
}
// GetServicerStatisticResponse 获取「客户数据统计」接待人员明细数据响应
type GetServicerStatisticResponse struct {
util.CommonError
StatisticList []ServicerStatisticList `json:"statistic_list"`
}
// ServicerStatisticList 接待人员明细统计数据列表
type ServicerStatisticList struct {
StatTime int64 `json:"stat_time"`
Statistic ServicerStatistic `json:"statistic"`
}
// ServicerStatistic 接待人员明细统计一天的统计数据
type ServicerStatistic struct {
SessionCnt int64 `json:"session_cnt"`
CustomerCnt int64 `json:"customer_cnt"`
CustomerMsgCnt int64 `json:"customer_msg_cnt"`
ReplyRate float64 `json:"reply_rate"`
FirstReplyAverageSec float64 `json:"first_reply_average_sec"`
SatisfactionInvestgateCnt int64 `json:"satisfaction_investgate_cnt"`
SatisfactionParticipationRate float64 `json:"satisfaction_participation_rate"`
SatisfiedRate float64 `json:"satisfied_rate"`
MiddlingRate float64 `json:"middling_rate"`
DissatisfiedRate float64 `json:"dissatisfied_rate"`
UpgradeServiceCustomerCnt int64 `json:"upgrade_service_customer_cnt"`
UpgradeServiceMemberInviteCnt int64 `json:"upgrade_service_member_invite_cnt"`
UpgradeServiceMemberCustomerCnt int64 `json:"upgrade_service_member_customer_cnt"`
UpgradeServiceGroupChatInviteCnt int64 `json:"upgrade_service_groupchat_invite_cnt"`
UpgradeServiceGroupChatCustomerCnt int64 `json:"upgrade_service_groupchat_customer_cnt"`
MsgRejectedCustomerCnt int64 `json:"msg_rejected_customer_cnt"`
}
// GetServicerStatistic 获取「客户数据统计」接待人员明细数据
// see https://developer.work.weixin.qq.com/document/path/95490
func (r *Client) GetServicerStatistic(req *GetServicerStatisticRequest) (*GetServicerStatisticResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getServicerStatisticURL, accessToken), req); err != nil {
return nil, err
}
result := &GetServicerStatisticResponse{}
if err = util.DecodeWithError(response, result, "GetServicerStatistic"); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -11,6 +11,8 @@ const (
uploadImgURL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"
// uploadTempFile 上传临时素材
uploadTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
// uploadAttachment 上传附件资源
uploadAttachment = "https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=%s&media_type=%s&attachment_type=%d"
)
// UploadImgResponse 上传图片响应
@@ -27,6 +29,14 @@ type UploadTempFileResponse struct {
Type string `json:"type"`
}
// UploadAttachmentResponse 上传资源附件响应
type UploadAttachmentResponse struct {
util.CommonError
MediaID string `json:"media_id"`
CreateAt int64 `json:"created_at"`
Type string `json:"type"`
}
// UploadImg 上传图片
// @see https://developer.work.weixin.qq.com/document/path/90256
func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
@@ -69,3 +79,26 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF
}
return result, nil
}
// UploadAttachment 上传附件资源
// @see https://developer.work.weixin.qq.com/document/path/95098
// @mediaType 媒体文件类型分别有图片image、视频video、普通文件file
// @attachment_type 附件类型不同的附件类型用于不同的场景。1朋友圈2:商品图册
func (r *Client) UploadAttachment(filename string, mediaType string, attachmentType int) (*UploadAttachmentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil {
return nil, err
}
result := &UploadAttachmentResponse{}
if err = util.DecodeWithError(response, result, "UploadAttachment"); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/silenceper/wechat/v2/credential"
"github.com/silenceper/wechat/v2/work/addresslist"
"github.com/silenceper/wechat/v2/work/appchat"
"github.com/silenceper/wechat/v2/work/checkin"
"github.com/silenceper/wechat/v2/work/config"
"github.com/silenceper/wechat/v2/work/context"
"github.com/silenceper/wechat/v2/work/externalcontact"
@@ -85,3 +86,8 @@ func (wk *Work) GetAppChat() *appchat.Client {
func (wk *Work) GetInvoice() *invoice.Client {
return invoice.NewClient(wk.ctx)
}
// GetCheckin 获取打卡接口实例
func (wk *Work) GetCheckin() *checkin.Client {
return checkin.NewClient(wk.ctx)
}