diff --git a/telegram-bot/bot.err b/telegram-bot/bot.err index 187fb0e..e69de29 100644 --- a/telegram-bot/bot.err +++ b/telegram-bot/bot.err @@ -1 +0,0 @@ -❌ Poll error: Network request for 'getUpdates' failed! diff --git a/telegram-bot/bot.log b/telegram-bot/bot.log index 9df8869..273f1dd 100644 --- a/telegram-bot/bot.log +++ b/telegram-bot/bot.log @@ -1,44 +1,2 @@ -🚀 Bot starting... -✅ Bot is live — polling started (model: claude-sonnet-4-6) -🤖 [775071081] Calling Pi (continue session)... -📤 [775071081] 263 chars in 4.2s -🤖 [775071081] Calling Pi (continue session)... -🔧 [775071081] cat /private/tmp/.env 2>/dev/null || cat ~/.env 2>/dev/null || echo "NO ENV FOUN... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -echo '=== UPTIME ===' && upti... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -echo '=== HAPROXY ===' && ser... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -echo '=== HAPROXY PROCESS ===... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 "cat ~/.env 2>/dev/null || ls /... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -echo '=== HIGH LOAD CAUSE ===... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 "grep -r 'ASANA' /root/ /opt/ -... -🔧 [775071081] find /private/tmp -name "*.env" 2>/dev/null; find ~ -maxdepth 3 -name ".env" 2>/... -🔧 [775071081] grep -i 'ASANA' /Users/azreenjamal/pi-vs-claude-code/.env /Users/azreenjamal/pi-... -🔧 [775071081] grep -i 'ASANA' /Users/azreenjamal/pi-vs-claude-code/.env -🔧 [775071081] grep -i 'ASANA\|WORKSPACE\|PROJECT' /Users/azreenjamal/pi-vs-claude-code/.env -🔧 [775071081] # Get workspace and project info from Asana -curl -s -H "Authorization: Bearer 2/... -🔧 [775071081] # Get projects in workspace -curl -s -H "Authorization: Bearer 2/1212300623824347... -🔧 [775071081] # Task 1: qc-server-new FROZEN -curl -s -X POST "https://app.asana.com/api/1.0/ta... -🔧 [775071081] # Task 2: High CPU load + rsync snapshot -curl -s -X POST "https://app.asana.com/... -📤 [775071081] 1026 chars in 190.5s -🤖 [775071081] Calling Pi (continue session)... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 "incus unfreeze qc-server-new &... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -incus list qc-server-new --fo... -🔧 [775071081] curl -s -X PUT "https://app.asana.com/api/1.0/tasks/1213564390289259" \ - -H "Au... -📤 [775071081] 221 chars in 33.6s -🤖 [775071081] Calling Pi (continue session)... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -echo '=== TOP SWAP USERS ==='... -🔧 [775071081] ssh -o ConnectTimeout=5 -p 22 root@159.195.60.33 " -cat /proc/meminfo | grep -i s... -📤 [775071081] 531 chars in 32.0s -🤖 [775071081] Calling Pi (continue session)... -📤 [775071081] 493 chars in 9.1s +🤖 CharityRight DevOps Bot starting... +✅ Bot online — polling for messages diff --git a/telegram-bot/index.ts b/telegram-bot/index.ts index b3e2304..f528da3 100644 --- a/telegram-bot/index.ts +++ b/telegram-bot/index.ts @@ -506,26 +506,66 @@ bot.on("message:text", async (ctx) => { // ─── Error handler ─── bot.catch((err) => { - console.error("Bot error:", err.message || err); + const msg = err.message || String(err); + // 409 is expected during restart — ignore it + if (msg.includes("409") || msg.includes("Conflict")) { + console.log("⚠️ Telegram 409 conflict (normal during restart, retrying...)"); + return; + } + console.error("Bot error:", msg); }); // ─── Startup ─── console.log("🤖 CharityRight DevOps Bot starting..."); -// Clear stale updates -await bot.api.getUpdates({ offset: -1, limit: 1 }).catch(() => {}); +const BOT_API = `https://api.telegram.org/bot${BOT_TOKEN}`; +let pollOffset = 0; +let running = true; -bot.start({ - onStart: () => console.log("✅ Bot online — polling for messages"), - drop_pending_update: true, -}); +async function pollLoop() { + console.log("✅ Bot online — polling for messages"); + + while (running) { + try { + const res = await fetch(`${BOT_API}/getUpdates?offset=${pollOffset}&limit=100&timeout=30`); + const data = (await res.json()) as any; + + if (!data.ok) { + if (data.error_code === 409) { + console.log("⚠️ 409 conflict — waiting 10s..."); + await new Promise((r) => setTimeout(r, 10_000)); + continue; + } + console.error("Poll error:", data.description); + await new Promise((r) => setTimeout(r, 3000)); + continue; + } + + for (const update of data.result || []) { + pollOffset = update.update_id + 1; + try { + await bot.handleUpdate(update); + } catch (e: any) { + console.error("Handler error:", e.message?.substring(0, 200)); + } + } + } catch (e: any) { + console.error("Network error:", e.message?.substring(0, 100)); + await new Promise((r) => setTimeout(r, 3000)); + } + } +} + +await pollLoop(); -// Cleanup on exit const cleanup = () => { console.log("🛑 Shutting down..."); + running = false; for (const [, controller] of activeProcesses) controller.abort(); - bot.stop(); process.exit(0); }; process.on("SIGINT", cleanup); process.on("SIGTERM", cleanup); +process.on("unhandledRejection", (err: any) => { + console.error("Unhandled:", err?.message?.substring(0, 200) || err); +});