diff --git a/backend/marketing_engine/social_worker.php b/backend/marketing_engine/social_worker.php index b454ea7b..e61a9131 100644 --- a/backend/marketing_engine/social_worker.php +++ b/backend/marketing_engine/social_worker.php @@ -139,6 +139,65 @@ try { } else { echo json_encode(['status' => 'success', 'message' => 'No tasks available', 'data' => null]); } + case 'process_organic_post': + // The bot found a post organically via Accessibility, copied its link, and needs a comment NOW + $platform = $_POST['platform'] ?? 'facebook'; + $deviceId = $_POST['device_id'] ?? null; + $targetUrl = $_POST['target_url'] ?? null; + $postText = $_POST['post_text'] ?? ''; + + if (!$deviceId || !$targetUrl || !$postText) { + echo json_encode(['status' => 'error', 'message' => 'device_id, target_url, and post_text are required']); + break; + } + + // 1. Resolve Device ID + $stmtDev = $con->prepare("SELECT id FROM social_accounts WHERE device_id = ?"); + $stmtDev->execute([$deviceId]); + $acc = $stmtDev->fetch(PDO::FETCH_ASSOC); + $accountId = $acc ? $acc['id'] : null; + + if (!$accountId) { + // Not registered yet, auto-bind + require_once __DIR__ . '/account_manager.php'; + $am = new AccountManager(); + $stmtFind = $con->prepare("SELECT id FROM social_accounts WHERE platform = ? AND device_id IS NULL AND status = 'active' LIMIT 1"); + $stmtFind->execute([$platform]); + $newAcc = $stmtFind->fetch(PDO::FETCH_ASSOC); + if ($newAcc) { + $accountId = $newAcc['id']; + $updateDev = $con->prepare("UPDATE social_accounts SET device_id = ? WHERE id = ?"); + $updateDev->execute([$deviceId, $accountId]); + } else { + echo json_encode(['status' => 'error', 'message' => 'No accounts available for new device']); + break; + } + } + + // 2. Generate Immediate Comment + require_once __DIR__ . '/gemini_comment_generator.php'; + $generatedComment = generateCommentWithGemini($postText, 'promote_siro'); + + // 3. Save this initial organic action to database as completed + $stmt = $con->prepare(" + INSERT INTO social_tasks (account_id, platform, type, target_url, prompt_context, generated_comment, status, completed_at) + VALUES (?, ?, 'post_comment', ?, ?, ?, 'completed', NOW()) + "); + $stmt->execute([$accountId, $platform, $targetUrl, $postText, $generatedComment]); + + // 4. Schedule Drama (Supporting Accounts) + require_once __DIR__ . '/schedule_manager.php'; + $sm = new ScheduleManager(); + $sm->scheduleDialogueDrama($platform, $targetUrl, $postText, $accountId, 5); // 5 mins delay + + // 5. Return the generated comment immediately to the Android Bot + echo json_encode([ + 'status' => 'success', + 'data' => [ + 'generated_comment' => $generatedComment, + 'target_url' => $targetUrl + ] + ]); break; case 'complete_task':