Files
tripz-llc/backend/marketing_engine/schedule_manager.php
T
Hamza-AyedandClaude Opus 5 4d8414c96b feat: استيراد كود سيرو إلى تريبز (سيرو @ecfe7568) — بلا تعديل
قرار المالك 2026-07-27: باك إند سيرو PHP هو المعتمد، وتطبيقاته المجرّبة
ميدانياً تحل محل إعادة البناء المؤرشفة. سيرو نفسه لم يُمسّ.

الخريطة:
  backend · payment_server · loction_server · ride_server ·
  passenger_server · docker · dashboard · stress_test  → الجذر
  siro_rider  → apps/rider          siro_driver  → apps/driver
  siro_admin  → dashboards/admin    siro_service → dashboards/service
  android_bot → apps/android_bot    socialBot    → apps/socialBot

نُسخ المتعقَّب في git سيرو فقط عبر `git archive` (3,198 ملفاً / ~169 م.ب)
لا `cp -r` — فاستُثنيت مخلفات البناء تلقائياً. بلا أي تعديل محتوى عمداً:
كل ما يلي يصير فرقاً مقروءاً مقابل المصدر.

لم يُستورد وسببه: siromove.com (الموقع التسويقي يبقى marketing/ في تريبز،
سيرو فيه 8 ملفات) · docs و planning (تريبز له docs/ الخاص) · deploy.sh
(ليس نشراً على سيرفر بل `git add . && git push origin --all` — فخّ في
مستودع آخر) · transit_dashboard (بانتظار قرار مصير backend-transit و
dashboards/transit-web).

⚠️ لا يبني بعد — ثلاثة نواقص متوقعة ومقصودة:
1. `.env` و `lib/env/env.g.dart` غير متعقَّبين في سيرو (أسرار لكل مستأجر):
   كل تطبيق فلاتر يحتاج .env خاصاً ثم توليد env.g.dart بـ build_runner.
2. إعدادات Firebase (9 ملفات google-services.json و GoogleService-Info.plist)
   يستبعدها .gitignore تريبز — ولكل مستأجر مشروع Firebase خاص أصلاً.
3. apps/driver في سيرو يشير إلى `../../Intaleq/packages/get` خارج المستودع →
   يجب ضمّ الحزم داخله أسوة بـ apps/rider.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 05:14:13 +03:00

124 lines
5.4 KiB
PHP

<?php
// ============================================================
// schedule_manager.php
// Creates and schedules tasks for the bots
// ============================================================
require_once __DIR__ . '/../core/bootstrap.php';
require_once __DIR__ . '/gemini_comment_generator.php';
class ScheduleManager {
private $con;
public function __construct() {
$this->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";
}