-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpaceApi.go
166 lines (137 loc) · 4.67 KB
/
SpaceApi.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
package feishuapi
import "github.com/sirupsen/logrus"
type SpaceInfo struct {
Name string
Description string
SpaceId string
}
// Create a new SpaceInfo
func NewSpaceInfo(data map[string]any) *SpaceInfo {
return &SpaceInfo{
Name: data["name"].(string),
Description: data["description"].(string),
SpaceId: data["space_id"].(string),
}
}
// Create a Knowledge Space
func (c AppClient) KnowledgeSpaceCreate(name string, description string, user_access_token string) *SpaceInfo {
body := make(map[string]string)
body["name"] = name
body["description"] = description
headers := make(map[string]string)
headers["Authorization"] = user_access_token
info := c.Request("post", "open-apis/wiki/v2/spaces", nil, headers, body)
if info == nil {
logrus.WithFields(logrus.Fields{
"Name": name,
"Description": description,
}).Error("create knowledge space fail")
return nil
}
return NewSpaceInfo(info["space"].(map[string]any))
}
// Add members to a Knowledge Space
// memberType: "openchat" for chat id, "userid" for feishuapi.UserId, "unionid" for feishuapi.UnionId, "opendepartmentid" for DepartmentId
func (c AppClient) KnowledgeSpaceAddMembers(spaceId string, membersId []string, memberType string) {
body := make(map[string]string)
body["member_type"] = memberType
body["member_role"] = "member"
for _, v := range membersId {
body["member_id"] = v
resp := c.Request("post", "open-apis/wiki/v2/spaces/"+spaceId+"/members", nil, nil, body)
if resp == nil {
logrus.WithFields(logrus.Fields{
"SpaceID": spaceId,
"MemberType": memberType,
"MemberID": v,
}).Warn("add member fail")
}
}
}
// Add robots to a Knowledge Space as admin
func (c AppClient) KnowledgeSpaceAddBotsAsAdmin(spaceId string, BotsId []string, user_access_token string) {
headers := make(map[string]string)
headers["Authorization"] = user_access_token
body := make(map[string]string)
body["member_type"] = "openid"
body["member_role"] = "admin"
for _, v := range BotsId {
body["member_id"] = v
resp := c.Request("post", "open-apis/wiki/v2/spaces/"+spaceId+"/members", nil, headers, body)
if resp == nil {
logrus.WithFields(logrus.Fields{
"SpaceID": spaceId,
"MemberID": v,
}).Warn("add bot fail")
}
}
}
type NodeInfo struct {
NodeToken string
ObjToken string
ObjType string
ParentNodeToken string
Title string
HasChild bool
}
// Create a new NodeInfo
func NewNodeInfo(data map[string]any) *NodeInfo {
return &NodeInfo{
NodeToken: data["node_token"].(string),
ObjToken: data["obj_token"].(string),
ObjType: data["obj_type"].(string),
ParentNodeToken: data["parent_node_token"].(string),
Title: data["title"].(string),
HasChild: data["has_child"].(bool),
}
}
func (c AppClient) KnowledgeSpaceGetNodeInfo(NodeToken string) *NodeInfo {
query := make(map[string]any)
query["token"] = NodeToken
info := c.Request("get", "open-apis/wiki/v2/spaces/get_node", query, nil, nil)
if info == nil {
logrus.WithField("NodeToken", NodeToken).Error("copy node fail")
return nil
}
return NewNodeInfo(info["node"].(map[string]any))
}
// Copy a node from SpaceId/NodeToken to TargetSpaceId/TargetParentToken
func (c AppClient) KnowledgeSpaceCopyNode(SpaceId string, NodeToken string, TargetSpaceId string, TargetParentToken string, Title ...string) *NodeInfo {
body := make(map[string]string)
body["target_parent_token"] = TargetParentToken
body["target_space_id"] = TargetSpaceId
if len(Title) != 0 {
body["title"] = Title[0]
}
info := c.Request("post", "open-apis/wiki/v2/spaces/"+SpaceId+"/nodes/"+NodeToken+"/copy", nil, nil, body)
if info == nil {
logrus.WithFields(logrus.Fields{
"SpaceID": SpaceId,
"NodeToken": NodeToken,
"TargetSpaceID": TargetSpaceId,
"TargetParentToken": TargetParentToken,
}).Error("copy node fail")
return nil
}
return NewNodeInfo(info["node"].(map[string]any))
}
// Get All Nodes in target Space and under specific ParentNode(not necessary)
func (c AppClient) KnowledgeSpaceGetAllNodes(SpaceId string, ParentNodeToken ...string) []NodeInfo {
var all_node []NodeInfo
var l []any
if len(ParentNodeToken) != 0 {
query := make(map[string]any)
query["parent_node_token"] = ParentNodeToken[0]
l = c.GetAllPages("get", "open-apis/wiki/v2/spaces/"+SpaceId+"/nodes", query, nil, nil, 10)
} else {
l = c.GetAllPages("get", "open-apis/wiki/v2/spaces/"+SpaceId+"/nodes", nil, nil, nil, 10)
}
if l == nil {
logrus.WithField("SpaceID", SpaceId).Error("nil node info return")
return nil
}
for _, value := range l {
all_node = append(all_node, *NewNodeInfo(value.(map[string]any)))
}
return all_node
}