@yaoyuanchao/dingtalk 1.4.6 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api.ts +36 -3
- package/src/monitor.ts +25 -3
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -70,6 +70,29 @@ function httpGetBuffer(url: string, headers?: Record<string, string>): Promise<B
|
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/** Retry wrapper for async functions */
|
|
74
|
+
async function withRetry<T>(
|
|
75
|
+
fn: () => Promise<T>,
|
|
76
|
+
maxRetries: number = 3,
|
|
77
|
+
delayMs: number = 1000,
|
|
78
|
+
backoffMultiplier: number = 2,
|
|
79
|
+
): Promise<T> {
|
|
80
|
+
let lastError: Error | undefined;
|
|
81
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
82
|
+
try {
|
|
83
|
+
return await fn();
|
|
84
|
+
} catch (err) {
|
|
85
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
86
|
+
if (attempt < maxRetries) {
|
|
87
|
+
const delay = delayMs * Math.pow(backoffMultiplier, attempt - 1);
|
|
88
|
+
console.log(`[dingtalk] Retry ${attempt}/${maxRetries} after ${delay}ms: ${lastError.message}`);
|
|
89
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
throw lastError;
|
|
94
|
+
}
|
|
95
|
+
|
|
73
96
|
export async function getDingTalkAccessToken(clientId: string, clientSecret: string): Promise<string> {
|
|
74
97
|
const cached = tokenCache.get(clientId);
|
|
75
98
|
if (cached && cached.expiresAt > Date.now() + 60_000) {
|
|
@@ -337,9 +360,14 @@ export async function downloadPicture(
|
|
|
337
360
|
return { error: response.errmsg || "Download failed" };
|
|
338
361
|
}
|
|
339
362
|
|
|
340
|
-
// If response has a file URL, download it
|
|
363
|
+
// If response has a file URL, download it with retry
|
|
341
364
|
if (response.downloadUrl) {
|
|
342
|
-
const imageBuffer = await
|
|
365
|
+
const imageBuffer = await withRetry(
|
|
366
|
+
() => httpGetBuffer(response.downloadUrl),
|
|
367
|
+
3, // maxRetries
|
|
368
|
+
1000, // initial delay 1s
|
|
369
|
+
2, // backoff multiplier
|
|
370
|
+
);
|
|
343
371
|
|
|
344
372
|
// Convert to base64
|
|
345
373
|
const base64 = imageBuffer.toString('base64');
|
|
@@ -404,7 +432,12 @@ export async function downloadMediaFile(
|
|
|
404
432
|
}
|
|
405
433
|
|
|
406
434
|
if (response.downloadUrl) {
|
|
407
|
-
const mediaBuffer = await
|
|
435
|
+
const mediaBuffer = await withRetry(
|
|
436
|
+
() => httpGetBuffer(response.downloadUrl),
|
|
437
|
+
3, // maxRetries
|
|
438
|
+
1000, // initial delay 1s
|
|
439
|
+
2, // backoff multiplier
|
|
440
|
+
);
|
|
408
441
|
|
|
409
442
|
if (!fs.existsSync(TEMP_DIR)) {
|
|
410
443
|
fs.mkdirSync(TEMP_DIR, { recursive: true });
|
package/src/monitor.ts
CHANGED
|
@@ -199,7 +199,8 @@ async function extractMessageContent(
|
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
|
|
202
|
+
// Process records with async image downloads
|
|
203
|
+
const formattedRecords = await Promise.all(records.map(async (record, idx) => {
|
|
203
204
|
// Try: senderNick > API resolved name (via staffId or senderId) > fallback
|
|
204
205
|
let sender = record.senderNick;
|
|
205
206
|
if (!sender) {
|
|
@@ -223,7 +224,28 @@ async function extractMessageContent(
|
|
|
223
224
|
break;
|
|
224
225
|
case 'picture':
|
|
225
226
|
case 'image':
|
|
226
|
-
|
|
227
|
+
// Try to download the image
|
|
228
|
+
if (record.downloadCode && account.clientId && account.clientSecret) {
|
|
229
|
+
try {
|
|
230
|
+
const robotCode = account.robotCode || account.clientId;
|
|
231
|
+
const pictureResult = await downloadPicture(
|
|
232
|
+
account.clientId, account.clientSecret, robotCode!, record.downloadCode,
|
|
233
|
+
);
|
|
234
|
+
if (pictureResult.filePath) {
|
|
235
|
+
msgContent = `[图片: ${pictureResult.filePath}]`;
|
|
236
|
+
log?.info?.("[dingtalk] Downloaded chatRecord picture: " + pictureResult.filePath);
|
|
237
|
+
} else if (pictureResult.error) {
|
|
238
|
+
msgContent = `[图片下载失败: ${pictureResult.error}]`;
|
|
239
|
+
} else {
|
|
240
|
+
msgContent = '[图片]';
|
|
241
|
+
}
|
|
242
|
+
} catch (err) {
|
|
243
|
+
log?.info?.("[dingtalk] Error downloading chatRecord picture: " + err);
|
|
244
|
+
msgContent = '[图片]';
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
msgContent = '[图片]';
|
|
248
|
+
}
|
|
227
249
|
break;
|
|
228
250
|
case 'video':
|
|
229
251
|
msgContent = '[视频]';
|
|
@@ -246,7 +268,7 @@ async function extractMessageContent(
|
|
|
246
268
|
}
|
|
247
269
|
const time = record.createAt ? new Date(record.createAt).toLocaleString('zh-CN') : '';
|
|
248
270
|
return `[${idx + 1}] ${sender}${time ? ` (${time})` : ''}: ${msgContent}`;
|
|
249
|
-
});
|
|
271
|
+
}));
|
|
250
272
|
const text = `[聊天记录合集 - ${records.length}条消息]\n${formattedRecords.join('\n')}`;
|
|
251
273
|
log?.info?.("[dingtalk] Parsed chatRecord with " + records.length + " messages");
|
|
252
274
|
return {
|