|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Sync PDB inbound access-requests into the LifeOS CRM funnel. |
| 3 | +
|
| 4 | +Reads new rows from the palestine-data-api keys.db `access_requests` table and |
| 5 | +creates a crm_org in LifeOS for each, so pricing-page requests land in the OS |
| 6 | +funnel automatically instead of only pinging ntfy. Idempotent via a high-water- |
| 7 | +mark file — it never writes keys.db (so no permission/lock issues with the |
| 8 | +running api container). |
| 9 | +
|
| 10 | +Runs on `main`, where both the keys.db and LifeOS live. The LifeOS write token |
| 11 | +is read from ~/lifeos/state.json (NOT hard-coded here), or from $LIFEOS_TOKEN. |
| 12 | +
|
| 13 | + python3 scripts/lifeos-inbound-sync.py |
| 14 | +
|
| 15 | +Enable as a cron only once inbound volume warrants it (Decision Log: no premature |
| 16 | +machinery). Until then a manual run is enough. |
| 17 | +""" |
| 18 | +import json |
| 19 | +import os |
| 20 | +import pathlib |
| 21 | +import sqlite3 |
| 22 | +import urllib.request |
| 23 | + |
| 24 | +KEYS_DB = os.environ.get("KEYS_DB_PATH", "/opt/stacks/palestine/data/keys.db") |
| 25 | +LIFEOS = os.environ.get("LIFEOS_URL", "http://127.0.0.1:8377") |
| 26 | +STATE = pathlib.Path(os.environ.get("PDB_INBOUND_STATE", os.path.expanduser("~/.pdb_inbound_last"))) |
| 27 | + |
| 28 | + |
| 29 | +def lifeos_token(): |
| 30 | + tok = os.environ.get("LIFEOS_TOKEN") |
| 31 | + if tok: |
| 32 | + return tok |
| 33 | + # Fall back to the claude actor token in LifeOS state.json (never in this repo). |
| 34 | + with open(os.path.expanduser("~/lifeos/state.json")) as fh: |
| 35 | + d = json.load(fh) |
| 36 | + return (d.get("actor_tokens") or {}).get("claude") or d.get("token") |
| 37 | + |
| 38 | + |
| 39 | +def last_synced_id(): |
| 40 | + try: |
| 41 | + return int(STATE.read_text().strip()) |
| 42 | + except Exception: |
| 43 | + return 0 |
| 44 | + |
| 45 | + |
| 46 | +def create_crm_org(token, req): |
| 47 | + org = req.get("org") or req.get("email") |
| 48 | + body = { |
| 49 | + "name": f"{org} (PDB inbound)", |
| 50 | + "contact_person": req.get("email") or "", |
| 51 | + "channel": "pricing form", |
| 52 | + "warmth": "warm", |
| 53 | + "coi_status": "clear", |
| 54 | + "status": "not_contacted", |
| 55 | + "owner": "zaid", |
| 56 | + "notes": ( |
| 57 | + f"[PDB INBOUND] tier={req.get('tier')} · " |
| 58 | + f"use_case={req.get('use_case') or '-'} · requested {req.get('created_at')}" |
| 59 | + ), |
| 60 | + } |
| 61 | + r = urllib.request.Request( |
| 62 | + f"{LIFEOS}/api/crm/orgs", |
| 63 | + data=json.dumps(body).encode(), |
| 64 | + method="POST", |
| 65 | + headers={"X-Token": token, "Content-Type": "application/json"}, |
| 66 | + ) |
| 67 | + with urllib.request.urlopen(r, timeout=8) as resp: |
| 68 | + return resp.status |
| 69 | + |
| 70 | + |
| 71 | +def main(): |
| 72 | + if not os.path.exists(KEYS_DB): |
| 73 | + print(f"keys.db not found at {KEYS_DB} — nothing to sync") |
| 74 | + return |
| 75 | + token = lifeos_token() |
| 76 | + con = sqlite3.connect(f"file:{KEYS_DB}?mode=ro", uri=True) |
| 77 | + con.row_factory = sqlite3.Row |
| 78 | + since = last_synced_id() |
| 79 | + rows = con.execute( |
| 80 | + "SELECT id, name, org, email, tier, use_case, created_at " |
| 81 | + "FROM access_requests WHERE id > ? ORDER BY id", |
| 82 | + (since,), |
| 83 | + ).fetchall() |
| 84 | + con.close() |
| 85 | + |
| 86 | + if not rows: |
| 87 | + print(f"no new access_requests (last synced id {since})") |
| 88 | + return |
| 89 | + |
| 90 | + high = since |
| 91 | + for row in rows: |
| 92 | + req = dict(row) |
| 93 | + try: |
| 94 | + status = create_crm_org(token, req) |
| 95 | + print(f"req {req['id']} <{req['email']}> -> CRM HTTP {status}") |
| 96 | + high = max(high, req["id"]) |
| 97 | + except Exception as e: |
| 98 | + # Stop at first failure; do not advance the high-water mark past it, |
| 99 | + # so the next run retries this request. |
| 100 | + print(f"req {req['id']} FAILED: {e}") |
| 101 | + break |
| 102 | + |
| 103 | + STATE.write_text(str(high)) |
| 104 | + print(f"synced up to access_request id {high}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments