Files
Siro/backend/marketing_engine/schedule_manager.php
T

70 lines
2.8 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')
");
return $stmt->execute([
$accountId,
$platform,
$type,
$targetUrl,
$promptContext,
$generatedComment,
$scheduledAt
]);
}
}
// 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";
}