openclaw-home-pc/workspace/update_feishu_doc3.mjs
2026-03-21 15:31:06 +08:00

102 lines
2.5 KiB
JavaScript

// Node.js 22 has built-in fetch
// 使用飞书 MCP API 直接调用 update-doc 工具
const APP_ID = 'cli_a93eb0f160399cc5';
const APP_SECRET = 'dRzJKrJ46j2Y1DKneyC33dQUNmsLwHCj';
const DOC_ID = 'YerQd8GomoDWQ6xA1zTc31xfnwb';
const MCP_ENDPOINT = 'https://mcp.feishu.cn/mcp';
// 表格内容 (Markdown 格式)
const TABLE_CONTENT = `| 日期 | 规格 | 数量 |
|------|------|------|
| 24 年 12 月 | 细和润 | 2 |
| 24 年 12 月 | 金细 | 1 |
| 24 年 12 月 | 红中支 | 3 |
| 25 年 | 细和润 | 2 |
| 25 年 | 双支 | 5 |
**总计**: 13`;
async function getTenantAccessToken() {
const response = await fetch('https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
app_id: APP_ID,
app_secret: APP_SECRET,
}),
});
const data = await response.json();
if (data.code !== 0) {
throw new Error(`Failed to get access token: ${data.msg}`);
}
return data.tenant_access_token;
}
async function callMcpTool(token, toolName, args) {
// 调用 MCP 工具
const body = {
jsonrpc: '2.0',
id: 'call-' + Date.now(),
method: 'tools/call',
params: {
name: toolName,
arguments: args,
},
};
const headers = {
'Content-Type': 'application/json',
'X-Lark-MCP-UAT': token,
'X-Lark-MCP-Allowed-Tools': toolName,
'User-Agent': 'OpenClaw/2026.3.13',
};
const response = await fetch(MCP_ENDPOINT, {
method: 'POST',
headers,
body: JSON.stringify(body),
});
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
throw new Error(`MCP 返回非 JSON: ${text.slice(0, 4000)}`);
}
if ('error' in data) {
throw new Error(`MCP error ${data.error.code}: ${data.error.message}`);
}
return data.result;
}
async function main() {
try {
console.log('正在获取访问令牌...');
const token = await getTenantAccessToken();
console.log('✓ 获取访问令牌成功');
console.log('正在调用 MCP update-doc 工具...');
const result = await callMcpTool(token, 'update-doc', {
doc_id: DOC_ID,
mode: 'append',
markdown: TABLE_CONTENT,
});
console.log('✓ MCP 调用成功!');
console.log('响应:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('✗ 发生错误:', error.message);
console.error(error.stack);
process.exit(1);
}
}
main();