103 lines
3.8 KiB
PHP
103 lines
3.8 KiB
PHP
<?php
|
|
// ============================================================
|
|
// marketing_engine/index.php
|
|
// Main API endpoint for the Marketing Microservice
|
|
// ============================================================
|
|
|
|
require_once __DIR__ . '/../core/bootstrap.php';
|
|
require_once __DIR__ . '/../functions.php';
|
|
require_once __DIR__ . '/../core/Services/SiroGeminiService.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Simple bot authentication
|
|
$headers = getallheaders();
|
|
$botToken = $headers['X-Bot-Token'] ?? '';
|
|
// if ($botToken !== 'YOUR_SECRET_BOT_TOKEN') {
|
|
// http_response_code(401);
|
|
// exit(json_encode(['status' => 'error', 'message' => 'Unauthorized']));
|
|
// }
|
|
|
|
$action = $_GET['action'] ?? '';
|
|
$platform = $_GET['platform'] ?? 'facebook';
|
|
|
|
try {
|
|
$con = Database::get('main');
|
|
|
|
switch ($action) {
|
|
case 'get_task':
|
|
$stmt = $con->prepare("
|
|
SELECT id, type
|
|
FROM marketing_tasks
|
|
WHERE status = 'pending' AND platform = ?
|
|
ORDER BY id ASC LIMIT 1
|
|
");
|
|
$stmt->execute([$platform]);
|
|
$task = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($task) {
|
|
$update = $con->prepare("UPDATE marketing_tasks SET status = 'in_progress' WHERE id = ?");
|
|
$update->execute([$task['id']]);
|
|
|
|
echo json_encode(['status' => 'success', 'data' => $task]);
|
|
} else {
|
|
echo json_encode(['status' => 'success', 'message' => 'No tasks available', 'data' => null]);
|
|
}
|
|
break;
|
|
|
|
case 'complete_task':
|
|
$taskId = $_POST['task_id'] ?? null;
|
|
$result = $_POST['result'] ?? '';
|
|
|
|
if ($taskId) {
|
|
$stmt = $con->prepare("UPDATE marketing_tasks SET status = 'completed', completed_at = NOW() WHERE id = ?");
|
|
$stmt->execute([$taskId]);
|
|
echo json_encode(['status' => 'success']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'task_id required']);
|
|
}
|
|
break;
|
|
|
|
case 'fail_task':
|
|
$taskId = $_POST['task_id'] ?? null;
|
|
$errorMsg = $_POST['error_message'] ?? 'Unknown error';
|
|
|
|
if ($taskId) {
|
|
$stmt = $con->prepare("UPDATE marketing_tasks SET status = 'failed', error_message = ? WHERE id = ?");
|
|
$stmt->execute([$errorMsg, $taskId]);
|
|
echo json_encode(['status' => 'success']);
|
|
} else {
|
|
echo json_encode(['status' => 'error', 'message' => 'task_id required']);
|
|
}
|
|
break;
|
|
|
|
case 'evaluate_posts':
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$posts = $input['posts'] ?? [];
|
|
|
|
if (empty($posts)) {
|
|
echo json_encode(['status' => 'success', 'data' => null]);
|
|
break;
|
|
}
|
|
|
|
$gemini = new SiroGeminiService();
|
|
$reportHtml = $gemini->evaluatePostsForReporting($posts);
|
|
|
|
if ($reportHtml) {
|
|
// Save to database
|
|
$stmt = $con->prepare("INSERT INTO marketing_reports (platform, report_html) VALUES (?, ?)");
|
|
$stmt->execute([$platform, $reportHtml]);
|
|
}
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'Posts evaluated and report saved']);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => 'Server error: ' . $e->getMessage()]);
|
|
}
|