- 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
91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
// 使用钉钉旧版 API(oapi.dingtalk.com)发送图片消息
|
||
const axios = require('axios');
|
||
const FormData = require('form-data');
|
||
|
||
const ACCESS_TOKEN_URL = "https://oapi.dingtalk.com/gettoken";
|
||
const UPLOAD_URL = "https://oapi.dingtalk.com/media/upload";
|
||
const APP_KEY = "ding4ursdp0l2giat4bj";
|
||
const APP_SECRET = "J0gBicjKiIHoKla7WfKKhRs1Tv8L6Xd5UhW3EVQByF16G7Vn7UUcRhP6u-PBCQNo";
|
||
const OPEN_CONVERSATION_ID = "cidcjYshXVtKck5LfOO9AqOJg==";
|
||
const ROBOT_CODE = "ding4ursdp0l2giat4bj";
|
||
const IMAGE_PATH = "C:/Users/ALC/.openclaw/workspace/yahoo_japan_screenshot.jpg";
|
||
|
||
async function getAccessToken() {
|
||
const response = await axios.get(ACCESS_TOKEN_URL, {
|
||
params: {
|
||
appkey: APP_KEY,
|
||
appsecret: APP_SECRET
|
||
}
|
||
});
|
||
return response.data.access_token;
|
||
}
|
||
|
||
async function uploadMedia(accessToken, filePath, type) {
|
||
const form = new FormData();
|
||
form.append('media', require('fs').createReadStream(filePath));
|
||
form.append('type', type);
|
||
|
||
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 sendConversationMessage(accessToken, conversationId, media_id) {
|
||
const url = `https://oapi.dingtalk.com/message/sendtoconversation_v2?access_token=${accessToken}`;
|
||
|
||
const body = {
|
||
msg: {
|
||
msgtype: "image",
|
||
image: {
|
||
media_id: media_id
|
||
}
|
||
},
|
||
sender_unionid: ROBOT_CODE,
|
||
cid: conversationId,
|
||
robotCode: ROBOT_CODE
|
||
};
|
||
|
||
try {
|
||
console.log('发送消息 Body:', JSON.stringify(body, null, 2));
|
||
const response = await axios.post(url, body);
|
||
|
||
console.log('发送响应:', JSON.stringify(response.data, null, 2));
|
||
|
||
if (response.data.errcode === 0) {
|
||
console.log('\n✅ 图片消息发送成功!');
|
||
return true;
|
||
} else {
|
||
console.log('\n❌ 发送失败:', response.data.errmsg);
|
||
return false;
|
||
}
|
||
} catch (err) {
|
||
console.log('\n❌ 发送异常:', err.message);
|
||
if (err.response) {
|
||
console.log('响应数据:', JSON.stringify(err.response.data, null, 2));
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
|
||
async function main() {
|
||
try {
|
||
console.log('使用钉钉旧版 API...\n');
|
||
|
||
const accessToken = await getAccessToken();
|
||
console.log('✓ Access Token 获取成功\n');
|
||
|
||
const media_id = await uploadMedia(accessToken, IMAGE_PATH, "image");
|
||
console.log('✓ 媒体上传成功\n');
|
||
|
||
await sendConversationMessage(accessToken, OPEN_CONVERSATION_ID, media_id);
|
||
} catch (err) {
|
||
console.error('错误:', err.message);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
main();
|