diff --git a/scripts/check_cares.py b/scripts/check_cares.py index f6b7bdd6..f8b9a2da 100755 --- a/scripts/check_cares.py +++ b/scripts/check_cares.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -""" -check_cares.py — 牵挂检查与飞书提醒 -================================== +# -*- coding: utf-8 -*- +"""check_cares.py — 牵挂检查与飞书提醒 + 用法: python3 check_cares.py # 检查并推送今天到期的牵挂 python3 check_cares.py --list # 仅列出,不推送 @@ -9,8 +9,12 @@ check_cares.py — 牵挂检查与飞书提醒 依赖:soulful_core.py(同目录) """ +import sys, os +for _ in (sys.stdout, sys.stderr): + try: _.reconfigure(encoding='utf-8', errors='replace') + except Exception: pass -import json, os, sys, urllib.request, urllib.error +import json, urllib.request, urllib.error from datetime import datetime, timezone HERMES = os.path.expanduser("~/.hermes") @@ -21,32 +25,22 @@ FEISHU_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/446db983-e392-4d2 def send_feishu(title: str, content: str, color: str = "green"): """发送飞书消息""" - color_map = { - "green": ("34", "197", "94"), - "yellow": ("255", "185", "0"), - "red": ("255", "65", "54"), - "blue": ("0", "122", "255"), - "purple": ("128", "90", "213"), + color_key = color if color in ("green", "yellow", "red", "blue", "purple") else "blue" + card_obj = { + "header": { + "title": {"tag": "plain_text", "content": f"🎗️ {title}"}, + "template": color_key, + }, + "elements": [ + {"tag": "markdown", "content": content}, + ], } - r, g, b = color_map.get(color, color_map["blue"]) payload = { "msg_type": "interactive", - "card": { - "header": { - "title": {"tag": "plain_text", "content": f"🎗️ {title}"}, - "template": color, - }, - "elements": [ - {"tag": "div", "content": {"tag": "lark_md", "content": content}}, - ], - }, + "card": json.dumps(card_obj, ensure_ascii=False), } - data = json.dumps(payload, ensure_ascii=False).encode("utf-8") - req = urllib.request.Request( - FEISHU_WEBHOOK, - data=data, - headers={"Content-Type": "application/json"}, - ) + data = json.dumps(payload, ensure_ascii=True).encode("utf-8") + req = urllib.request.Request(FEISHU_WEBHOOK, data=data, headers={"Content-Type": "application/json; charset=utf-8"}) try: with urllib.request.urlopen(req, timeout=10) as resp: return json.loads(resp.read()) @@ -63,15 +57,13 @@ def generate_warm_text(care: dict, index: int = 1) -> str: follow_up_date = care.get("follow_up_date", "") tags = care.get("tags", []) - # 语气选择 if reminder_count == 0: - opener = f"你之前说过" + opener = "你之前说过" elif reminder_count == 1: - opener = f"上次提醒过一次了,还是想问一下" + opener = "上次提醒过一次了,还是想问一下" else: opener = f"已经提醒 {reminder_count} 次了,这件重要的事" - # 标签辅助语气 tag_hints = { "工作": "关于工作的事", "生活": "生活里的小事", @@ -84,7 +76,6 @@ def generate_warm_text(care: dict, index: int = 1) -> str: tag_hint = tag_hints[t] break - # 组合文案 parts = [opener] if tag_hint: parts.append(tag_hint) @@ -92,8 +83,8 @@ def generate_warm_text(care: dict, index: int = 1) -> str: if context and len(context) > 5: parts.append(f"(你说的是:{context[:40]})") if reminder_count > 0: - parts.append(f"——依然放在心上") - + parts.append("——依然放在心上") + return ",".join(parts) @@ -101,7 +92,7 @@ def format_daily_report(cares: list) -> str: """格式化每日牵挂清单""" if not cares: return '今天没有待提醒的牵挂 ✓\n\n你可以说"记得提醒我做XXX",我会帮你记住并准时提醒。' - + lines = [f"📋 今天有 **{len(cares)}** 件牵挂:\n"] for i, c in enumerate(cares, 1): follow_up = c.get("follow_up_date", "") @@ -118,47 +109,43 @@ def format_daily_report(cares: list) -> str: def main(): list_only = "--list" in sys.argv daily = "--daily" in sys.argv - + sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from soulful_core import CaresQueue - + cq = CaresQueue() - + if daily: - # 每日 21:00 模式:显示所有 pending pending = cq.pending() if not pending: print("无 pending 牵挂") return - + report = format_daily_report(pending) print(report) if not list_only: send_feishu("今日牵挂清单", report, "blue") return - - # 默认:检查今天到期的 + due = cq.today_check() - + if not due: print("没有今天到期的牵挂 ✓") return - + print(f"发现 {len(due)} 条到期牵挂:") for i, c in enumerate(due, 1): print(f" {i}. [{c['id']}] {c['content']}") - + if list_only: return - - # 发送飞书 + for care in due: text = generate_warm_text(care) print(f"\n推送: {text[:60]}...") result = send_feishu("你有一件事一直放在心上", text, "purple") if result and result.get("code") == 0: - # 更新 reminder_count - cq.snooze(care["id"], days=0) # 只增加 reminder_count,不推日期 + cq.snooze(care["id"], days=0) print(" ✓ 已推送") else: print(f" ✗ 推送失败: {result}")