@yaoyuanchao/dingtalk 1.4.14 → 1.4.15
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/channel.ts +51 -0
package/package.json
CHANGED
package/src/channel.ts
CHANGED
|
@@ -38,6 +38,7 @@ export const dingtalkPlugin = {
|
|
|
38
38
|
capabilities: {
|
|
39
39
|
chatTypes: ['direct', 'group'],
|
|
40
40
|
media: true, // Supports images via markdown in sessionWebhook replies
|
|
41
|
+
files: true, // Supports file upload and sending
|
|
41
42
|
threads: false,
|
|
42
43
|
reactions: false,
|
|
43
44
|
mentions: true,
|
|
@@ -251,6 +252,56 @@ export const dingtalkPlugin = {
|
|
|
251
252
|
return { channel: 'dingtalk', ok: true };
|
|
252
253
|
},
|
|
253
254
|
|
|
255
|
+
async sendFile({ to, content, fileName, accountId, cfg }) {
|
|
256
|
+
const account = resolveDingTalkAccount({ cfg, accountId });
|
|
257
|
+
const { type, id } = parseOutboundTo(to);
|
|
258
|
+
|
|
259
|
+
// Convert content to buffer if it's a string
|
|
260
|
+
let fileBuffer: Buffer;
|
|
261
|
+
if (typeof content === 'string') {
|
|
262
|
+
// Add UTF-8 BOM for text files (better Chinese display)
|
|
263
|
+
const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
|
|
264
|
+
const textContent = Buffer.from(content, 'utf-8');
|
|
265
|
+
fileBuffer = Buffer.concat([bom, textContent]);
|
|
266
|
+
} else if (Buffer.isBuffer(content)) {
|
|
267
|
+
fileBuffer = content;
|
|
268
|
+
} else {
|
|
269
|
+
throw new Error('content must be a string or Buffer');
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Upload file to DingTalk
|
|
273
|
+
const uploadResult = await uploadMediaFile({
|
|
274
|
+
clientId: account.clientId,
|
|
275
|
+
clientSecret: account.clientSecret,
|
|
276
|
+
robotCode: account.robotCode || account.clientId,
|
|
277
|
+
fileBuffer,
|
|
278
|
+
fileName: fileName || 'file.txt',
|
|
279
|
+
fileType: 'file',
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
if (!uploadResult.mediaId) {
|
|
283
|
+
throw new Error(`File upload failed: ${uploadResult.error}`);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Send file message
|
|
287
|
+
const sendResult = await sendFileMessage({
|
|
288
|
+
clientId: account.clientId,
|
|
289
|
+
clientSecret: account.clientSecret,
|
|
290
|
+
robotCode: account.robotCode || account.clientId,
|
|
291
|
+
userId: type === 'dm' ? id : undefined,
|
|
292
|
+
conversationId: type === 'group' ? id : undefined,
|
|
293
|
+
mediaId: uploadResult.mediaId,
|
|
294
|
+
fileName: fileName || 'file.txt',
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
if (!sendResult.ok) {
|
|
298
|
+
throw new Error(`File send failed: ${sendResult.error}`);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
console.log(`[dingtalk] File sent via outbound.sendFile: ${fileName}`);
|
|
302
|
+
return { channel: 'dingtalk', ok: true };
|
|
303
|
+
},
|
|
304
|
+
|
|
254
305
|
async sendMedia({ to, text, mediaUrl, accountId, cfg }) {
|
|
255
306
|
// Note: DingTalk REST API (oToMessages/groupMessages) doesn't support markdown or images
|
|
256
307
|
// Images can only be sent via sessionWebhook (when replying to messages)
|