Update: 2026-07-05 00:06:55

This commit is contained in:
Hamza-Ayed
2026-07-05 00:06:55 +03:00
parent a0e9bd3cbb
commit 35c0a6680c
10 changed files with 300 additions and 0 deletions
@@ -0,0 +1,77 @@
<?php
// ============================================================
// marketing_engine/cron_weekly_report.php
// Script to be run via cron (e.g. weekly) to summarize all reports
// ============================================================
require_once __DIR__ . '/../core/bootstrap.php';
require_once __DIR__ . '/../functions.php';
require_once __DIR__ . '/../core/Services/SiroGeminiService.php';
require_once __DIR__ . '/../core/Services/FcmService.php';
try {
$con = Database::get('main');
// Fetch all reports from the last 7 days (that are not weekly themselves)
$stmt = $con->prepare("
SELECT platform, report_html, created_at
FROM marketing_reports
WHERE is_weekly = 0
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
ORDER BY created_at ASC
");
$stmt->execute();
$reports = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($reports)) {
echo "No reports generated in the last 7 days to summarize.\n";
exit;
}
// Combine all report data for Gemini
$combinedData = [];
foreach ($reports as $report) {
$combinedData[] = [
'platform' => $report['platform'],
'date' => $report['created_at'],
'content' => strip_tags($report['report_html']) // Send stripped HTML to save tokens
];
}
$gemini = new SiroGeminiService();
// Create a new method in GeminiService or use evaluatePostsForReporting with a specific prompt wrapper
$prompt = "You are a Chief Marketing Officer AI. Below are all the daily/individual social media intelligence reports collected over the last 7 days from our scraper bots. Please read through all of them and generate one comprehensive, executive 'Weekly Market Intelligence Summary'. Format it beautifully in HTML. Highlight key market trends, recurrent complaints, competitor activities, and actionable advice for the week.\n\nData:\n" . json_encode($combinedData);
$response = $gemini->callGemini($prompt, 'gemini-1.5-flash');
if (isset($response['candidates'][0]['content']['parts'][0]['text'])) {
$weeklyHtml = $response['candidates'][0]['content']['parts'][0]['text'];
// Save the weekly report
$insert = $con->prepare("INSERT INTO marketing_reports (platform, report_html, is_weekly) VALUES ('all', ?, 1)");
$insert->execute([$weeklyHtml]);
$reportId = $con->lastInsertId();
// Notify Admins
$fcm = new FcmService();
$fcm->send(
token: '/topics/admin_alerts',
title: 'Weekly Market Intelligence Summary 📈',
body: 'The comprehensive weekly social media report has been generated.',
data: [
'type' => 'marketing_report',
'report_id' => $reportId,
'is_weekly' => 1
],
category: 'marketing_report'
);
echo "Weekly report generated and saved successfully. ID: $reportId\n";
} else {
echo "Failed to generate report from Gemini.\n";
}
} catch (Exception $e) {
echo "Error generating weekly report: " . $e->getMessage() . "\n";
}
+28
View File
@@ -7,6 +7,7 @@
require_once __DIR__ . '/../core/bootstrap.php';
require_once __DIR__ . '/../functions.php';
require_once __DIR__ . '/../core/Services/SiroGeminiService.php';
require_once __DIR__ . '/../core/Services/FcmService.php';
header('Content-Type: application/json');
@@ -87,11 +88,38 @@ try {
// Save to database
$stmt = $con->prepare("INSERT INTO marketing_reports (platform, report_html) VALUES (?, ?)");
$stmt->execute([$platform, $reportHtml]);
$reportId = $con->lastInsertId();
// Send FCM notification to admins
$fcm = new FcmService();
$fcm->send(
token: '/topics/admin_alerts',
title: 'New Social Media Intelligence Report 📊',
body: "A new autonomous analysis report for {$platform} has been generated.",
data: [
'type' => 'marketing_report',
'report_id' => $reportId,
'platform' => $platform
],
category: 'marketing_report'
);
}
echo json_encode(['status' => 'success', 'message' => 'Posts evaluated and report saved']);
break;
case 'get_reports':
$stmt = $con->prepare("
SELECT id, platform, report_html, is_weekly, created_at
FROM marketing_reports
ORDER BY created_at DESC
LIMIT 50
");
$stmt->execute();
$reports = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['status' => 'success', 'data' => $reports]);
break;
default:
echo json_encode(['status' => 'error', 'message' => 'Invalid action']);
}
+8
View File
@@ -68,3 +68,11 @@ INSERT IGNORE INTO `api_quotas` (`service_name`, `daily_usage`, `quota_limit`, `
('gemini', 0, 100, CURDATE()),
('elevenlabs', 0, 5, CURDATE()),
('creatomate', 0, 2, CURDATE());
CREATE TABLE IF NOT EXISTS marketing_reports (
id INT AUTO_INCREMENT PRIMARY KEY,
platform ENUM('facebook', 'instagram', 'tiktok', 'twitter', 'youtube', 'all') NOT NULL,
report_html TEXT NOT NULL,
is_weekly TINYINT(1) DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);