@yaoyuanchao/dingtalk 1.4.12 → 1.4.14
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 +12 -4
- package/src/channel.ts +44 -1
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -167,18 +167,26 @@ export async function sendDingTalkRestMessage(params: {
|
|
|
167
167
|
userId?: string;
|
|
168
168
|
conversationId?: string;
|
|
169
169
|
text: string;
|
|
170
|
+
format?: 'text' | 'markdown';
|
|
170
171
|
}): Promise<{ ok: boolean }> {
|
|
171
172
|
const token = await getDingTalkAccessToken(params.clientId, params.clientSecret);
|
|
172
173
|
const headers = { "x-acs-dingtalk-access-token": token };
|
|
173
174
|
|
|
175
|
+
// Use markdown format by default for better rendering
|
|
176
|
+
const useMarkdown = params.format !== 'text';
|
|
177
|
+
const msgKey = useMarkdown ? 'sampleMarkdown' : 'sampleText';
|
|
178
|
+
const msgParam = useMarkdown
|
|
179
|
+
? JSON.stringify({ title: 'AI', text: params.text })
|
|
180
|
+
: JSON.stringify({ content: params.text });
|
|
181
|
+
|
|
174
182
|
if (params.userId) {
|
|
175
183
|
const res = await jsonPost(
|
|
176
184
|
`${DINGTALK_API_BASE}/robot/oToMessages/batchSend`,
|
|
177
185
|
{
|
|
178
186
|
robotCode: params.robotCode,
|
|
179
187
|
userIds: [params.userId],
|
|
180
|
-
msgKey
|
|
181
|
-
msgParam
|
|
188
|
+
msgKey,
|
|
189
|
+
msgParam,
|
|
182
190
|
},
|
|
183
191
|
headers,
|
|
184
192
|
);
|
|
@@ -194,8 +202,8 @@ export async function sendDingTalkRestMessage(params: {
|
|
|
194
202
|
{
|
|
195
203
|
robotCode: params.robotCode,
|
|
196
204
|
openConversationId: params.conversationId,
|
|
197
|
-
msgKey
|
|
198
|
-
msgParam
|
|
205
|
+
msgKey,
|
|
206
|
+
msgParam,
|
|
199
207
|
},
|
|
200
208
|
headers,
|
|
201
209
|
);
|
package/src/channel.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getDingTalkRuntime } from './runtime.js';
|
|
2
2
|
import { resolveDingTalkAccount } from './accounts.js';
|
|
3
3
|
import { startDingTalkMonitor } from './monitor.js';
|
|
4
|
-
import { sendDingTalkRestMessage } from './api.js';
|
|
4
|
+
import { sendDingTalkRestMessage, uploadMediaFile, sendFileMessage, textToMarkdownFile } from './api.js';
|
|
5
5
|
import { probeDingTalk } from './probe.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -187,6 +187,49 @@ export const dingtalkPlugin = {
|
|
|
187
187
|
const account = resolveDingTalkAccount({ cfg, accountId });
|
|
188
188
|
const { type, id } = parseOutboundTo(to);
|
|
189
189
|
|
|
190
|
+
// Check longTextMode config
|
|
191
|
+
const longTextMode = account.config?.longTextMode ?? 'chunk';
|
|
192
|
+
const longTextThreshold = account.config?.longTextThreshold ?? 4000;
|
|
193
|
+
|
|
194
|
+
// If longTextMode is 'file' and text exceeds threshold, send as file
|
|
195
|
+
if (longTextMode === 'file' && text.length > longTextThreshold) {
|
|
196
|
+
console.log(`[dingtalk] Outbound text exceeds threshold (${text.length} > ${longTextThreshold}), sending as file`);
|
|
197
|
+
|
|
198
|
+
const { buffer, fileName } = textToMarkdownFile(text, 'AI Response');
|
|
199
|
+
|
|
200
|
+
// Upload file
|
|
201
|
+
const uploadResult = await uploadMediaFile({
|
|
202
|
+
clientId: account.clientId,
|
|
203
|
+
clientSecret: account.clientSecret,
|
|
204
|
+
robotCode: account.robotCode || account.clientId,
|
|
205
|
+
fileBuffer: buffer,
|
|
206
|
+
fileName,
|
|
207
|
+
fileType: 'file',
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
if (uploadResult.mediaId) {
|
|
211
|
+
// Send file message
|
|
212
|
+
const sendResult = await sendFileMessage({
|
|
213
|
+
clientId: account.clientId,
|
|
214
|
+
clientSecret: account.clientSecret,
|
|
215
|
+
robotCode: account.robotCode || account.clientId,
|
|
216
|
+
userId: type === 'dm' ? id : undefined,
|
|
217
|
+
conversationId: type === 'group' ? id : undefined,
|
|
218
|
+
mediaId: uploadResult.mediaId,
|
|
219
|
+
fileName,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
if (sendResult.ok) {
|
|
223
|
+
console.log(`[dingtalk] File sent successfully via outbound: ${fileName}`);
|
|
224
|
+
return { channel: 'dingtalk', ok: true };
|
|
225
|
+
}
|
|
226
|
+
console.log(`[dingtalk] File send failed, falling back to text: ${sendResult.error}`);
|
|
227
|
+
} else {
|
|
228
|
+
console.log(`[dingtalk] File upload failed, falling back to text: ${uploadResult.error}`);
|
|
229
|
+
}
|
|
230
|
+
// Fall through to text sending if file send fails
|
|
231
|
+
}
|
|
232
|
+
|
|
190
233
|
if (type === 'dm') {
|
|
191
234
|
await sendDingTalkRestMessage({
|
|
192
235
|
clientId: account.clientId,
|