@@ -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