@yaoyuanchao/dingtalk 1.4.11 → 1.4.12
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 +10 -14
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -497,7 +497,7 @@ export const cleanupOldPictures = cleanupOldMedia;
|
|
|
497
497
|
|
|
498
498
|
/**
|
|
499
499
|
* Upload a file to DingTalk and get media_id
|
|
500
|
-
* Uses the
|
|
500
|
+
* Uses the legacy oapi media upload endpoint (the new API doesn't exist)
|
|
501
501
|
*/
|
|
502
502
|
export async function uploadMediaFile(params: {
|
|
503
503
|
clientId: string;
|
|
@@ -514,16 +514,9 @@ export async function uploadMediaFile(params: {
|
|
|
514
514
|
const boundary = `----DingTalkBoundary${Date.now()}`;
|
|
515
515
|
const fileType = params.fileType || 'file';
|
|
516
516
|
|
|
517
|
-
// Construct multipart body
|
|
517
|
+
// Construct multipart body - oapi uses "media" as the file field name
|
|
518
518
|
const parts: Buffer[] = [];
|
|
519
519
|
|
|
520
|
-
// Add robotCode field
|
|
521
|
-
parts.push(Buffer.from(
|
|
522
|
-
`--${boundary}\r\n` +
|
|
523
|
-
`Content-Disposition: form-data; name="robotCode"\r\n\r\n` +
|
|
524
|
-
`${params.robotCode}\r\n`
|
|
525
|
-
));
|
|
526
|
-
|
|
527
520
|
// Add file field
|
|
528
521
|
parts.push(Buffer.from(
|
|
529
522
|
`--${boundary}\r\n` +
|
|
@@ -538,15 +531,14 @@ export async function uploadMediaFile(params: {
|
|
|
538
531
|
|
|
539
532
|
const body = Buffer.concat(parts);
|
|
540
533
|
|
|
541
|
-
//
|
|
542
|
-
const url =
|
|
534
|
+
// Use legacy oapi endpoint - the new v1.0 API doesn't have this endpoint
|
|
535
|
+
const url = `https://oapi.dingtalk.com/media/upload?access_token=${token}&type=${fileType}`;
|
|
543
536
|
|
|
544
537
|
return new Promise((resolve) => {
|
|
545
538
|
const urlObj = new URL(url);
|
|
546
539
|
const req = https.request(urlObj, {
|
|
547
540
|
method: 'POST',
|
|
548
541
|
headers: {
|
|
549
|
-
'x-acs-dingtalk-access-token': token,
|
|
550
542
|
'Content-Type': `multipart/form-data; boundary=${boundary}`,
|
|
551
543
|
'Content-Length': body.length,
|
|
552
544
|
},
|
|
@@ -558,12 +550,16 @@ export async function uploadMediaFile(params: {
|
|
|
558
550
|
res.on('end', () => {
|
|
559
551
|
try {
|
|
560
552
|
const json = JSON.parse(buf);
|
|
561
|
-
|
|
553
|
+
// oapi returns media_id (with underscore), not mediaId
|
|
554
|
+
if (json.media_id) {
|
|
555
|
+
console.log(`[dingtalk] File uploaded successfully: media_id=${json.media_id}`);
|
|
556
|
+
resolve({ mediaId: json.media_id });
|
|
557
|
+
} else if (json.mediaId) {
|
|
562
558
|
console.log(`[dingtalk] File uploaded successfully: mediaId=${json.mediaId}`);
|
|
563
559
|
resolve({ mediaId: json.mediaId });
|
|
564
560
|
} else {
|
|
565
561
|
console.warn(`[dingtalk] File upload failed:`, json);
|
|
566
|
-
resolve({ error: json.
|
|
562
|
+
resolve({ error: json.errmsg || json.message || 'Upload failed' });
|
|
567
563
|
}
|
|
568
564
|
} catch {
|
|
569
565
|
resolve({ error: `Invalid response: ${buf}` });
|