You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

/**
* Google Calendar Node.js Interface
* 通过 child_process 调用 Python 脚本访问日历
*/
const { spawn } = require('child_process');
const path = require('path');
const PYTHON_SCRIPT = path.join(__dirname, '..', 'google-calendar', 'google_calendar.py');
const CREDENTIALS_PATH = '/root/.openclaw/credentials/google-calendar.json';
/**
* 调用 Python Google Calendar 脚本
* @param {string} command - 日历命令 (today, tomorrow, week, status)
* @returns {Promise<string>} 日历信息
*/
async function getCalendarInfo(command = 'today') {
return new Promise((resolve, reject) => {
const pythonProcess = spawn('python3', [PYTHON_SCRIPT, command], {
env: {
...process.env,
PYTHONPATH: path.join(__dirname, '..', 'google-calendar')
}
});
let output = '';
let errorOutput = '';
pythonProcess.stdout.on('data', (data) => {
output += data.toString();
});
pythonProcess.stderr.on('data', (data) => {
errorOutput += data.toString();
});
pythonProcess.on('close', (code) => {
if (code === 0) {
resolve(output.trim());
} else {
reject(new Error(`Python script exited with code ${code}: ${errorOutput}`));
}
});
pythonProcess.on('error', (err) => {
reject(err);
});
});
}
/**
* 测试日历连接
*/
async function testCalendarConnection() {
try {
const result = await getCalendarInfo('status');
return {
connected: result.includes('✅'),
message: result
};
} catch (error) {
return {
connected: false,
message: `❌ 连接失败:${error.message}`
};
}
}
// 命令行测试
if (require.main === module) {
const command = process.argv[2] || 'today';
getCalendarInfo(command)
.then(result => console.log(result))
.catch(err => console.error('Error:', err.message));
}
module.exports = { getCalendarInfo, testCalendarConnection };