forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
203 lines (185 loc) · 5.77 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package externalcontact
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// GetCropTagURL 获取标签列表
GetCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list"
// AddCropTagURL 添加标签
AddCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag"
// EditCropTagURL 修改标签
EditCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag"
// DelCropTagURL 删除标签
DelCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag"
// MarkCropTagURL 为客户打上、删除标签
MarkCropTagURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag"
)
// GetCropTagRequest 获取企业标签请求
type GetCropTagRequest struct {
TagID []string `json:"tag_id"`
GroupID []string `json:"group_id"`
}
// GetCropTagListResponse 获取企业标签列表响应
type GetCropTagListResponse struct {
util.CommonError
TagGroup []TagGroup `json:"tag_group"`
}
// TagGroup 企业标签组
type TagGroup struct {
GroupID string `json:"group_id"`
GroupName string `json:"group_name"`
CreateTime int `json:"create_time"`
GroupOrder int `json:"group_order"`
Deleted bool `json:"deleted"`
Tag []TagGroupTagItem `json:"tag"`
}
// TagGroupTagItem 企业标签内的子项
type TagGroupTagItem struct {
ID string `json:"id"`
Name string `json:"name"`
CreateTime int `json:"create_time"`
Order int `json:"order"`
Deleted bool `json:"deleted"`
}
// GetCropTagList 获取企业标签库
// @see https://developer.work.weixin.qq.com/document/path/92117
func (r *Client) GetCropTagList(req GetCropTagRequest) ([]TagGroup, error) {
accessToken, err := r.GetAccessToken()
if err != nil {
return nil, err
}
var response []byte
jsonData, err := json.Marshal(req)
if err != nil {
return nil, err
}
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", GetCropTagURL, accessToken), string(jsonData))
if err != nil {
return nil, err
}
var result GetCropTagListResponse
err = util.DecodeWithError(response, &result, "GetCropTagList")
if err != nil {
return nil, err
}
return result.TagGroup, nil
}
// AddCropTagRequest 添加企业标签请求
type AddCropTagRequest struct {
GroupID string `json:"group_id,omitempty"`
GroupName string `json:"group_name"`
Order int `json:"order"`
Tag []AddCropTagItem `json:"tag"`
AgentID int `json:"agentid"`
}
// AddCropTagItem 添加企业标签子项
type AddCropTagItem struct {
Name string `json:"name"`
Order int `json:"order"`
}
// AddCropTagResponse 添加企业标签响应
type AddCropTagResponse struct {
util.CommonError
TagGroup TagGroup `json:"tag_group"`
}
// AddCropTag 添加企业客户标签
// @see https://developer.work.weixin.qq.com/document/path/92117
func (r *Client) AddCropTag(req AddCropTagRequest) (*TagGroup, error) {
var accessToken string
accessToken, err := r.GetAccessToken()
if err != nil {
return nil, err
}
var response []byte
jsonData, err := json.Marshal(req)
if err != nil {
return nil, err
}
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", AddCropTagURL, accessToken), string(jsonData))
if err != nil {
return nil, err
}
var result AddCropTagResponse
err = util.DecodeWithError(response, &result, "AddCropTag")
if err != nil {
return nil, err
}
return &result.TagGroup, nil
}
// EditCropTagRequest 编辑客户企业标签请求
type EditCropTagRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Order int `json:"order"`
AgentID string `json:"agent_id"`
}
// EditCropTag 修改企业客户标签
// @see https://developer.work.weixin.qq.com/document/path/92117
func (r *Client) EditCropTag(req EditCropTagRequest) error {
accessToken, err := r.GetAccessToken()
if err != nil {
return err
}
var response []byte
jsonData, err := json.Marshal(req)
if err != nil {
return err
}
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", EditCropTagURL, accessToken), string(jsonData))
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "EditCropTag")
}
// DeleteCropTagRequest 删除企业标签请求
type DeleteCropTagRequest struct {
TagID []string `json:"tag_id"`
GroupID []string `json:"group_id"`
AgentID string `json:"agent_id"`
}
// DeleteCropTag 删除企业客户标签
// @see https://developer.work.weixin.qq.com/document/path/92117
func (r *Client) DeleteCropTag(req DeleteCropTagRequest) error {
accessToken, err := r.GetAccessToken()
if err != nil {
return err
}
var response []byte
jsonData, err := json.Marshal(req)
if err != nil {
return err
}
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", DelCropTagURL, accessToken), string(jsonData))
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "DeleteCropTag")
}
// MarkTagRequest 给客户打标签请求
// 相关文档地址:https://developer.work.weixin.qq.com/document/path/92118
type MarkTagRequest struct {
UserID string `json:"userid"`
ExternalUserID string `json:"external_userid"`
AddTag []string `json:"add_tag"`
RemoveTag []string `json:"remove_tag"`
}
// MarkTag 为客户打上标签
// @see https://developer.work.weixin.qq.com/document/path/92118
func (r *Client) MarkTag(request MarkTagRequest) error {
accessToken, err := r.GetAccessToken()
if err != nil {
return err
}
var response []byte
jsonData, err := json.Marshal(request)
if err != nil {
return err
}
response, err = util.HTTPPost(fmt.Sprintf("%s?access_token=%v", MarkCropTagURL, accessToken), string(jsonData))
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "MarkTag")
}