@yaoyuanchao/dingtalk 1.4.15 → 1.4.16

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/channel.ts +79 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaoyuanchao/dingtalk",
3
- "version": "1.4.15",
3
+ "version": "1.4.16",
4
4
  "type": "module",
5
5
  "description": "DingTalk channel plugin for Clawdbot with Stream Mode support",
6
6
  "license": "MIT",
package/src/channel.ts CHANGED
@@ -347,6 +347,85 @@ export const dingtalkPlugin = {
347
347
  },
348
348
  },
349
349
 
350
+ // Handle message actions (sendAttachment, etc.)
351
+ actions: {
352
+ supportsAction({ action }: { action: string }) {
353
+ return action === 'sendAttachment' || action === 'send';
354
+ },
355
+
356
+ async handleAction(ctx: any) {
357
+ const { action, params, cfg, accountId } = ctx;
358
+
359
+ // Only handle sendAttachment action
360
+ if (action !== 'sendAttachment') {
361
+ return null; // Let SDK handle other actions
362
+ }
363
+
364
+ const buffer = params?.buffer;
365
+ const filename = params?.filename || 'attachment.bin';
366
+ const target = params?.target;
367
+
368
+ if (!buffer || !target) {
369
+ return null; // Let SDK handle if missing required params
370
+ }
371
+
372
+ const account = resolveDingTalkAccount({ cfg, accountId });
373
+ const { type, id } = parseOutboundTo(target);
374
+
375
+ // Decode base64 buffer
376
+ let fileBuffer: Buffer;
377
+ try {
378
+ fileBuffer = Buffer.from(buffer, 'base64');
379
+ } catch {
380
+ return { ok: false, error: 'Invalid base64 buffer' };
381
+ }
382
+
383
+ // Add UTF-8 BOM for text files
384
+ const isTextFile = /\.(txt|md|json|csv|xml|html?)$/i.test(filename);
385
+ if (isTextFile) {
386
+ const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
387
+ // Check if BOM already exists
388
+ if (fileBuffer[0] !== 0xEF || fileBuffer[1] !== 0xBB || fileBuffer[2] !== 0xBF) {
389
+ fileBuffer = Buffer.concat([bom, fileBuffer]);
390
+ }
391
+ }
392
+
393
+ // Upload file
394
+ const uploadResult = await uploadMediaFile({
395
+ clientId: account.clientId,
396
+ clientSecret: account.clientSecret,
397
+ robotCode: account.robotCode || account.clientId,
398
+ fileBuffer,
399
+ fileName: filename,
400
+ fileType: 'file',
401
+ });
402
+
403
+ if (!uploadResult.mediaId) {
404
+ console.warn(`[dingtalk] sendAttachment upload failed: ${uploadResult.error}`);
405
+ return { ok: false, error: uploadResult.error };
406
+ }
407
+
408
+ // Send file
409
+ const sendResult = await sendFileMessage({
410
+ clientId: account.clientId,
411
+ clientSecret: account.clientSecret,
412
+ robotCode: account.robotCode || account.clientId,
413
+ userId: type === 'dm' ? id : undefined,
414
+ conversationId: type === 'group' ? id : undefined,
415
+ mediaId: uploadResult.mediaId,
416
+ fileName: filename,
417
+ });
418
+
419
+ if (!sendResult.ok) {
420
+ console.warn(`[dingtalk] sendAttachment send failed: ${sendResult.error}`);
421
+ return { ok: false, error: sendResult.error };
422
+ }
423
+
424
+ console.log(`[dingtalk] sendAttachment success: ${filename}`);
425
+ return { ok: true, channel: 'dingtalk', filename };
426
+ },
427
+ },
428
+
350
429
  gateway: {
351
430
  async startAccount({ account, signal, setStatus }) {
352
431
  const runtime = getDingTalkRuntime();