-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_bot.py
More file actions
721 lines (623 loc) · 33.9 KB
/
Copy pathcomment_bot.py
File metadata and controls
721 lines (623 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#!/usr/bin/env python3
"""comment_bot — grounded, human-in-the-loop comment automation for Instagram, Facebook & YouTube.
The pipeline (queue-first by design):
collect → pull unanswered comments from your recent IG reels, FB page reels
(Meta Graph API) and YouTube videos (YouTube Data API v3, optional —
see youtube_comments.py), classify them (praise/question/factual/
troll/other), ground factual ones against YOUR knowledge source,
draft replies in your voice — then append everything to a human
review queue.
post → publish ONLY the rows you approved (status ✅).
What makes it different — the guard stack (all deterministic, not prompt-hope):
• GROUNDING Factual comments are answered from a facts block produced by
your pluggable grounding hook (GROUNDING_CMD or FACTS_DIR).
• QUOTE MANDATE Anything inside quotation marks in a reply must appear
VERBATIM in the facts/thread source. Paraphrase is how drift
happens; quotes are how it's caught.
• NUMBER GUARD Every number in a reply must literally occur in the facts
block, or the reply is rewritten without numbers.
• KEYWORD GUARD Domain terms (DOMAIN_KEYWORDS) used in a reply must be backed
by the facts block.
• ERROR POLICY If a commenter rightly corrects you (checked against your own
content + facts), the reply owns the mistake — never deflects.
• VARIANCE A rolling memory of recent replies + similarity check makes
sure replies never look copy-pasted.
• THE LOOP Replies to YOUR replies are re-ingested with thread context;
when the user was right, a lesson is appended to LESSONS_FILE
and the last lessons are injected into every future prompt.
Configuration (env):
META_ENV path to env file with META_PAGE_TOKEN / META_PAGE_ID /
META_IG_USER_ID (default ~/.config/meta/.env)
YOUTUBE_ENV optional — path to env file with CLIENT_ID / CLIENT_SECRET /
REFRESH_TOKEN / CHANNEL_ID (default ~/.config/youtube/.env).
One-time setup: youtube_auth_setup.py. Missing file = YouTube
is silently skipped, IG/FB run unaffected.
GROUNDING_CMD executable; receives JSON {entities, comment, context} on
stdin, prints a facts block (your SSOT — quoted verbatim)
FACTS_DIR alternative: folder of .md files used as a naive fact base
REPLY_LANGUAGE language for replies (default: English)
COMMENT_PERSONA who the reply voice is (default: a friendly creator)
REPLY_MAX_CHARS max reply length (default 150)
DOMAIN_KEYWORDS comma-separated terms that trigger the keyword guard
COMMENT_QUEUE review queue path (default ./comment-queue.md)
LESSONS_FILE lessons ledger path (default ./comment-lessons.md)
CLAUDE_BIN LLM CLI (default: claude); swap _llm() for any model
comment_bot.py collect → pull + classify + ground + write queue
comment_bot.py post [--dry-run] → publish approved (✅) rows
"""
import difflib
import json
import os
import re
import shutil
import subprocess
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime
from pathlib import Path
HERE = Path(__file__).resolve().parent
STATE_DIR = Path(os.environ.get("STATE_DIR", HERE / "comment_data"))
STATE = STATE_DIR / "state.json"
QUEUE = Path(os.environ.get("COMMENT_QUEUE", HERE / "comment-queue.md"))
LESSONS = Path(os.environ.get("LESSONS_FILE", HERE / "comment-lessons.md"))
ENV = Path(os.environ.get("META_ENV", Path.home() / ".config" / "meta" / ".env"))
GRAPH = "https://graph.facebook.com/v25.0"
MEDIA_WINDOW = int(os.environ.get("MEDIA_WINDOW", 25))
THROTTLE_S = float(os.environ.get("THROTTLE_S", 2))
LANG = os.environ.get("REPLY_LANGUAGE", "English")
MAX_CHARS = int(os.environ.get("REPLY_MAX_CHARS", 150))
PERSONA = os.environ.get("COMMENT_PERSONA",
"You are the comment voice of a creator channel: honest, warm, expert, human.")
DOMAIN_KEYWORDS = [k.strip().upper() for k in os.environ.get("DOMAIN_KEYWORDS", "").split(",") if k.strip()]
RECENT_MAX, SIM_LIMIT = 50, 0.72
POST_BATCH_MAX = int(os.environ.get("POST_BATCH_MAX", 6)) # max publishes per run (drip)
POST_JITTER_S = os.environ.get("POST_JITTER_S", "20-90") # random pause between publishes
def _jitter():
"""Human-looking spacing between publishes. Nobody answers 60 comments in one minute."""
import random
lo, hi = (float(x) for x in POST_JITTER_S.split("-"))
if hi > 0:
time.sleep(random.uniform(lo, hi))
QUEUE_HEADER = """# Comment queue — review & approve
> Set the status: `⬜` = open · `✅` = approved (published on next `post`) · `❌` = skip.
> You may edit drafts in place — the cell content is what gets posted.
> The Evidence column shows what a draft is grounded on.
| Status | Date | Platform | User | Comment | Class | Draft reply | ID | Evidence | Video |
|---|---|---|---|---|---|---|---|---|---|
"""
# ── LLM + HTTP plumbing ──────────────────────────────────────────────────────────────────
def _llm(prompt: str, timeout_s: int = 240) -> str:
"""Headless call to the Claude Code CLI. Swap for any LLM you prefer."""
bin_ = os.environ.get("CLAUDE_BIN") or shutil.which("claude") or "claude"
p = subprocess.run([bin_, "-p", prompt], capture_output=True, text=True, timeout=timeout_s)
if p.returncode != 0:
raise RuntimeError(f"llm call failed: {(p.stderr or '')[:300]}")
return p.stdout
_env_cache = {"mtime": None, "data": {}}
def _env() -> dict:
"""Tolerant .env parser (export/quotes/whitespace/inline comments), cached by mtime."""
mtime = ENV.stat().st_mtime
if _env_cache["mtime"] == mtime:
return _env_cache["data"]
d = {}
for ln in ENV.read_text().splitlines():
ln = ln.strip()
if not ln or ln.startswith("#") or "=" not in ln:
continue
if ln.startswith("export "):
ln = ln[7:]
k, v = ln.split("=", 1)
v = v.split(" #")[0].strip().strip("'\"")
d[k.strip()] = v
_env_cache.update(mtime=mtime, data=d)
return d
_last = [0.0]
def _throttle():
wait = THROTTLE_S - (time.time() - _last[0])
if wait > 0:
time.sleep(wait)
def _request(url: str, data: bytes = None, method: str = "GET", retries: int = 3) -> dict:
"""Graph call with backoff on 429/5xx and readable API errors."""
for attempt in range(retries):
_throttle()
try:
req = urllib.request.Request(url, data=data, method=method)
with urllib.request.urlopen(req, timeout=30) as r:
_last[0] = time.time()
return json.loads(r.read())
except urllib.error.HTTPError as e:
body = e.read().decode(errors="ignore")[:300]
_last[0] = time.time()
if e.code in (429, 500, 502, 503) and attempt < retries - 1:
time.sleep(2 ** attempt * THROTTLE_S)
continue
raise RuntimeError(f"Graph API {e.code}: {body}") from None
def _get(path: str, **params) -> dict:
params.setdefault("access_token", _env()["META_PAGE_TOKEN"])
return _request(f"{GRAPH}/{path}?{urllib.parse.urlencode(params)}")
def _get_all(path: str, cap: int = 200, **params) -> list:
"""Follow paging.next so 'every unanswered comment' actually means every (README promise)."""
out, url = [], f"{GRAPH}/{path}?{urllib.parse.urlencode({**params, 'access_token': _env()['META_PAGE_TOKEN']})}"
while url and len(out) < cap:
d = _request(url)
out += d.get("data", [])
url = (d.get("paging") or {}).get("next")
return out[:cap]
def _post_api(path: str, **params) -> dict:
params.setdefault("access_token", _env()["META_PAGE_TOKEN"])
return _request(f"{GRAPH}/{path}", data=urllib.parse.urlencode(params).encode(), method="POST")
def _state() -> dict:
if STATE.exists():
return json.loads(STATE.read_text())
return {"seen": {}, "replied": {}, "recent_replies": []}
def _atomic_write(path: Path, text: str):
"""tmp + os.replace: a crash mid-write can never corrupt state or queue."""
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_text(text)
os.replace(tmp, path)
def _save_state(s: dict):
STATE_DIR.mkdir(parents=True, exist_ok=True)
_atomic_write(STATE, json.dumps(s, indent=1, ensure_ascii=False))
def _slice_array(text: str) -> list:
i, j = text.find("["), text.rfind("]")
if i < 0 or j <= i:
raise ValueError("no JSON array in output")
return json.loads(text[i:j + 1])
def _reply_dispatch(platform: str, comment_id: str, text: str) -> dict:
"""Publish a reply, tolerant of the full platform name ('instagram') or the 2-char
abbreviation the queue table stores ('in') — write_queue() truncates to that. Both
callers (_publish(), post()) used to inline `if platform.startswith("in"): IG else: FB`
— with a third platform that silently misroutes YouTube replies to the Facebook API
(wrong endpoint, wrong id semantics). YouTube needs OAuth2, not a Meta access token,
hence the separate module — imported lazily so it's optional (see YOUTUBE_ENV above)."""
p = platform[:2].lower()
if p == "in":
return _post_api(f"{comment_id}/replies", message=text)
if p == "fa":
return _post_api(f"{comment_id}/comments", message=text)
if p == "yo":
import youtube_comments
return youtube_comments.reply(comment_id, text)
raise ValueError(f"unknown platform {platform!r}")
# ── Collection (IG + FB + YouTube + follow-ups on our own replies) ──────────────────────
def fetch_yt() -> list:
"""Fail-soft: without YOUTUBE_ENV (setup not run), YouTube is simply skipped —
IG/FB continue unaffected."""
try:
import youtube_comments
return youtube_comments.fetch()
except Exception as e:
print(f"⚠️ YouTube skipped ({type(e).__name__}: {e})")
return []
def fetch_ig() -> list:
env, out = _env(), []
for m in _get_all(f"{env['META_IG_USER_ID']}/media",
fields="id,caption", limit=MEDIA_WINDOW, cap=MEDIA_WINDOW):
for c in _get_all(f"{m['id']}/comments",
fields="id,text,username,timestamp,replies{id}", limit=25):
if c.get("replies", {}).get("data"):
continue
out.append({"platform": "instagram", "id": c["id"], "user": c.get("username", "?"),
"text": c.get("text") or "", "video": (m.get("caption") or "")[:60]})
return out
def fetch_fb() -> list:
env, out = _env(), []
for v in _get_all(f"{env['META_PAGE_ID']}/video_reels",
fields="id,description", limit=MEDIA_WINDOW, cap=MEDIA_WINDOW):
for c in _get_all(f"{v['id']}/comments",
fields="id,message,from,comment_count", limit=25):
if c.get("comment_count", 0) > 0:
continue
frm = c.get("from") or {}
if str(frm.get("id")) == env["META_PAGE_ID"]:
continue
out.append({"platform": "facebook", "id": c["id"], "user": frm.get("name", "?"),
"text": c.get("message") or "", "video": (v.get("description") or "")[:60]})
return out
def fetch_followups(state: dict) -> list:
"""Replies to OUR published replies — the feedback loop's return channel."""
env, out = _env(), []
own_ig = os.environ.get("IG_USERNAME", "").lower()
yt = None # lazy-imported only if a YouTube record is actually present
for cid, rec in list(state.get("replied", {}).items())[-100:]:
if not isinstance(rec, dict):
continue
platform = rec.get("platform", "")
p = platform[:2].lower()
try:
if p == "in":
orig = _get(cid, fields="text").get("text", "")
kids = _get(f"{cid}/replies", fields="id,text,username").get("data", [])
for k in kids:
if own_ig and (k.get("username") or "").lower() == own_ig:
continue
out.append({"platform": "instagram", "id": k["id"], "user": k.get("username", "?"),
"text": k.get("text") or "", "video": "", "class": "followup",
"thread": {"original": orig, "our_reply": rec.get("reply", "")}})
elif p == "yo":
if yt is None:
import youtube_comments
yt = youtube_comments
for k in yt.fetch_replies(cid):
sn = k.get("snippet", {})
out.append({"platform": "youtube", "id": k["id"],
"user": sn.get("authorDisplayName", "?"),
"text": sn.get("textOriginal") or "", "video": "", "class": "followup",
"thread": {"original": "", "our_reply": rec.get("reply", "")}})
else:
orig = _get(cid, fields="message").get("message", "")
kids = _get(f"{cid}/comments", fields="id,message,from").get("data", [])
for k in kids:
if str((k.get("from") or {}).get("id")) == env["META_PAGE_ID"].strip():
continue
out.append({"platform": "facebook", "id": k["id"],
"user": (k.get("from") or {}).get("name", "?"),
"text": k.get("message") or "", "video": "", "class": "followup",
"thread": {"original": orig, "our_reply": rec.get("reply", "")}})
except Exception:
continue
return out
# ── Classification (batched) ─────────────────────────────────────────────────────────────
def classify(comments: list, chunk: int = 20) -> list:
pre = [c for c in comments if c.get("class")]
todo = [c for c in comments if not c.get("class")]
out = list(pre)
for k in range(0, len(todo), chunk):
part = todo[k:k + chunk]
out += _classify_chunk(part)
return out
def _classify_chunk(part: list, retries: int = 1) -> list:
for attempt in range(retries + 1):
try:
return _classify_once(part)
except Exception as e:
if attempt == retries:
print(f"⚠️ classify chunk failed after retry ({e}) — items stay UNSEEN for next run")
return []
def _classify_once(part: list) -> list:
blocks = [f"[{i}] ({c['platform']}, user {c['user']}, under: \"{c['video']}\")\n{c['text']}"
for i, c in enumerate(part)]
prompt = (f"{PERSONA}\n\nClassify each comment. classes:\n"
f" praise (compliment/agreement) | question (no factual core) | factual "
f"(contains or asks about a factual/domain claim, INCLUDING unit/meta assessments like 'X is bad / needs rework / underrated') | troll (insults/bait) | "
f"ignore (warrants no reply: friend-tagging, bare emoji strings, self-promo, spam) "
f"| other.\n"
f"confidence 0-1: how safe an auto-reply without human review would be.\n"
f'ONLY JSON: [{{"i":0,"class":"...","confidence":0.9,'
f'"entities":["thing the comment refers to"]}}]\n\n'
+ "\n\n".join(blocks))
out, seen_idx = [], set()
VALID = {"praise", "question", "factual", "troll", "ignore", "other"}
for r in _slice_array(_llm(prompt, 180)):
try: # strict validation: never trust model output shape
i = int(r["i"])
if i in seen_idx or not 0 <= i < len(part):
continue
seen_idx.add(i)
c = dict(part[i])
c["class"] = r.get("class") if r.get("class") in VALID else "other"
try:
c["confidence"] = min(1.0, max(0.0, float(r.get("confidence", 0))))
except (TypeError, ValueError):
c["confidence"] = 0.0
ents = r.get("entities")
c["entities"] = [str(e)[:60] for e in ents[:5]] if isinstance(ents, list) else []
out.append(c)
except (KeyError, TypeError, ValueError):
continue
return out
# ── Grounding hook (pluggable) ───────────────────────────────────────────────────────────
def ground(c: dict) -> str:
"""Facts block for a comment. GROUNDING_CMD (your SSOT script) wins; FACTS_DIR is a
naive markdown retriever fallback. Empty string = no grounding available."""
cmd = os.environ.get("GROUNDING_CMD")
if cmd:
try:
p = subprocess.run(cmd, input=json.dumps(
{"entities": c.get("entities", []), "comment": c["text"],
"context": c.get("video", "")}), capture_output=True, text=True,
timeout=90, shell=True)
return (p.stdout or "").strip()[:4000]
except Exception as e:
print(f"⚠️ GROUNDING_CMD failed ({e})")
return ""
fdir = os.environ.get("FACTS_DIR")
if fdir and Path(fdir).is_dir():
toks = set(re.findall(r"[a-z0-9]{3,}", (c["text"] + " " + " ".join(c.get("entities", []))).lower()))
scored = []
for f in Path(fdir).glob("**/*.md"):
for para in f.read_text(errors="ignore").split("\n\n"):
ov = len(toks & set(re.findall(r"[a-z0-9]{3,}", para.lower())))
if ov >= 2:
scored.append((ov, para.strip()))
scored.sort(key=lambda x: -x[0])
return "\n\n".join(p for _, p in scored[:8])[:4000]
return ""
# ── Deterministic guards ─────────────────────────────────────────────────────────────────
_QUOTE_RE = re.compile(r'["“”„]([^"“”„]{6,}?)["“”„]')
def _norm(s: str) -> str:
return re.sub(r"\s+", " ", (s or "").upper())
def guard_check(reply: str, source: str) -> dict:
"""All deterministic checks against the grounding source. Empty dict values = pass."""
src = _norm(source)
quotes = [q.strip() for q in _QUOTE_RE.findall(reply)]
fake_quotes = [q for q in quotes if _norm(q).strip(" .!?…") not in src]
numbers = re.findall(r"\d+(?:[.,]\d+)?", reply)
unbacked_numbers = [n for n in numbers if n not in source]
unbacked_keywords = [k for k in DOMAIN_KEYWORDS if k in reply.upper() and k not in src]
needs_quote = bool(source) and (bool(unbacked_keywords) or bool(numbers)) and not quotes
style = []
if "—" in reply or "–" in reply:
style.append("em/en dash (use commas or periods)")
if re.search(r"\b(we|us|our|ours)\b", _QUOTE_RE.sub("", reply), re.I):
style.append('first person plural (say "I/me", one person runs this channel)')
ROOKIE = ("might have to try", "have to try that", "i'll have to test", "ill have to test",
"never thought of that", "sounds like it could", "might give that a go",
"will have to check that out")
if any(ph in reply.lower() for ph in ROOKIE):
style.append("rookie phrasing (speak from experience, not like a novice)")
return {"fake_quotes": fake_quotes, "unbacked_numbers": unbacked_numbers,
"unbacked_keywords": unbacked_keywords, "needs_quote": needs_quote,
"style": style,
"ok": not (fake_quotes or unbacked_numbers or unbacked_keywords
or needs_quote or style)}
def style_sanitize(reply: str) -> str:
"""Last-resort mechanical fixes (dashes only — pronouns need a rewrite, not a patch)."""
return re.sub(r"\s*[—–]\s*", ", ", reply).replace(" ,", ",").strip()
def _too_similar(reply: str, recent: list) -> bool:
r = reply.lower()
return any(difflib.SequenceMatcher(None, r, old.lower()).ratio() > SIM_LIMIT for old in recent)
# ── Lessons (the closed loop) ────────────────────────────────────────────────────────────
def _load_lessons(n: int = 5) -> str:
try:
rows = [ln for ln in LESSONS.read_text().splitlines() if ln.startswith("- ")]
return "\n".join(rows[-n:])
except Exception:
return ""
def _save_lesson(thread: dict, lesson: str):
if not LESSONS.exists():
LESSONS.write_text("# Comment lessons — collected by the follow-up loop\n\n"
"> One line per case where a commenter rightly corrected us.\n"
"> The last 5 are injected into every future reply prompt.\n\n")
with open(LESSONS, "a") as f:
f.write(f"- {datetime.now().date().isoformat()}: {lesson.strip()[:300]} "
f"(thread: “{(thread.get('original') or '')[:60]}…”)\n")
def followup_verdict(c: dict) -> dict:
t = c.get("thread") or {}
prompt = (f'Judge this comment thread. ONLY JSON: {{"verdict":"user_correct|we_correct|neutral",'
f'"lesson":"one sentence on what to do differently (only if user_correct)"}}\n\n'
f"Original: {t.get('original','')}\nOur reply: {t.get('our_reply','')}\n"
f"User's reaction: {c['text']}")
try:
out = _llm(prompt, 90)
i, j = out.find("{"), out.rfind("}")
return json.loads(out[i:j + 1])
except Exception:
return {"verdict": "neutral", "lesson": ""}
# ── Drafting (grounded, guarded, varied) ─────────────────────────────────────────────────
def draft_reply(c: dict, facts: str, recent: list, revise_note: str = "") -> str:
thread = c.get("thread") or {}
thread_block = (f"\nTHREAD (this is a REACTION to our earlier reply!):\n"
f" Original comment: {thread.get('original','?')}\n"
f" OUR reply: {thread.get('our_reply','?')}\n"
f"Check FIRST: is the user rightly correcting us? Then own it and correct "
f"the record — never deflect or justify.\n") if thread else ""
lessons = _load_lessons()
lessons_block = f"\nLESSONS FROM PAST MISTAKES (respect these):\n{lessons}\n" if lessons else ""
avoid = ("\nFORBIDDEN (variance): do not open like, or reuse phrasings from, these recent "
"replies:\n- " + "\n- ".join(recent[-12:])) if recent else ""
prompt = f"""{PERSONA}
Reply to this comment under the video "{c.get('video','')}":
COMMENT ({c['user']}): {c['text']}
{thread_block}{lessons_block}
FACTS (your ONLY source of truth — claim nothing beyond it):
{facts or '(no verified facts available → make NO factual claims, no numbers)'}
RULES: under {MAX_CHARS} characters · in {LANG} · warm and human, no bot voice · max 1 emoji.
CORE-POINT RULE (hard): answer the comment's MAIN point, never just a side remark.
Side notes may be touched additionally, never instead.
AUTHORITY (hard): you speak from real experience with this domain. Judge ideas from
knowledge ("that combo does real work"), NEVER like a novice discovering them: no
"might have to try that", "I'll have to test", "never thought of that". Honest openness
only as expert framing ("numbers aren't out yet"), not beginner curiosity.
STYLE (humanizer rules, hard):
- First person singular only: I / me / my. NEVER "we/us/our" (one person runs this channel:
"you got me", not "you got us").
- NO em or en dashes (— –). Use commas or periods instead.
- No AI tells: no "it's not X, it's Y" reversals, no rule-of-three lists, no "Great question!"
openers, no "delve/dive into/vibrant/game-changer", no mirrored sentence lengths.
- Use contractions, uneven rhythm, ONE clear statement. Sounds like a fan typing on their
phone, not a support desk.
QUOTE MANDATE: when you state a fact/rule, carry it as a VERBATIM short quote in "…" from the
facts block (shortening allowed, rephrasing not). Numbers only if they appear in the facts.
If the facts don't cover the question, say exactly that — never speculate.
If the commenter is right and we were wrong: own it openly ("you're right — X was our wording,
the source only says Y").{avoid}
{revise_note}
Output ONLY the reply, nothing else."""
return _llm(prompt, 120).strip().strip('"')[:MAX_CHARS]
def grounded_reply(c: dict, state: dict) -> dict:
"""Full chain for factual/question/followup comments. Returns the item with reply+evidence."""
recent = state.setdefault("recent_replies", [])
if c.get("class") == "followup" and c.get("thread"):
v = followup_verdict(c)
c["verdict"] = v.get("verdict", "neutral")
if c["verdict"] == "user_correct" and v.get("lesson"):
_save_lesson(c["thread"], v["lesson"])
c["evidence"] = "lesson saved"
facts = ground(c)
source = facts + "\n" + json.dumps(c.get("thread") or {}, ensure_ascii=False)
try:
reply = draft_reply(c, facts, recent)
g = guard_check(reply, source)
if not g["ok"]:
note = ("\nFIX: " + "; ".join(filter(None, [
f"these quotes are not verbatim in the source: {g['fake_quotes']}" if g["fake_quotes"] else "",
f"these numbers are not in the facts, drop them: {g['unbacked_numbers']}" if g["unbacked_numbers"] else "",
f"these domain terms are unbacked: {g['unbacked_keywords']}" if g["unbacked_keywords"] else "",
"factual statements need a verbatim quote from the facts" if g["needs_quote"] else "",
f"style violations: {'; '.join(g['style'])}" if g["style"] else ""])))
reply = draft_reply(c, facts, recent, revise_note=note)
if not guard_check(reply, source)["ok"]:
reply = draft_reply(c, "", recent,
revise_note="\nFIX: write it with NO numbers, NO factual claims, "
"no dashes, first person singular.")
if _too_similar(reply, recent):
reply = draft_reply(c, facts, recent + [reply],
revise_note="\nFIX: structure this reply differently from all previous ones.")
c["reply"] = style_sanitize(reply)[:MAX_CHARS]
c.setdefault("evidence", "grounded" if facts else "no-facts (claim-free)")
recent.append(reply)
del recent[:-RECENT_MAX]
except Exception as e:
c["evidence"] = f"grounding-fail: {e}"
return c
def light_reply(c: dict, state: dict) -> dict:
"""Cheap path for praise/other — still variance-guarded."""
recent = state.setdefault("recent_replies", [])
try:
c["reply"] = draft_reply(c, "", recent)
if guard_check(c["reply"], "")["style"]:
c["reply"] = draft_reply(c, "", recent,
revise_note="\nFIX: no dashes, first person singular (I/me).")
if _too_similar(c["reply"], recent):
c["reply"] = draft_reply(c, "", recent + [c["reply"]],
revise_note="\nFIX: phrase it differently from all previous ones.")
c["reply"] = style_sanitize(c["reply"])
recent.append(c["reply"])
del recent[:-RECENT_MAX]
except Exception:
c["reply"] = ""
return c
# ── Disposition: who needs a human? ──────────────────────────────────────────────────────
AUTO_SEND = os.environ.get("AUTO_SEND", "0") == "1"
AUTO_CONFIDENCE = float(os.environ.get("AUTO_CONFIDENCE", 0.85))
def route(c: dict) -> str:
"""auto | review | ignore — deterministic gates on top of the classifier.
auto is only possible for praise/other with high confidence AND a reply with zero
factual surface (no digits, no quotes, no domain keywords, no style violations)."""
cls = c.get("class", "other")
if cls in ("troll", "ignore"):
return "ignore"
if cls in ("factual", "question", "followup"):
return "review"
r = (c.get("reply") or "").strip()
if (AUTO_SEND and c.get("confidence", 0) >= AUTO_CONFIDENCE and r
and not any(ch.isdigit() for ch in r)
and not any(q in r for q in '"“”„') # any quote char forces review
and not any(k in r.upper() for k in DOMAIN_KEYWORDS)
and not guard_check(r, "")["style"]
and len(r) <= MAX_CHARS):
return "auto"
return "review"
# ── Queue + publishing ───────────────────────────────────────────────────────────────────
def _esc(s: str) -> str:
return (s or "").replace("|", "/").replace("\n", " ").strip()
def write_queue(items: list):
if not QUEUE.exists():
QUEUE.write_text(QUEUE_HEADER)
with open(QUEUE, "a") as f:
for c in items:
status = {"ignore": "❌", "auto": "✅🤖"}.get(c.get("disposition"), "⬜")
f.write(f"| {status} | {datetime.now().strftime('%d.%m. %H:%M')} | {c['platform'][:2]} "
f"| {_esc(c['user'])[:20]} | {_esc(c['text'])[:200]} | {c.get('class','?')} "
f"| {_esc(c.get('reply',''))[:MAX_CHARS]} | `{c['id']}` "
f"| {_esc(c.get('evidence',''))[:60]} | {_esc(c.get('video',''))[:50]} |\n")
def _publish(c: dict, s: dict) -> bool:
try:
_reply_dispatch(c["platform"], c["id"], c["reply"])
s["replied"][c["id"]] = {"ts": datetime.now().isoformat(timespec="seconds"),
"reply": c["reply"], "platform": c["platform"]}
return True
except Exception as e:
print(f"⚠️ publish {c['id']}: {e}")
return False
def collect():
s = _state()
fresh = [c for c in fetch_ig() + fetch_fb() + fetch_yt() + fetch_followups(s)
if c["id"] not in s["seen"] and c["text"].strip()]
print(f"{len(fresh)} new unanswered comments")
if not fresh:
return
items = classify(fresh)
done, auto_sent = [], 0
for c in items:
if c.get("class") in ("factual", "question", "followup"):
done.append(grounded_reply(c, s))
elif c.get("class") in ("troll", "ignore"):
c["reply"] = ""
done.append(c)
else:
done.append(light_reply(c, s))
for c in done: # Triage: auto | review | ignore
c["disposition"] = route(c)
if c["disposition"] == "auto":
if auto_sent:
_jitter() # drip: auto replies are spaced out too
if _publish(c, s):
auto_sent += 1
continue
elif c["disposition"] == "auto":
c["disposition"] = "review" # publish failed → human decides
write_queue(done)
queued_ids = {c["id"] for c in done}
lost = sum(1 for c in fresh if c["id"] not in queued_ids)
for c in fresh:
if c["id"] in queued_ids: # fail-closed: dropped chunks stay UNSEEN
s["seen"][c["id"]] = datetime.now().isoformat(timespec="seconds")
if lost:
print(f"⚠️ {lost} comments not queued (chunk failures) — they stay unseen and retry next run")
_save_state(s)
from collections import Counter
print("classes:", dict(Counter(c.get("class", "?") for c in done)))
print("dispositions:", dict(Counter(c.get("disposition", "?") for c in done)),
f"· auto-published: {auto_sent}")
print(f"queue: {QUEUE}")
def post(dry: bool = False):
if not QUEUE.exists():
print("no queue"); return
s = _state()
lines = QUEUE.read_text().splitlines()
posted = 0
for n, ln in enumerate(lines):
if not ln.strip().startswith("|"):
continue
cells = [x.strip() for x in ln.strip().strip("|").split("|")]
# cell-based status check: editors (Obsidian etc.) re-align tables with padding,
# so prefix matching breaks. ✅ posts, ✅📤 (already sent) never posts again.
if len(cells) < 8 or cells[0] != "✅":
continue
platform, reply, cid = cells[2], cells[6], cells[7].strip("`")
if cid in s["replied"] or not reply:
continue
if dry:
print(f"WOULD reply [{platform}] to {cid}: {reply}"); posted += 1; continue
if posted >= POST_BATCH_MAX:
continue # drip: rest stays ✅ for the next run
if posted:
_jitter()
try:
_reply_dispatch(platform, cid, reply)
s["replied"][cid] = {"ts": datetime.now().isoformat(timespec="seconds"),
"reply": reply, "platform": platform}
lines[n] = re.sub(r"^(\|\s*)✅(\s*\|)", r"\g<1>✅📤\g<2>", ln, count=1)
posted += 1
print(f"📤 [{platform}] {reply[:60]}")
except Exception as e:
print(f"⚠️ {cid}: {e}")
if not dry:
_atomic_write(QUEUE, "\n".join(lines) + "\n")
_save_state(s)
approved_left = sum(1 for ln in QUEUE.read_text().splitlines() if ln.startswith("| ✅ |"))
print(f"{posted} replies {'planned' if dry else 'published'}"
+ (f" · {approved_left} approved rows left for the next drip run" if not dry and approved_left else ""))
if __name__ == "__main__":
cmd = sys.argv[1] if len(sys.argv) > 1 else "collect"
if cmd == "collect":
collect()
elif cmd == "post":
post(dry="--dry-run" in sys.argv)
else:
sys.exit("usage: comment_bot.py collect | post [--dry-run]")