con = Database::get('main'); } /** * Create a new task and optionally generate a comment for it using Gemini. * * @param string $platform 'facebook' or 'instagram' * @param string $type 'join_group', 'read_posts', 'post_comment', 'share_link' * @param string|null $targetUrl The group or post URL * @param string|null $promptContext Context for Gemini * @param int|null $accountId Specific account ID or null for any available * @param int $delayMinutes How many minutes to delay this task */ public function scheduleTask($platform, $type, $targetUrl = null, $promptContext = null, $accountId = null, $delayMinutes = 0) { // Quiet hours check (e.g. don't schedule tasks between 1 AM and 6 AM) $currentHour = (int)date('H'); if ($currentHour >= 1 && $currentHour < 6) { // Push it to 6 AM if we are in quiet hours $hoursToAdd = 6 - $currentHour; $delayMinutes += ($hoursToAdd * 60); } $scheduledAt = date('Y-m-d H:i:s', strtotime("+$delayMinutes minutes")); $generatedComment = null; if (($type === 'post_comment' || $type === 'share_link') && $promptContext) { // Pre-generate the comment using Gemini // If it's share_link, we want an intent to promote, otherwise just answer/interact $intent = ($type === 'share_link') ? 'promote_siro' : 'answer_question'; $generatedComment = generateCommentWithGemini($promptContext, $intent); } $stmt = $this->con->prepare(" INSERT INTO social_tasks (account_id, platform, type, target_url, prompt_context, generated_comment, scheduled_at, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending') "); $success = $stmt->execute([ $accountId, $platform, $type, $targetUrl, $promptContext, $generatedComment, $scheduledAt ]); if ($success && ($type === 'share_link' || $type === 'post_comment')) { // Retreive an active account to exclude as parent $parentAccountId = $accountId; if (!$parentAccountId) { $stmtAcc = $this->con->prepare("SELECT id FROM social_accounts WHERE platform = ? AND status = 'active' LIMIT 1"); $stmtAcc->execute([$platform]); $parentAccountId = $stmtAcc->fetchColumn() ?: 0; } $this->scheduleDialogueDrama($platform, $targetUrl, $promptContext, $parentAccountId, $delayMinutes); } return $success; } /** * Schedule supporting dialogue tasks from other accounts to create a buzz/drama. */ public function scheduleDialogueDrama($platform, $targetUrl, $promptContext, $parentAccountId, $baseDelayMinutes) { // Fetch 2 other active accounts on the same platform $stmt = $this->con->prepare(" SELECT id FROM social_accounts WHERE platform = ? AND status = 'active' AND id != ? ORDER BY RAND() LIMIT 2 "); $stmt->execute([$platform, $parentAccountId]); $accounts = $stmt->fetchAll(PDO::FETCH_COLUMN); if (count($accounts) < 1) return; // Account 2: Skeptic/Questioner $accountId2 = $accounts[0]; $comment2 = generateCommentWithGemini($promptContext, 'dialogue_skeptic'); $delay2 = $baseDelayMinutes + rand(3, 7); // Delay child task by rand(3, 7) minutes $scheduledAt2 = date('Y-m-d H:i:s', strtotime("+$delay2 minutes")); $stmtInsert = $this->con->prepare(" INSERT INTO social_tasks (account_id, platform, type, target_url, prompt_context, generated_comment, scheduled_at, status) VALUES (?, ?, 'post_comment', ?, ?, ?, ?, 'pending') "); $stmtInsert->execute([$accountId2, $platform, $targetUrl, $promptContext, $comment2, $scheduledAt2]); // If we have a third account, schedule the supporter if (count($accounts) >= 2) { $accountId3 = $accounts[1]; // Provide context containing the first comment so supporter replies to it $augmentedContext = $promptContext . "\n[PREVIOUS_REPLY]: " . $comment2; $comment3 = generateCommentWithGemini($augmentedContext, 'dialogue_supporter'); $delay3 = $delay2 + rand(4, 9); // Supporter replies after the skeptic $scheduledAt3 = date('Y-m-d H:i:s', strtotime("+$delay3 minutes")); $stmtInsert->execute([$accountId3, $platform, $targetUrl, $promptContext, $comment3, $scheduledAt3]); } } } // Example usage via CLI if (php_sapi_name() === 'cli' && isset($argv[1]) && $argv[1] === 'test_schedule') { $sm = new ScheduleManager(); $context = "شو رأيكم بتطبيق سيرو يا كباتن؟ حد جربه؟"; $success = $sm->scheduleTask('facebook', 'post_comment', 'https://facebook.com/groups/amman.drivers/post/12345', $context, null, 5); echo $success ? "Task scheduled successfully!\n" : "Failed to schedule task.\n"; }