153 lines
6.5 KiB
PHP
153 lines
6.5 KiB
PHP
<?php
|
|
// ============================================================
|
|
// social_worker.php
|
|
// Endpoint for the Android Social Media Bot
|
|
// ============================================================
|
|
|
|
require_once __DIR__ . '/../../core/bootstrap.php';
|
|
require_once __DIR__ . '/../../functions.php';
|
|
|
|
// Authentication and validation could be added here similar to driver_socket.php
|
|
// For now, let's keep it simple or use a static token for the bot
|
|
$headers = getallheaders();
|
|
$botToken = $headers['X-Bot-Token'] ?? '';
|
|
if ($botToken !== 'YOUR_SECRET_BOT_TOKEN') {
|
|
// In production, use a secure token or JWT
|
|
// http_response_code(401);
|
|
// exit(json_encode(['status' => 'error', 'message' => 'Unauthorized']));
|
|
}
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
|
|
try {
|
|
$con = Database::get('main');
|
|
|
|
switch ($action) {
|
|
case 'get_task':
|
|
// The bot asks for a task to do
|
|
$platform = $_GET['platform'] ?? 'facebook';
|
|
|
|
// Find a pending task that is scheduled for now or earlier
|
|
$stmt = $con->prepare("
|
|
SELECT id, account_id, type, target_url, prompt_context, generated_comment
|
|
FROM social_tasks
|
|
WHERE status = 'pending' AND platform = ? AND (scheduled_at IS NULL OR scheduled_at <= NOW())
|
|
ORDER BY created_at ASC LIMIT 1
|
|
");
|
|
$stmt->execute([$platform]);
|
|
$task = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($task) {
|
|
require_once __DIR__ . '/account_manager.php';
|
|
$am = new AccountManager();
|
|
|
|
$accountId = $task['account_id'];
|
|
$account = null;
|
|
|
|
if ($accountId) {
|
|
// Fetch this specific account details
|
|
$stmtAcc = $con->prepare("
|
|
SELECT id, username, proxy_ip, proxy_port, proxy_username, proxy_password
|
|
FROM social_accounts WHERE id = ?
|
|
");
|
|
$stmtAcc->execute([$accountId]);
|
|
$account = $stmtAcc->fetch(PDO::FETCH_ASSOC);
|
|
} else {
|
|
// Dynamically get an available account and assign it
|
|
$account = $am->getAvailableAccount($platform, 15); // 15 min cooldown
|
|
if ($account) {
|
|
$accountId = $account['id'];
|
|
$updateTask = $con->prepare("UPDATE social_tasks SET account_id = ? WHERE id = ?");
|
|
$updateTask->execute([$accountId, $task['id']]);
|
|
$task['account_id'] = $accountId;
|
|
}
|
|
}
|
|
|
|
if (!$account) {
|
|
echo json_encode(['status' => 'error', 'message' => 'No active social account available or in cooldown']);
|
|
break;
|
|
}
|
|
|
|
if ($account) {
|
|
$task['bot_username'] = $account['username'];
|
|
$task['proxy_ip'] = $account['proxy_ip'];
|
|
$task['proxy_port'] = $account['proxy_port'];
|
|
$task['proxy_username'] = $account['proxy_username'];
|
|
$task['proxy_password'] = $account['proxy_password'];
|
|
}
|
|
|
|
// Mark as in progress and update last active
|
|
$update = $con->prepare("UPDATE social_tasks SET status = 'in_progress' WHERE id = ?");
|
|
$update->execute([$task['id']]);
|
|
|
|
$am->markAccountActive($accountId);
|
|
|
|
// Return task directly in 'data' to preserve compatibility with existing Android bots
|
|
echo json_encode([
|
|
'status' => 'success',
|
|
'data' => $task
|
|
]);
|
|
} else {
|
|
echo json_encode(['status' => 'success', 'message' => 'No tasks available', 'data' => null]);
|
|
}
|
|
break;
|
|
|
|
case 'complete_task':
|
|
// The bot reports task completion
|
|
$taskId = $_POST['task_id'] ?? null;
|
|
$result = $_POST['result'] ?? ''; // e.g. success or error details
|
|
|
|
if ($taskId) {
|
|
$stmt = $con->prepare("UPDATE social_tasks SET status = 'completed', completed_at = NOW() WHERE id = ?");
|
|
$stmt->execute([$taskId]);
|
|
|
|
// Log it
|
|
$log = $con->prepare("INSERT INTO social_logs (task_id, log_level, message) VALUES (?, 'info', ?)");
|
|
$log->execute([$taskId, "Task completed: " . substr($result, 0, 200)]);
|
|
|
|
echo json_encode(['status' => 'success']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'task_id required']);
|
|
}
|
|
break;
|
|
|
|
case 'fail_task':
|
|
// The bot reports task failure
|
|
$taskId = $_POST['task_id'] ?? null;
|
|
$errorMsg = $_POST['error_message'] ?? 'Unknown error';
|
|
|
|
if ($taskId) {
|
|
$stmt = $con->prepare("UPDATE social_tasks SET status = 'failed', error_message = ? WHERE id = ?");
|
|
$stmt->execute([$errorMsg, $taskId]);
|
|
|
|
// Log it
|
|
$log = $con->prepare("INSERT INTO social_logs (task_id, log_level, message) VALUES (?, 'error', ?)");
|
|
$log->execute([$taskId, "Task failed: " . substr($errorMsg, 0, 200)]);
|
|
|
|
echo json_encode(['status' => 'success']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'task_id required']);
|
|
}
|
|
break;
|
|
|
|
case 'log':
|
|
// The bot sends a general log
|
|
$accountId = $_POST['account_id'] ?? null;
|
|
$level = $_POST['level'] ?? 'info';
|
|
$message = $_POST['message'] ?? '';
|
|
|
|
$log = $con->prepare("INSERT INTO social_logs (account_id, log_level, message) VALUES (?, ?, ?)");
|
|
$log->execute([$accountId, $level, $message]);
|
|
|
|
echo json_encode(['status' => 'success']);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
|
|
}
|