claude cisteni skillu remind

This commit is contained in:
lachtan
2026-06-10 08:35:31 +02:00
parent addd153e8f
commit edf823fc72
5 changed files with 261 additions and 88 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.11"
# dependencies = ["croniter", "pyyaml"]
# dependencies = ["croniter"]
# ///
"""Deterministic reminder sender backed by SQLite.
@@ -21,7 +21,6 @@ from datetime import date, datetime, timedelta
from pathlib import Path
from zoneinfo import ZoneInfo
import yaml
from croniter import croniter
from db import get_db, init_db, log_operation
from random_times import compute_fire_times
@@ -32,19 +31,23 @@ DB_PATH = Path(os.environ.get("REMIND_DB", str(DEFAULT_DB_PATH)))
CONFIG = Path.home() / ".nanobot" / "config.json"
TZ = ZoneInfo("Europe/Prague")
CHAT_ID = "8826147089"
TOLERANCE_SECONDS = 60
FALLBACK_CHAT_ID = "8826147089"
def _telegram_token() -> str:
def _telegram_config() -> tuple[str, str]:
"""Return (bot token, chat id). Chat id reads channels.telegram.allowFrom[0], with a constant fallback."""
data = json.loads(CONFIG.read_text(encoding="utf-8"))
return data["channels"]["telegram"]["token"]
telegram = data["channels"]["telegram"]
allow_from = telegram.get("allowFrom") or []
chat_id = str(allow_from[0]) if allow_from else FALLBACK_CHAT_ID
return telegram["token"], chat_id
def _send_telegram(text: str) -> None:
token = _telegram_token()
token, chat_id = _telegram_config()
url = f"https://api.telegram.org/bot{token}/sendMessage"
payload = urllib.parse.urlencode({"chat_id": CHAT_ID, "text": text}).encode()
payload = urllib.parse.urlencode({"chat_id": chat_id, "text": text}).encode()
req = urllib.request.Request(url, data=payload, method="POST")
with urllib.request.urlopen(req, timeout=15) as resp:
resp.read()
@@ -75,7 +78,7 @@ def _due_at(conn, now: datetime) -> list[dict]:
""",
(since, until),
).fetchall()
return [dict(r) for r in rows]
return [{**dict(r), "schedule_type": "at"} for r in rows]
def _due_cron(conn, now: datetime) -> list[dict]:
@@ -107,6 +110,7 @@ def _due_cron(conn, now: datetime) -> list[dict]:
"text": row["text"],
"schedule_id": row["schedule_id"],
"fire_time": fire_iso,
"schedule_type": "cron",
})
return due
@@ -156,6 +160,7 @@ def _due_random(conn, now: datetime) -> list[dict]:
"text": row["text"],
"schedule_id": row["schedule_id"],
"fire_time": fire_iso,
"schedule_type": "random",
})
return due
@@ -189,14 +194,7 @@ def main() -> None:
rid = fire["id"]
sid = fire["schedule_id"]
ft = fire["fire_time"]
# Determine schedule_type from which query produced it
# We can infer: if 'schedule_id' came from schedule_at, it's 'at'
# But we don't have that info here. Let's look it up.
st = conn.execute(
"SELECT 'at' FROM schedule_at WHERE id = ? UNION ALL SELECT 'cron' FROM schedule_cron WHERE id = ? UNION ALL SELECT 'random' FROM schedule_random WHERE id = ?",
(sid, sid, sid),
).fetchone()
schedule_type = st[0] if st else "unknown"
schedule_type = fire["schedule_type"]
try:
_send_telegram(f"⏰ Reminder: {text}")