-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexecute.ts
More file actions
323 lines (281 loc) · 12.1 KB
/
Copy pathexecute.ts
File metadata and controls
323 lines (281 loc) · 12.1 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import type { IExecuteFunctions, INodeExecutionData, IDataObject } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { weComApiRequest } from '../../shared/transport';
export async function executeKf(
this: IExecuteFunctions,
operation: string,
items: INodeExecutionData[],
): Promise<INodeExecutionData[]> {
const returnData: INodeExecutionData[] = [];
for (let i = 0; i < items.length; i++) {
try {
let response: IDataObject;
// 客服账号管理
if (operation === 'addKfAccount') {
const name = this.getNodeParameter('name', i) as string;
const media_id = this.getNodeParameter('media_id', i) as string;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/account/add', {
name,
media_id,
});
} else if (operation === 'delKfAccount') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/account/del', {
open_kfid,
});
} else if (operation === 'updateKfAccount') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const name = this.getNodeParameter('name', i, '') as string;
const media_id = this.getNodeParameter('media_id', i, '') as string;
const body: IDataObject = { open_kfid };
if (name) body.name = name;
if (media_id) body.media_id = media_id;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/account/update', body);
} else if (operation === 'listKfAccount') {
const limit = this.getNodeParameter('limit', i, 50) as number;
const cursor = this.getNodeParameter('cursor', i, '') as string;
const body: IDataObject = { limit };
if (cursor) body.cursor = cursor;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/account/list', body);
} else if (operation === 'getKfAccountLink') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const scene = this.getNodeParameter('scene', i, '') as string;
const body: IDataObject = { open_kfid };
if (scene) body.scene = scene;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/add_contact_way', body);
}
// 接待人员管理
else if (operation === 'addServicer') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const userid_list = this.getNodeParameter('userid_list', i) as string;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/servicer/add', {
open_kfid,
userid_list: userid_list.split(',').map((id) => id.trim()),
});
} else if (operation === 'delServicer') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const userid_list = this.getNodeParameter('userid_list', i) as string;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/servicer/del', {
open_kfid,
userid_list: userid_list.split(',').map((id) => id.trim()),
});
} else if (operation === 'listServicer') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/servicer/list', {
open_kfid,
});
}
// 会话分配与消息收发
else if (operation === 'transServiceState') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const external_userid = this.getNodeParameter('external_userid', i) as string;
const service_state = this.getNodeParameter('service_state', i) as number;
const servicer_userid = this.getNodeParameter('servicer_userid', i, '') as string;
const body: IDataObject = {
open_kfid,
external_userid,
service_state,
};
if (servicer_userid) body.servicer_userid = servicer_userid;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/service_state/trans', body);
} else if (operation === 'sendKfMsg') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const touser = this.getNodeParameter('touser', i) as string;
const msgtype = this.getNodeParameter('msgtype', i) as string;
const content = this.getNodeParameter('content', i) as string;
let parsedContent;
try {
parsedContent = JSON.parse(content);
} catch (error) {
throw new NodeOperationError(this.getNode(), `content 必须是有效的 JSON: ${error.message}`, { itemIndex: i });
}
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/send_msg', {
touser,
open_kfid,
msgtype,
[msgtype]: parsedContent,
});
} else if (operation === 'sendKfEventMsg') {
const code = this.getNodeParameter('code', i) as string;
const msgtype = this.getNodeParameter('msgtype', i) as string;
const content = this.getNodeParameter('content', i) as string;
let parsedContent;
try {
parsedContent = JSON.parse(content);
} catch (error) {
throw new NodeOperationError(this.getNode(), `content 必须是有效的 JSON: ${error.message}`, { itemIndex: i });
}
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/send_msg_on_event', {
code,
msgtype,
[msgtype]: parsedContent,
});
} else if (operation === 'setUpgradeService') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const upgrade_config = this.getNodeParameter('upgrade_config', i) as string;
let parsedConfig;
try {
parsedConfig = JSON.parse(upgrade_config);
} catch (error) {
throw new NodeOperationError(this.getNode(), `upgrade_config 必须是有效的 JSON: ${error.message}`, { itemIndex: i });
}
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/customer/upgrade_service_config', {
open_kfid,
upgrade_config: parsedConfig,
});
} else if (operation === 'syncMsg') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const cursor = this.getNodeParameter('cursor', i, '') as string;
const token = this.getNodeParameter('token', i, '') as string;
const limit = this.getNodeParameter('limit', i, 100) as number;
const voice_format = this.getNodeParameter('voice_format', i, 0) as number;
const parse_message_types = this.getNodeParameter('parse_message_types', i, true) as boolean;
const body: IDataObject = {
open_kfid,
limit,
voice_format,
};
if (cursor) body.cursor = cursor;
if (token) body.token = token;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/sync_msg', body);
// 如果需要解析消息类型,对msg_list进行处理
if (parse_message_types && response.msg_list && Array.isArray(response.msg_list)) {
response.msg_list = (response.msg_list as IDataObject[]).map((msg: IDataObject) => {
const msgtype = msg.msgtype as string;
// 为每个消息添加解析后的字段,便于后续处理
const parsedMsg: IDataObject = {
...msg,
parsed_content: null,
};
// 根据消息类型解析内容
if (msgtype === 'text' && msg.text) {
parsedMsg.parsed_content = msg.text;
} else if (msgtype === 'image' && msg.image) {
parsedMsg.parsed_content = msg.image;
} else if (msgtype === 'voice' && msg.voice) {
parsedMsg.parsed_content = msg.voice;
} else if (msgtype === 'video' && msg.video) {
parsedMsg.parsed_content = msg.video;
} else if (msgtype === 'file' && msg.file) {
parsedMsg.parsed_content = msg.file;
} else if (msgtype === 'location' && msg.location) {
parsedMsg.parsed_content = msg.location;
} else if (msgtype === 'link' && msg.link) {
parsedMsg.parsed_content = msg.link;
} else if (msgtype === 'business_card' && msg.business_card) {
parsedMsg.parsed_content = msg.business_card;
} else if (msgtype === 'miniprogram' && msg.miniprogram) {
parsedMsg.parsed_content = msg.miniprogram;
} else if (msgtype === 'msgmenu' && msg.msgmenu) {
parsedMsg.parsed_content = msg.msgmenu;
} else if (msgtype === 'channels_shop_product' && msg.channels_shop_product) {
parsedMsg.parsed_content = msg.channels_shop_product;
} else if (msgtype === 'channels_shop_order' && msg.channels_shop_order) {
parsedMsg.parsed_content = msg.channels_shop_order;
} else if (msgtype === 'merged_msg' && msg.merged_msg) {
parsedMsg.parsed_content = msg.merged_msg;
} else if (msgtype === 'channels' && msg.channels) {
parsedMsg.parsed_content = msg.channels;
} else if (msgtype === 'event' && msg.event) {
parsedMsg.parsed_content = msg.event;
// 为事件类型添加event_type字段方便筛选
parsedMsg.event_type = (msg.event as IDataObject).event_type;
}
return parsedMsg;
});
}
} else if (operation === 'getCustomerInfo') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const external_userid = this.getNodeParameter('external_userid', i) as string;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/customer/get_upgrade_service_config', {
open_kfid,
external_userid,
});
}
// 统计管理
else if (operation === 'getCorpStatistic') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const start_time = this.getNodeParameter('start_time', i) as number;
const end_time = this.getNodeParameter('end_time', i) as number;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/get_corp_statistic', {
open_kfid,
start_time,
end_time,
});
} else if (operation === 'getServicerStatistic') {
const open_kfid = this.getNodeParameter('open_kfid', i) as string;
const servicer_userid = this.getNodeParameter('servicer_userid', i) as string;
const start_time = this.getNodeParameter('start_time', i) as number;
const end_time = this.getNodeParameter('end_time', i) as number;
response = await weComApiRequest.call(this, 'POST', '/cgi-bin/kf/get_servicer_statistic', {
open_kfid,
servicer_userid,
start_time,
end_time,
});
}
// 机器人管理
else if (operation === 'manageKnowledgeGroup') {
const action_type = this.getNodeParameter('action_type', i) as string;
const params = this.getNodeParameter('params', i, '{}') as string;
let parsedParams;
try {
parsedParams = JSON.parse(params);
} catch (error) {
throw new NodeOperationError(this.getNode(), `params 必须是有效的 JSON: ${error.message}`, { itemIndex: i });
}
// 根据操作类型调用不同的API
let endpoint = '';
if (action_type === 'add') {
endpoint = '/cgi-bin/kf/knowledge/add_group';
} else if (action_type === 'del') {
endpoint = '/cgi-bin/kf/knowledge/del_group';
} else if (action_type === 'mod') {
endpoint = '/cgi-bin/kf/knowledge/mod_group';
} else if (action_type === 'list') {
endpoint = '/cgi-bin/kf/knowledge/list_group';
}
response = await weComApiRequest.call(this, 'POST', endpoint, parsedParams);
} else if (operation === 'manageKnowledgeIntent') {
const action_type = this.getNodeParameter('action_type', i) as string;
const params = this.getNodeParameter('params', i, '{}') as string;
let parsedParams;
try {
parsedParams = JSON.parse(params);
} catch (error) {
throw new NodeOperationError(this.getNode(), `params 必须是有效的 JSON: ${error.message}`, { itemIndex: i });
}
// 根据操作类型调用不同的API
let endpoint = '';
if (action_type === 'add') {
endpoint = '/cgi-bin/kf/knowledge/add_intent';
} else if (action_type === 'del') {
endpoint = '/cgi-bin/kf/knowledge/del_intent';
} else if (action_type === 'mod') {
endpoint = '/cgi-bin/kf/knowledge/mod_intent';
} else if (action_type === 'list') {
endpoint = '/cgi-bin/kf/knowledge/list_intent';
}
response = await weComApiRequest.call(this, 'POST', endpoint, parsedParams);
} else {
response = {};
}
returnData.push({
json: response,
pairedItem: { item: i },
});
} catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: {
error: error.message,
},
pairedItem: { item: i },
});
continue;
}
throw error;
}
}
return returnData;
}