Add OpenClaw workspace configuration and tools
- Add agent configuration files (AGENTS.md, USER.md, IDENTITY.md, SOUL.md) - Add git configuration and skills management scripts - Add frontend/backend analysis tools and reports - Add DingTalk media sender utilities and documentation - Fix OpenClaw runtime environment (Node.js and Python) - Configure git remotes and push scripts
This commit is contained in:
144
send_test_file.js
Normal file
144
send_test_file.js
Normal file
@@ -0,0 +1,144 @@
|
||||
// 上传并发送测试文件到钉钉群聊
|
||||
const axios = require('axios');
|
||||
const FormData = require('form-data');
|
||||
|
||||
const ACCESS_TOKEN_URL = "https://api.dingtalk.com/v1.0/oauth2/accessToken";
|
||||
const UPLOAD_URL = "https://oapi.dingtalk.com/media/upload";
|
||||
const SEND_URL = "https://api.dingtalk.com/v1.0/robot/groupMessages/send";
|
||||
|
||||
const APP_KEY = "ding4ursdp0l2giat4bj";
|
||||
const APP_SECRET = "J0gBicjKiIHoKla7WfKKhRs1Tv8L6Xd5UhW3EVQByF16G7Vn7UUcRhP6u-PBCQNo";
|
||||
const OPEN_CONVERSATION_ID = "cidcjYshXVtKck5LfOO9AqOJg==";
|
||||
const ROBOT_CODE = "ding4ursdp0l2giat4bj";
|
||||
const FILE_PATH = "C:/Users/ALC/.openclaw/workspace/test_file.txt";
|
||||
|
||||
async function getAccessToken() {
|
||||
const response = await axios.post(ACCESS_TOKEN_URL, {
|
||||
appKey: APP_KEY,
|
||||
appSecret: APP_SECRET
|
||||
});
|
||||
return response.data.accessToken;
|
||||
}
|
||||
|
||||
async function uploadFile(accessToken, filePath, type) {
|
||||
const form = new FormData();
|
||||
form.append('media', require('fs').createReadStream(filePath));
|
||||
form.append('type', type);
|
||||
|
||||
console.log('\n上传文件...');
|
||||
console.log(`文件路径: ${filePath}`);
|
||||
console.log(`文件大小: ${require('fs').statSync(filePath).size} bytes`);
|
||||
|
||||
const response = await axios.post(`${UPLOAD_URL}?access_token=${accessToken}`, form, {
|
||||
headers: form.getHeaders()
|
||||
});
|
||||
|
||||
console.log('上传响应:', JSON.stringify(response.data, null, 2));
|
||||
|
||||
return response.data.media_id;
|
||||
}
|
||||
|
||||
async function sendFileMessage(accessToken, media_id, fileName) {
|
||||
const headers = {
|
||||
"x-acs-dingtalk-access-token": accessToken,
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
|
||||
// 测试文件消息
|
||||
const body = {
|
||||
robotCode: ROBOT_CODE,
|
||||
openConversationId: OPEN_CONVERSATION_ID,
|
||||
msgKey: "sampleFile",
|
||||
msgParam: JSON.stringify({
|
||||
mediaId: media_id,
|
||||
fileName: fileName || "测试文件"
|
||||
})
|
||||
};
|
||||
|
||||
try {
|
||||
console.log('\n发送文件到钉钉群聊...');
|
||||
console.log(`Body:\n${JSON.stringify(body, null, 2)}\n`);
|
||||
|
||||
const response = await axios.post(SEND_URL, body, { headers });
|
||||
|
||||
if (response.status === 200) {
|
||||
console.log(`✅ 文件发送成功!ProcessQueryKey: ${response.data.processQueryKey}\n`);
|
||||
return true;
|
||||
} else {
|
||||
console.log(`❌ 发送失败\n`);
|
||||
return false;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(`❌ 发送异常: ${err.message}`);
|
||||
if (err.response?.data) {
|
||||
console.log(`详细错误: ${JSON.stringify(err.response.data, null, 2)}\n`);
|
||||
}
|
||||
|
||||
// 如果 sampleFile 失败,尝试用 Markdown
|
||||
console.log('\n尝试使用 Markdown 格式发送文件...');
|
||||
return sendFileAsMarkdown(accessToken, fileName, media_id);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendFileAsMarkdown(accessToken, fileName, media_id) {
|
||||
const headers = {
|
||||
"x-acs-dingtalk-access-token": accessToken,
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
|
||||
const cleanMediaId = media_id.replace('@', '');
|
||||
|
||||
const body = {
|
||||
robotCode: ROBOT_CODE,
|
||||
openConversationId: OPEN_CONVERSATION_ID,
|
||||
msgKey: "sampleMarkdown",
|
||||
msgParam: JSON.stringify({
|
||||
title: `📄 ${fileName}`,
|
||||
text: `📎 文件已上传到钉钉服务器\n\n**文件名**: ${fileName}\n**media_id**: @${cleanMediaId}\n\n请在钉钉客户端中查看和下载文件。`
|
||||
})
|
||||
};
|
||||
|
||||
try {
|
||||
console.log(`Body (Markdown 格式):\n${JSON.stringify(body, null, 2)}\n`);
|
||||
|
||||
const response = await axios.post(SEND_URL, body, { headers });
|
||||
|
||||
if (response.status === 200) {
|
||||
console.log(`✅ 文件消息发送成功(Markdown格式)!ProcessQueryKey: ${response.data.processQueryKey}\n`);
|
||||
return true;
|
||||
} else {
|
||||
console.log(`❌ Markdown 格式也失败\n`);
|
||||
return false;
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(`❌ Markdown 格式异常: ${err.message}\n`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
console.log('='.repeat(60));
|
||||
console.log('发送测试文件到钉钉群聊');
|
||||
console.log('='.repeat(60));
|
||||
|
||||
const accessToken = await getAccessToken();
|
||||
console.log('✓ Access Token 获取成功\n');
|
||||
|
||||
const media_id = await uploadFile(accessToken, FILE_PATH, "file");
|
||||
console.log(`✓ 文件上传成功`);
|
||||
console.log(` media_id: ${media_id}\n`);
|
||||
|
||||
await sendFileMessage(accessToken, media_id, "test_file.txt");
|
||||
|
||||
console.log('='.repeat(60));
|
||||
} catch (err) {
|
||||
console.error('\n错误:', err.message);
|
||||
if (err.response) {
|
||||
console.error('详细错误:', JSON.stringify(err.response.data, null, 2));
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user