- 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
101 lines
3.1 KiB
JavaScript
101 lines
3.1 KiB
JavaScript
// 发送图片消息到钉钉群聊
|
|
const { default: dingtalkRobot, OrgGroupSendHeaders, OrgGroupSendRequest } = require('@alicloud/dingtalk/robot_1_0');
|
|
const { default: dingtalkOauth2_1_0, GetAccessTokenRequest } = require('@alicloud/dingtalk/oauth2_1_0');
|
|
const { Config } = require('@alicloud/openapi-client');
|
|
const { RuntimeOptions } = require('@alicloud/tea-util');
|
|
|
|
const APP_KEY = process.env.DINGTALK_APP_KEY || "ding4ursdp0l2giat4bj";
|
|
const APP_SECRET = process.env.DINGTALK_APP_SECRET || "J0gBicjKiIHoKla7WfKKhRs1Tv8L6Xd5UhW3EVQByF16G7Vn7UUcRhP6u-PBCQNo";
|
|
const OPEN_CONVERSATION_ID = "cidcjYshXVtKck5LfOO9AqOJg==";
|
|
const ROBOT_CODE = "4293382733";
|
|
const MEDIA_ID = "@lADPD0ni1-bFMwXNB9DNARg";
|
|
|
|
console.log('准备发送图片到钉钉群聊...');
|
|
console.log(`Conversation ID: ${OPEN_CONVERSATION_ID}`);
|
|
console.log(`Media ID: ${MEDIA_ID}`);
|
|
|
|
function createConfig() {
|
|
const config = new Config({});
|
|
config.protocol = "https";
|
|
config.regionId = "central";
|
|
return config;
|
|
}
|
|
|
|
async function getAccessToken(appKey, appSecret) {
|
|
const config = createConfig();
|
|
const client = new dingtalkOauth2_1_0(config);
|
|
|
|
const request = new GetAccessTokenRequest({
|
|
appKey: appKey,
|
|
appSecret: appSecret,
|
|
});
|
|
|
|
try {
|
|
const response = await client.getAccessToken(request);
|
|
const accessToken = response.body?.accessToken;
|
|
|
|
if (!accessToken) {
|
|
throw new Error('获取 access_token 失败');
|
|
}
|
|
|
|
console.log('Access Token 获取成功');
|
|
return accessToken;
|
|
} catch (err) {
|
|
throw new Error(`获取 access_token 失败: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
async function sendImageMessage(accessToken, openConversationId, robotCode, mediaId) {
|
|
const client = new dingtalkRobot(createConfig());
|
|
const headers = new OrgGroupSendHeaders({});
|
|
headers.xAcsDingtalkAccessToken = accessToken;
|
|
|
|
// 图片消息格式
|
|
const msgParam = JSON.stringify({
|
|
"mediaId": mediaId,
|
|
"altText": "日本 Yahoo 首页截图"
|
|
});
|
|
|
|
const request = new OrgGroupSendRequest({
|
|
openConversationId: openConversationId,
|
|
robotCode: robotCode,
|
|
msgKey: "sampleImage",
|
|
msgParam: msgParam,
|
|
});
|
|
|
|
try {
|
|
console.log('发送图片消息...');
|
|
const response = await client.orgGroupSendWithOptions(request, headers, new RuntimeOptions({}));
|
|
|
|
if (response.statusCode === 200 && response.body?.processQueryKey) {
|
|
console.log(`✅ 图片消息发送成功!`);
|
|
console.log(`Process Query Key: ${response.body.processQueryKey}`);
|
|
return true;
|
|
} else {
|
|
console.log('❌ 发送失败');
|
|
console.log('Status Code:', response.statusCode);
|
|
console.log('Body:', JSON.stringify(response.body, null, 2));
|
|
return false;
|
|
}
|
|
} catch (err) {
|
|
console.log('❌ 发送异常');
|
|
console.log('错误:', err.message);
|
|
if (err.data) {
|
|
console.log('详细错误:', JSON.stringify(err.data, null, 2));
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
try {
|
|
const accessToken = await getAccessToken(APP_KEY, APP_SECRET);
|
|
await sendImageMessage(accessToken, OPEN_CONVERSATION_ID, ROBOT_CODE, MEDIA_ID);
|
|
} catch (err) {
|
|
console.error('错误:', err.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|