#!/usr/bin/env python3 """ OpenClaw 101 学习资料定时推送脚本 通过 cron 定时调用,发送到飞书 """ import sys import os # 添加工作区到路径 sys.path.insert(0, '/root/.openclaw/workspace') try: from send_lesson_to_feishu import generate_lesson_message, get_learning_plan, get_current_day, save_current_day except ImportError: # 如果导入失败,直接复制函数 import json import datetime def get_learning_plan(): plan_path = "/root/.openclaw/workspace/code-generate/openclaw-learning-plan.json" with open(plan_path, 'r', encoding='utf-8') as f: return json.load(f) def get_current_day(): state_file = "/root/.openclaw/workspace/code-generate/learning-state.json" if os.path.exists(state_file): with open(state_file, 'r', encoding='utf-8') as f: state = json.load(f) day = state.get('current_day', 1) else: day = 1 return day def save_current_day(day): state_file = "/root/.openclaw/workspace/code-generate/learning-state.json" with open(state_file, 'w', encoding='utf-8') as f: json.dump({'current_day': day}, f, ensure_ascii=False, indent=2) def generate_lesson_message(day, plan): total_days = len(plan['plan']) if day > total_days: return generate_extra_resources_message(day - total_days, plan) lesson = plan['plan'][day - 1] message = f"""✨ **OpenClaw 101 学习计划 - 第 {day}/{total_days} 天** 📚 **今日主题:{lesson['topic']}** --- """ for i, resource in enumerate(lesson['resources'], 1): message += f"""🔗 **{i}. {resource['title']}** 📝 {resource['desc']} 🌐 {resource['url']} """ message += f"---\n💡 今天就先学习这些内容吧!明天继续第 {day + 1} 天的内容~" return message def generate_extra_resources_message(index, plan): extra = plan['extra_resources'] resource = extra[index % len(extra)] message = f"""🎉 **OpenClaw 101 7天计划已完成!** 现在开始为你推荐更多精选资源: --- 🔗 **{resource['title']}** 📝 {resource['desc']} 🌐 {resource['url']} --- 💡 每天都会为你推荐一个新资源! """ return message def main(): plan = get_learning_plan() day = get_current_day() message = generate_lesson_message(day, plan) # 输出消息到 stdout,cron 会捕获 print(message) # 同时保存到文件,方便调试 log_file = "/tmp/openclaw-lesson.log" with open(log_file, 'a', encoding='utf-8') as f: f.write(f"\n{'='*60}\n") f.write(f"推送时间: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") f.write(f"{'='*60}\n") f.write(message + "\n") # 保存下一天 save_current_day(day + 1) return message if __name__ == "__main__": main()