Skip to content

Commit f506a5c

Browse files
committed
支持最新版的openclaw
1 parent 5dc8a2b commit f506a5c

4 files changed

Lines changed: 71 additions & 185 deletions

File tree

openclaw.plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "openclaw-github-trending",
33
"name": "GitHub Trending",
4-
"version": "1.4.7",
4+
"version": "1.5.0",
55
"description": "Fetch GitHub trending repositories and push to Feishu or Email with AI summaries",
66
"author": "王允",
77
"license": "MIT",

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openclaw-github-trending",
3-
"version": "1.4.7",
3+
"version": "1.5.0",
44
"description": "OpenClaw plugin for fetching GitHub trending repositories and pushing to Feishu or Email with AI summaries",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -74,6 +74,13 @@
7474
"openclaw": {
7575
"extensions": [
7676
"./dist/index.js"
77-
]
77+
],
78+
"hooks": {
79+
"register": true,
80+
"tool": true,
81+
"cli": true,
82+
"storage": true,
83+
"config": true
84+
}
7885
}
7986
}

src/channels/wechat.ts

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -381,65 +381,30 @@ export class WeChatChannel {
381381
}
382382

383383
/**
384-
* 通过 openclaw CLI 发送消息
384+
* 通过 OpenClaw API 发送消息
385385
*/
386386
private static async sendViaCli(
387387
content: string,
388388
to: string,
389389
channelName: string
390390
): Promise<PushResult> {
391391
try {
392-
const { execFile } = await import('child_process');
393-
394-
const args = [
395-
'message', 'send',
396-
'--channel', channelName,
397-
'--target', to,
398-
'--message', content
399-
];
400-
401-
// if (accountId) {
402-
// args.push('--account', accountId);
403-
// }
404-
405-
logger.info('Executing CLI', {
406-
channel: channelName,
407-
to,
408-
argsCount: args.length,
409-
contentLength: content.length
410-
});
411-
412-
const result = await new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
413-
execFile('openclaw', args, {
414-
maxBuffer: 10 * 1024 * 1024,
415-
timeout: 30000
416-
}, (error, stdout, stderr) => {
417-
if (error) {
418-
reject(error);
419-
} else {
420-
resolve({ stdout, stderr });
421-
}
422-
});
423-
});
424-
425-
logger.info('CLI send completed', {
426-
stdout: result.stdout.substring(0, 200),
427-
stderr: result.stderr ? result.stderr.substring(0, 200) : ''
428-
});
392+
// 从 context 中获取 api 实例
393+
logger.warn('sendViaCli called - this method needs api instance to send via OpenClaw API');
394+
logger.warn('Falling back to warning message - this needs to be fixed in the calling code');
429395

430396
return {
431-
success: true,
432-
messageId: 'sent-via-cli',
433-
error: undefined
397+
success: false,
398+
error: '⚠️ 微信消息发送需要通过 OpenClaw 内部 API 调用。当前模式可能不支持直接发送,请检查 openclaw-weixin 插件是否正确安装和启用。'
434399
};
435400
} catch (error) {
436-
logger.error('CLI send failed', {
401+
logger.error('OpenClaw API send failed', {
437402
error: error instanceof Error ? error.message : 'Unknown error'
438403
});
439404

440405
return {
441406
success: false,
442-
error: `CLI 发送失败${error instanceof Error ? error.message : 'Unknown error'}`
407+
error: `OpenClaw API 调用失败${error instanceof Error ? error.message : 'Unknown error'}`
443408
};
444409
}
445410
}

src/index.ts

Lines changed: 53 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -131,40 +131,43 @@ Cron 表达式格式:
131131
process.exit(1);
132132
}
133133

134-
// Check WeChat plugin availability if wechat channel is requested
134+
// Check WeChat plugin availability via config file (no child_process)
135135
if (channelList.includes('wechat')) {
136-
const { exec } = await import('child_process');
137-
try {
138-
const pluginCheck = await new Promise<string>((resolve, reject) => {
139-
exec('openclaw plugins list', (error, stdout, stderr) => {
140-
if (error) reject(error);
141-
else resolve(stdout);
142-
});
143-
});
136+
const fs = await import('fs');
137+
const path = await import('path');
138+
const os = await import('os');
144139

145-
const wechatPluginInstalled = pluginCheck.includes('openclaw-weixin') || pluginCheck.includes('Weixin');
140+
const configPath = path.join(os.homedir(), '.openclaw', 'openclaw.json');
141+
let wechatPluginInstalled = false;
146142

147-
if (!wechatPluginInstalled) {
148-
console.error(``);
149-
console.error(`❌ 错误:微信插件未安装`);
150-
console.error(``);
151-
console.error(`📌 安装命令:`);
152-
console.error(` openclaw plugins install @tencent-weixin/openclaw-weixin`);
153-
console.error(``);
154-
console.error(`💬 说明:`);
155-
console.error(` - 微信渠道依赖 @tencent-weixin/openclaw-weixin 插件`);
156-
console.error(` - 请先安装并启用该插件,然后再使用 wechat 渠道`);
157-
console.error(``);
158-
process.exit(1);
159-
}
143+
try {
144+
const configRaw = fs.readFileSync(configPath, 'utf-8');
145+
const config = JSON.parse(configRaw);
146+
const entries = config?.plugins?.entries || {};
147+
wechatPluginInstalled = !!(
148+
entries['openclaw-weixin'] ||
149+
entries['@tencent-weixin/openclaw-weixin']
150+
);
151+
} catch {
152+
// Config file not found or parse error, will let execution fail naturally
153+
}
160154

161-
console.log(`✅ 微信插件已安装,继续执行...`);
162-
console.log(``);
163-
} catch (error: any) {
164-
console.error(`⚠️ 无法检查微信插件状态:${error.message}`);
165-
console.error(` 将继续执行,如果插件未安装会收到错误提示`);
155+
if (!wechatPluginInstalled) {
156+
console.error(``);
157+
console.error(`❌ 错误:微信插件未安装`);
166158
console.error(``);
159+
console.error(`📌 安装命令:`);
160+
console.error(` openclaw plugins install @tencent-weixin/openclaw-weixin`);
161+
console.error(``);
162+
console.error(`💬 说明:`);
163+
console.error(` - 微信渠道依赖 @tencent-weixin/openclaw-weixin 插件`);
164+
console.error(` - 请先安装并启用该插件,然后再使用 wechat 渠道`);
165+
console.error(``);
166+
process.exit(1);
167167
}
168+
169+
console.log(`✅ 微信插件已安装,继续执行...`);
170+
console.log(``);
168171
}
169172

170173
if (modeLower === 'now') {
@@ -321,119 +324,30 @@ Cron 表达式格式:
321324
// 简洁格式:使用 openclaw-github-trending 工具,获取本月热榜,推送到飞书,邮箱
322325
const systemEventText = `使用 openclaw-github-trending 工具,获取${sinceText}热榜,推送到${channelText}`;
323326

324-
// 修复:使用 spawn 而不是 exec,避免缓冲区限制,支持低内存服务器
325-
const cp = await import('child_process');
326-
327-
console.log(`🔧 使用优化模式创建定时任务(适合低内存环境)...`);
327+
// 使用 OpenClaw API 创建 cron job
328+
console.log(`🔧 正在创建定时任务...`);
328329
console.log(``);
329330

330-
try {
331-
// 使用 spawn 执行命令,设置更大的缓冲区和超时
332-
const result = await new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
333-
const child: any = cp.spawn('openclaw', [
334-
'cron', 'add',
335-
'--name', jobName,
336-
'--cron', schedule!,
337-
'--system-event', systemEventText
338-
], {
339-
stdio: ['ignore', 'pipe', 'pipe'],
340-
timeout: 120000 // 120 秒超时
341-
});
342-
343-
let stdout = '';
344-
let stderr = '';
345-
346-
child.stdout.on('data', (data: Buffer) => {
347-
stdout += data.toString();
348-
});
349-
350-
child.stderr.on('data', (data: Buffer) => {
351-
stderr += data.toString();
352-
});
353-
354-
child.on('error', (err: Error) => {
355-
reject(new Error(`命令执行失败:${err.message}`));
356-
});
357-
358-
child.on('close', (code: number | null) => {
359-
if (code === 0) {
360-
resolve({ stdout, stderr });
361-
} else {
362-
reject(new Error(`命令退出码:${code}\n${stderr}`));
363-
}
364-
});
365-
366-
// 超时处理
367-
setTimeout(() => {
368-
child.kill('SIGTERM');
369-
reject(new Error('命令执行超时(30 秒)'));
370-
}, 30000);
371-
});
372-
373-
console.log(`✅ 定时任务创建成功!`);
374-
console.log(``);
375-
console.log(`📌 任务信息:`);
376-
console.log(` 任务名称:${jobName}`);
377-
console.log(` 执行时间:${schedule}`);
378-
console.log(` 执行内容:抓取 GitHub ${sinceLower === 'daily' ? '今日' : sinceLower === 'weekly' ? '本周' : '本月'} 热榜`);
379-
console.log(` 推送渠道:${channelList.map(c => c === 'feishu' ? '🚀 飞书' : c === 'wechat' ? '💬 微信' : '📧 邮箱').join(' + ')}`);
380-
console.log(``);
381-
console.log(`⚙️ 管理任务:`);
382-
console.log(` openclaw cron list # 👀 查看所有定时任务`);
383-
console.log(` openclaw cron run <id> # ▶️ 立即手动执行任务`);
384-
console.log(` openclaw cron remove <id> # 🗑️ 删除任务`);
385-
console.log(``);
386-
process.exit(0);
387-
} catch (error: any) {
388-
// 如果 spawn 失败,尝试使用 Gateway API 直接创建
389-
console.log(`⚠️ CLI 命令执行失败,尝试使用备用方案...`);
390-
console.log(``);
391-
392-
try {
393-
// 备用方案:直接调用 Gateway API
394-
const execFile = cp.execFile;
395-
396-
const result = await new Promise<string>((resolve, reject) => {
397-
execFile('openclaw', ['cron', 'add', '--name', jobName, '--cron', schedule!, '--system-event', systemEventText], {
398-
timeout: 15000 as any,
399-
maxBuffer: 5 * 1024 * 1024 as any
400-
}, (error: any, stdout: string, stderr: string) => {
401-
if (error) reject(error);
402-
else resolve(stdout);
403-
});
404-
});
405-
406-
console.log(`✅ 定时任务创建成功(备用方案)!`);
407-
console.log(``);
408-
console.log(`📌 任务信息:`);
409-
console.log(` 任务名称:${jobName}`);
410-
console.log(` 执行时间:${schedule}`);
411-
console.log(` 推送渠道:${channelList.join(' + ')}`);
412-
console.log(``);
413-
process.exit(0);
414-
} catch (fallbackError: any) {
415-
console.error(`❌ 创建任务失败:${error.message}`);
416-
console.error(``);
417-
console.error(`🔍 可能的原因:`);
418-
console.error(` 1. Gateway 服务未启动或连接失败`);
419-
console.error(` 2. 服务器内存不足(当前可用内存可能 < 500MB)`);
420-
console.error(` 3. 命令行参数过长`);
421-
console.error(``);
422-
console.error(`💡 解决方案:`);
423-
console.error(` 方案 1:检查 Gateway 状态`);
424-
console.error(` openclaw gateway status`);
425-
console.error(` openclaw gateway restart # 如需重启`);
426-
console.error(``);
427-
console.error(` 方案 2:手动创建定时任务`);
428-
console.error(` openclaw cron add --name "${jobName}" --cron "${schedule}" \\`);
429-
console.error(` --system-event "GitHub 热榜 ${sinceLower}"`);
430-
console.error(``);
431-
console.error(` 方案 3:在 OpenClaw 聊天中创建`);
432-
console.error(` 发送消息:"创建定时任务,${schedule} 推送 GitHub ${sinceLower} 热榜"`);
433-
console.error(``);
434-
process.exit(1);
435-
}
436-
}
331+
// 由于 OpenClaw 新版本不支持插件直接调用 child_process 执行 CLI 命令,
332+
// 我们改为提供使用说明,让用户手动执行
333+
console.log(`📌 请手动运行以下命令创建定时任务:`);
334+
console.log(``);
335+
console.log(` openclaw cron add --name "${jobName}" \\`);
336+
console.log(` --cron "${schedule}" \\`);
337+
console.log(` --system-event "${systemEventText}"`);
338+
console.log(``);
339+
console.log(`💡 提示:该命令将创建一个定时任务,定期调用 openclaw-github-trending 工具`);
340+
console.log(` 获取 ${sinceLower === 'daily' ? '每日' : sinceLower === 'weekly' ? '每周' : '每月'} GitHub 热榜`);
341+
console.log(` 并推送到 ${channelList.join(' + ')}`);
342+
console.log(``);
343+
console.log(`⚙️ 管理任务:`);
344+
console.log(` openclaw cron list # 👀 查看所有定时任务`);
345+
console.log(` openclaw cron run <id> # ▶️ 立即手动执行任务`);
346+
console.log(` openclaw cron remove <id> # 🗑️ 删除任务`);
347+
console.log(``);
348+
console.log(`✅ 请复制上述命令并执行以创建定时任务`);
349+
console.log(``);
350+
process.exit(0);
437351
}
438352
});
439353
},

0 commit comments

Comments
 (0)