chore: auto-commit from dashboard

This commit is contained in:
2026-04-02 19:43:01 +00:00
parent d9450ce70d
commit 5fafc29dc1
9 changed files with 383 additions and 20 deletions

View File

@@ -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' });

View File

@@ -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"

File diff suppressed because one or more lines are too long

View File

@@ -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"
}

View File

@@ -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

View File

@@ -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]

View File

@@ -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:

View File

@@ -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:

View File

@@ -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()