From 5fafc29dc178cebd15b0e18885d5be8fba3f806a Mon Sep 17 00:00:00 2001 From: Marius Mutu Date: Thu, 2 Apr 2026 19:43:01 +0000 Subject: [PATCH] chore: auto-commit from dashboard --- bridge/whatsapp/index.js | 29 +++ config.json | 49 +++-- cron/jobs.json | 29 ++- cron/newsletter-cercetasi-state.json | 5 +- src/adapters/discord_bot.py | 4 +- src/main.py | 2 + src/newsletter_cercetasi.py | 2 +- src/scheduler.py | 4 +- tools/check_newsletter_cercetasi.py | 279 +++++++++++++++++++++++++++ 9 files changed, 383 insertions(+), 20 deletions(-) create mode 100755 tools/check_newsletter_cercetasi.py diff --git a/bridge/whatsapp/index.js b/bridge/whatsapp/index.js index 1b3415b..b815914 100644 --- a/bridge/whatsapp/index.js +++ b/bridge/whatsapp/index.js @@ -17,6 +17,7 @@ let sock = null; let connected = false; let phoneNumber = null; let currentQR = null; +let currentPairingCode = null; let reconnectAttempts = 0; let messageQueue = []; let shuttingDown = false; @@ -122,6 +123,34 @@ app.get('/status', (_req, res) => { }); }); +app.post('/pair', async (req, res) => { + if (connected) { + return res.json({ error: 'already connected' }); + } + const { phone } = req.body || {}; + if (!phone) { + return res.status(400).json({ error: 'missing "phone" in body' }); + } + if (!sock) { + return res.status(503).json({ error: 'socket not ready yet, try again in a few seconds' }); + } + try { + const code = await sock.requestPairingCode(phone.replace(/\D/g, '')); + currentPairingCode = code; + console.log(`[whatsapp] Pairing code for ${phone}: ${code}`); + res.json({ ok: true, code }); + } catch (err) { + console.error('[whatsapp] Pairing code error:', err.message); + res.status(500).json({ error: err.message }); + } +}); + +app.get('/pair-code', (_req, res) => { + if (connected) return res.json({ error: 'already connected' }); + if (!currentPairingCode) return res.json({ error: 'no pairing code yet — POST /pair first' }); + res.json({ code: currentPairingCode }); +}); + app.get('/qr', (_req, res) => { if (connected) { return res.json({ error: 'already connected' }); diff --git a/config.json b/config.json index 4b75b12..2b17b82 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,9 @@ "name": "Echo", "default_model": "sonnet", "owner": "949388626146517022", - "admins": ["5040014994"] + "admins": [ + "5040014994" + ] }, "channels": { "echo-core": { @@ -29,7 +31,10 @@ "interval_minutes": 120, "channel": "echo-core", "model": "haiku", - "quiet_hours": [23, 7], + "quiet_hours": [ + 23, + 7 + ], "checks": { "email": true, "calendar": true, @@ -44,24 +49,42 @@ } }, "newsletter_cercetasi": { - "enabled": true, + "enabled": false, "cron": "0 17 * * 4,5,1", "channel": "echo-core" }, "allowed_tools": [ - "Read", "Edit", "Write", "Glob", "Grep", - "WebFetch", "WebSearch", - "Bash(python3 *)", "Bash(.venv/bin/python3 *)", - "Bash(pip *)", "Bash(pytest *)", + "Read", + "Edit", + "Write", + "Glob", + "Grep", + "WebFetch", + "WebSearch", + "Bash(python3 *)", + "Bash(.venv/bin/python3 *)", + "Bash(pip *)", + "Bash(pytest *)", "Bash(git *)", - "Bash(npm *)", "Bash(node *)", "Bash(npx *)", + "Bash(npm *)", + "Bash(node *)", + "Bash(npx *)", "Bash(systemctl --user *)", - "Bash(trash *)", "Bash(mkdir *)", "Bash(cp *)", - "Bash(mv *)", "Bash(ls *)", "Bash(cat *)", "Bash(chmod *)", - "Bash(docker *)", "Bash(docker-compose *)", "Bash(docker compose *)", - "Bash(ssh *@10.0.20.*)", "Bash(ssh root@10.0.20.*)", + "Bash(trash *)", + "Bash(mkdir *)", + "Bash(cp *)", + "Bash(mv *)", + "Bash(ls *)", + "Bash(cat *)", + "Bash(chmod *)", + "Bash(docker *)", + "Bash(docker-compose *)", + "Bash(docker compose *)", + "Bash(ssh *@10.0.20.*)", + "Bash(ssh root@10.0.20.*)", "Bash(ssh echo@10.0.20.*)", - "Bash(scp *10.0.20.*)", "Bash(rsync *10.0.20.*)" + "Bash(scp *10.0.20.*)", + "Bash(rsync *10.0.20.*)" ], "ollama": { "url": "http://10.0.20.161:11434" diff --git a/cron/jobs.json b/cron/jobs.json index fe51488..c11dbc3 100644 --- a/cron/jobs.json +++ b/cron/jobs.json @@ -1 +1,28 @@ -[] +[ + { + "name": "discord-test", + "cron": "0 18 2 4 *", + "channel": "echo-core", + "model": "haiku", + "prompt": "Răspunde doar cu textul: Test Discord cron job — funcționează!", + "allowed_tools": [], + "enabled": false, + "last_run": "2026-04-02T18:09:42.851876+00:00", + "last_status": "ok", + "next_run": null + }, + { + "name": "newsletter-test", + "cron": "0 0 1 1 *", + "channel": "echo-core", + "model": "sonnet", + "prompt": "Newsletter-ul Cercetașilor #13/2026 este disponibil la: https://cercetaiis-newsletter.beehiiv.com/p/newsletter-13-din-2026\n\nUrmează instrucțiunile de mai jos pentru a genera rezumatul:\n\n# Prompt: Rezumat Newsletter Cercetași pentru WhatsApp\n\n## CONTEXT\nEști un asistent care procesează newsletter-ul săptămânal al Organizației Naționale Cercetașii României și creează un rezumat structurat pentru distribuire pe WhatsApp. Scopul este să facilitezi accesul rapid la informații importante: deadline-uri, formulare, evenimente, proiecte.\n\n## TASK\n1. Accesează ultimul newsletter de la URL-ul: `https://cercetaiis-newsletter.beehiiv.com/p/newsletter-{număr}-din-2026`\n2. Extrage conținut complet\n3. Generează rezumat structurat conform template-ului de mai jos\n\n## TEMPLATE OUTPUT (OBLIGATORIU - FĂRĂ EMOJI)\n\n```\nNEWSLETTER CERCETAȘI #{număr}/2026\n\nDEADLINE-URI IMPORTANTE:\n- [dată]: [eveniment/activitate scurtă]\n- [dată]: [eveniment/activitate scurtă]\n[sortează cronologic, cel mai apropiat deadline primul]\n\nFORMULARE & PARTICIPARE:\n- [titlu formular/activitate]\n- [titlu formular/activitate]\n[doar itemuri cu call-to-action clar]\n\nPROIECTE ACTIVE:\n- [nume proiect]: [descriere 1 linie max]\n- [nume proiect]: [descriere 1 linie max]\n[doar proiecte în desfășurare sau cu impact imediat]\n\nLink complet:\n[URL newsletter original]\n\n---\nInformatii Consiliul Director saptamanale:\n[daca exista link specific, altfel omite sectiunea]\n```\n\n## REGULI DE PROCESARE\n\n### 1. PRIORITIZARE INFORMAȚIE\n- **DEADLINE-URI:** Extrage TOATE datele limită (DDL) + evenimente cu dată concretă\n- **FORMULARE:** Orice link către Google Forms, Beehiiv forms, etc. + descriere scurtă\n- **PROIECTE:** Doar proiecte în derulare sau cu impact direct asupra cititorului\n\n### 2. FILTRARE\n**INCLUDE:**\n- Oportunități de participare (training-uri, evenimente, voluntariat)\n- Formulare de înscriere/aplicare\n- Deadline-uri concrete\n- Proiecte cu call-to-action clar\n\n**EXCLUDE:**\n- Reflecții generale/filosofice\n- Povești fără acțiune concretă\n- Quote-uri motivaționale\n- Imagini/fotografii (doar text)\n\n### 3. FORMATARE TEXT\n- **FĂRĂ emoji** - text plain simplu\n- Maxim 1-2 linii per item\n- Păstrează link-uri originale (Google Forms, site-uri externe)\n- Deadline-uri: format \"DD luna\" (ex: \"10 aprilie\", \"15 mai\")\n\n### 4. IDENTIFICARE NUMĂR NEWSLETTER\n- URL format: `newsletter-{N}-din-2026`\n- Extrage numărul N din URL pentru titlu\n- Dacă nu știi numărul, CERE utilizatorului să specifice sau verifică ultimul newsletter disponibil\n\n## EXEMPLE\n\n### INPUT (Newsletter #13):\n```\n## Scouts go solar ambassador training [DDL: 10 aprilie 2026]\nProiectul va avea loc pe o perioadă de 9-12 luni...\n\n## Anunț recrutare voluntari români Healthy Mind Camp\n...completează formularul până pe 15 mai 2026!\n\n## Fii voluntar la Adunarea Generală!\n...completează formularul de aplicare până la data de 11 aprilie...\n```\n\n### OUTPUT:\n```\nNEWSLETTER CERCETAȘI #13/2026\n\nDEADLINE-URI IMPORTANTE:\n- 10 aprilie: Scouts go solar ambassador training\n- 11 aprilie: Voluntari Adunare Generală\n- 15 mai: Healthy Mind Camp - recrutare voluntari\n\nFORMULARE & PARTICIPARE:\n- Scouts go solar: https://share.google/Xi9MRT0NHFEiU3N3F\n- Voluntari AG: https://forms.gle/PuctjapaNcGeRzHx6\n- Healthy Mind Camp: https://docs.google.com/.../viewform\n\nPROIECTE ACTIVE:\n- Scouts Go Solar: formare 9-12 luni, etapă fizică KISC 2027\n- Healthy Mind Camp: 9-29 iulie Nocrich, sănătate mentală tineri\n\nLink complet:\nhttps://cercetaiis-newsletter.beehiiv.com/p/newsletter-13-din-2026\n```\n\n## FLUX DE LUCRU\n\n1. **Verifică ultimul newsletter:**\n - Începe cu numărul cel mai recent cunoscut (ex: #13)\n - Dacă 404, decrementează până găsești ultimul disponibil\n\n2. **Extrage conținut:**\n - Fetch HTML de la URL\n - Parse secțiuni (titluri, paragrafe, link-uri)\n\n3. **Identifică elemente cheie:**\n - Scan pentru \"DDL:\", \"deadline\", \"până la\", \"până pe\"\n - Scan pentru \"formular\", \"forms.gle\", \"docs.google.com/forms\"\n - Identifică proiecte cu descrieri acționabile\n\n4. **Sortează deadline-uri cronologic:**\n - Parsează datele (format \"DD lună YYYY\")\n - Sortează crescător (cel mai apropiat primul)\n\n5. **Generează output conform template**\n\n6. **Validare finală:**\n - Verifică că TOATE deadline-urile au fost capturate\n - Verifică că link-urile sunt complete și funcționale\n - Verifică lungimea textului (maxim 1-2 linii per item)\n\n## GESTIONARE ERORI\n\n- **Newsletter lipsă (404):** Raportează \"Newsletter #{N} nu este disponibil. Ultimul găsit: #{N-1}\"\n- **Lipsă deadline-uri:** Menționează \"Nu sunt deadline-uri urgente în acest număr\"\n- **Link-uri broken:** Păstrează textul dar menționează \"(link indisponibil)\"\n\n## NOTE FINALE\n\n- **Ton:** Informativ, direct, fără filler\n- **Claritate:** Eva (organizatoarea) vrea ca oamenii să știe rapid ce trebuie să facă și până când\n- **Acțiune:** Fiecare item trebuie să răspundă la \"Ce trebuie să fac?\" sau \"Când e deadline-ul?\"\n- **WhatsApp compatibility:** Plain text, line breaks clare, fără formatări fancy\n\n## COMENZI RAPIDE\n\n**Pentru a rula:**\n1. \"Extrage și rezumă newsletter #13 cercetași\"\n2. \"Caută ultimul newsletter cercetași și fă rezumat\"\n\n**Pentru update:**\n1. \"Verifică dacă a apărut newsletter nou cercetași (>13)\"\n", + "allowed_tools": [ + "WebFetch" + ], + "enabled": true, + "last_run": "2026-04-02T18:18:07.775703+00:00", + "last_status": "ok", + "next_run": "2027-01-01T00:00:00+00:00" + } +] diff --git a/cron/newsletter-cercetasi-state.json b/cron/newsletter-cercetasi-state.json index d519bc2..0c82c80 100644 --- a/cron/newsletter-cercetasi-state.json +++ b/cron/newsletter-cercetasi-state.json @@ -1,4 +1,5 @@ { - "last_sent": 12, - "year": 2026 + "last_sent": 13, + "year": 2026, + "last_sent_at": "2026-04-02T18:59:37.878273+00:00" } diff --git a/src/adapters/discord_bot.py b/src/adapters/discord_bot.py index dc4bee7..2539179 100644 --- a/src/adapters/discord_bot.py +++ b/src/adapters/discord_bot.py @@ -936,8 +936,8 @@ def create_bot(config: Config) -> discord.Client: @client.event async def on_message(message: discord.Message) -> None: - # Ignore bot's own messages - if message.author == client.user: + # Ignore messages from any bot (including self) + if message.author.bot: return # DM handling: only process if sender is admin diff --git a/src/main.py b/src/main.py index a435e7c..8c786f4 100644 --- a/src/main.py +++ b/src/main.py @@ -74,9 +74,11 @@ def main(): if channel is None: logger.warning("Cron: channel %s not found in Discord cache", channel_id) return + logger.info("Cron: sending %d chars to channel %s (%s)", len(text), channel_alias, channel_id) chunks = split_message(text) for chunk in chunks: await channel.send(chunk) + logger.info("Cron: sent successfully to %s", channel_alias) scheduler = Scheduler(send_callback=_send_to_channel, config=config) client.scheduler = scheduler # type: ignore[attr-defined] diff --git a/src/newsletter_cercetasi.py b/src/newsletter_cercetasi.py index 62ca1f1..0729f02 100644 --- a/src/newsletter_cercetasi.py +++ b/src/newsletter_cercetasi.py @@ -19,7 +19,7 @@ STATE_FILE = PROJECT_ROOT / "cron" / "newsletter-cercetasi-state.json" KB_PROMPT_FILE = ( PROJECT_ROOT / "memory" / "kb" / "projects" / "grup-sprijin" / "prompt-newsletter-cercetasi.md" ) -CLAUDE_TIMEOUT = 120 +CLAUDE_TIMEOUT = 300 def _read_state() -> dict: diff --git a/src/scheduler.py b/src/scheduler.py index e66e185..9c03bbd 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -329,7 +329,9 @@ class Scheduler: self._save_jobs() # Send output via callback - if self._send_callback and result_text: + if not result_text: + logger.warning("Job '%s' produced empty result, skipping send", name) + elif self._send_callback: try: await self._send_callback(job["channel"], result_text) except Exception as exc: diff --git a/tools/check_newsletter_cercetasi.py b/tools/check_newsletter_cercetasi.py new file mode 100755 index 0000000..d66c02f --- /dev/null +++ b/tools/check_newsletter_cercetasi.py @@ -0,0 +1,279 @@ +#!/usr/bin/env python3 +""" +Standalone cron script: check for new Cercetași newsletter and send summary to Discord. + +Usage: python3 /home/moltbot/echo-core/tools/check_newsletter_cercetasi.py +Crontab: 0 17 * * 4,5,1 /home/moltbot/echo-core/.venv/bin/python3 /home/moltbot/echo-core/tools/check_newsletter_cercetasi.py >> /home/moltbot/echo-core/logs/newsletter-cercetasi.log 2>&1 +""" + +import json +import subprocess +import sys +import urllib.request +from datetime import datetime, timezone +from pathlib import Path + +import httpx + +PROJECT_ROOT = Path(__file__).resolve().parent.parent + +STATE_FILE = PROJECT_ROOT / "cron" / "newsletter-cercetasi-state.json" +KB_PROMPT_FILE = PROJECT_ROOT / "memory" / "kb" / "projects" / "grup-sprijin" / "prompt-newsletter-cercetasi.md" +CONFIG_FILE = PROJECT_ROOT / "config.json" +CLAUDE_BIN = "claude" +CLAUDE_TIMEOUT = 300 + +NEWSLETTER_BASE_URL = "https://cercetaiis-newsletter.beehiiv.com/p/newsletter-{n}-din-{year}" + + +def log(msg): + print(f"[{datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S')} UTC] {msg}", flush=True) + + +def read_state() -> dict: + try: + return json.loads(STATE_FILE.read_text()) + except (FileNotFoundError, json.JSONDecodeError): + return {"last_sent": 0, "year": datetime.now(timezone.utc).year} + + +def write_state(state: dict): + STATE_FILE.write_text(json.dumps(state, indent=2, ensure_ascii=False) + "\n") + + +def newsletter_exists(n: int, year: int) -> bool: + url = NEWSLETTER_BASE_URL.format(n=n, year=year) + try: + req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) + with urllib.request.urlopen(req, timeout=10) as resp: + return resp.status == 200 + except urllib.error.HTTPError as e: + if e.code == 404: + return False + log(f"HTTP check failed: {e}") + return False + except Exception as e: + log(f"HTTP check failed: {e}") + return False + + +def generate_summary(n: int, year: int) -> str | None: + url = NEWSLETTER_BASE_URL.format(n=n, year=year) + try: + kb_prompt = KB_PROMPT_FILE.read_text() + except FileNotFoundError: + log(f"KB prompt file not found: {KB_PROMPT_FILE}") + return None + + prompt = ( + f"Newsletter-ul Cercetașilor #{n}/{year} este disponibil la: {url}\n\n" + f"Urmează instrucțiunile de mai jos pentru a genera rezumatul:\n\n" + f"{kb_prompt}" + ) + + # Strip Claude Code env vars to allow nested execution + import os + env = {k: v for k, v in os.environ.items() + if k not in {"CLAUDECODE", "CLAUDE_CODE_SSE_PORT", "CLAUDE_CODE_ENTRYPOINT", + "CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS"}} + + cmd = [ + CLAUDE_BIN, "-p", prompt, + "--model", "sonnet", + "--output-format", "json", + "--allowedTools", "WebFetch", + ] + + try: + proc = subprocess.run( + cmd, + capture_output=True, + text=True, + timeout=CLAUDE_TIMEOUT, + env=env, + cwd=PROJECT_ROOT, + ) + if proc.returncode != 0: + log(f"Claude CLI error (exit {proc.returncode}): {proc.stderr[:300]}") + return None + data = json.loads(proc.stdout) + return data.get("result", "").strip() or None + except subprocess.TimeoutExpired: + log(f"Claude CLI timed out after {CLAUDE_TIMEOUT}s") + return None + except Exception as e: + log(f"Failed to generate summary: {e}") + return None + + +def send_discord(channel_id: str, token: str, text: str) -> bool: + limit = 2000 + chunks = [] + while text: + if len(text) <= limit: + chunks.append(text) + break + split_at = text.rfind("\n", 0, limit) + if split_at == -1: + split_at = limit + chunks.append(text[:split_at]) + text = text[split_at:].lstrip("\n") + + try: + with httpx.Client(timeout=15) as client: + for chunk in chunks: + resp = client.post( + f"https://discord.com/api/v10/channels/{channel_id}/messages", + headers={"Authorization": f"Bot {token}"}, + json={"content": chunk}, + ) + if resp.status_code not in (200, 201): + log(f"Discord send failed: {resp.status_code} {resp.text[:200]}") + return False + except Exception as e: + log(f"Discord send error: {e}") + return False + return True + + +def get_discord_token() -> str | None: + """Read Discord token from keyring.""" + try: + import keyring + return keyring.get_password("echo-core", "discord_token") + except Exception as e: + log(f"Keyring error: {e}") + return None + + +def get_discord_channel_id() -> str | None: + try: + config = json.loads(CONFIG_FILE.read_text()) + return config.get("channels", {}).get("echo-core", {}).get("id") + except Exception as e: + log(f"Config read error: {e}") + return None + + +def get_telegram_token() -> str | None: + try: + import keyring + return keyring.get_password("echo-core", "telegram_token") + except Exception as e: + log(f"Keyring error (telegram): {e}") + return None + + +def send_telegram(bot_token: str, chat_id: str, text: str) -> bool: + limit = 4096 + chunks = [] + while text: + if len(text) <= limit: + chunks.append(text) + break + split_at = text.rfind("\n", 0, limit) + if split_at == -1: + split_at = limit + chunks.append(text[:split_at]) + text = text[split_at:].lstrip("\n") + + try: + with httpx.Client(timeout=15) as client: + for chunk in chunks: + resp = client.post( + f"https://api.telegram.org/bot{bot_token}/sendMessage", + json={"chat_id": chat_id, "text": chunk}, + ) + if resp.status_code != 200: + log(f"Telegram send failed: {resp.status_code} {resp.text[:200]}") + return False + except Exception as e: + log(f"Telegram send error: {e}") + return False + return True + + +def main(): + state = read_state() + current_year = datetime.now(timezone.utc).year + + # New year → reset counter + if state.get("year", current_year) != current_year: + log(f"New year detected ({state['year']} → {current_year}), resetting counter") + state = {"last_sent": 0, "year": current_year} + + next_n = state["last_sent"] + 1 + log(f"Checking for newsletter #{next_n}/{current_year}...") + + if not newsletter_exists(next_n, current_year): + log(f"Newsletter #{next_n}/{current_year} not yet available. Exiting.") + return + + log(f"Newsletter #{next_n}/{current_year} found! Generating summary...") + summary = generate_summary(next_n, current_year) + if not summary: + log("Summary generation failed. Exiting.") + sys.exit(1) + + success = False + + # Send to Discord + discord_token = get_discord_token() + channel_id = get_discord_channel_id() + if discord_token and channel_id: + log(f"Sending {len(summary)} chars to Discord channel {channel_id}...") + if send_discord(channel_id, discord_token, summary): + log(f"Discord: sent successfully.") + success = True + else: + log("Discord send failed.") + else: + log("Discord token or channel ID missing, skipping.") + + # Send to Telegram + telegram_token = get_telegram_token() + if telegram_token: + config = json.loads(CONFIG_FILE.read_text()) + telegram_chat_id = config.get("newsletter_cercetasi", {}).get("telegram_chat_id", "5040014994") + log(f"Sending {len(summary)} chars to Telegram chat {telegram_chat_id}...") + if send_telegram(telegram_token, telegram_chat_id, summary): + log(f"Telegram: sent successfully.") + success = True + else: + log("Telegram send failed.") + else: + log("Telegram token missing, skipping.") + + # Send to WhatsApp + config = json.loads(CONFIG_FILE.read_text()) + bridge_url = config.get("whatsapp", {}).get("bridge_url", "http://127.0.0.1:8098") + owner_phone = config.get("whatsapp", {}).get("owner", "") + if owner_phone: + wa_to = f"{owner_phone}@s.whatsapp.net" + log(f"Sending {len(summary)} chars to WhatsApp {owner_phone}...") + try: + with httpx.Client(timeout=15) as client: + resp = client.post(f"{bridge_url}/send", json={"to": wa_to, "text": summary}) + if resp.status_code == 200 and resp.json().get("ok"): + log("WhatsApp: sent successfully.") + success = True + else: + log(f"WhatsApp send failed: {resp.text[:200]}") + except Exception as e: + log(f"WhatsApp send error: {e}") + else: + log("WhatsApp owner not configured, skipping.") + + if success: + state["last_sent"] = next_n + state["year"] = current_year + state["last_sent_at"] = datetime.now(timezone.utc).isoformat() + write_state(state) + log(f"Newsletter #{next_n}/{current_year} done.") + else: + log("All sends failed — will retry next run.") + sys.exit(1) + + +if __name__ == "__main__": + main()