














سرویس آرکوپال
https://api.openai.com/v1/chat/completions<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Models\Product;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class AutoProductAI extends Command
{
protected $signature = 'products:ai-auto';
protected $description = 'تولید خودکار محتوا و Excel روزانه برای محصولات جدید';
public function handle()
{
$this->info("شروع پردازش محصولات جدید...");
// گرفتن محصولات جدید که هنوز توضیح ندارند
$products = Product::whereNull('description')->get();
if ($products->isEmpty()) {
$this->info("محصول جدیدی برای پردازش وجود ندارد.");
return;
}
foreach ($products as $product) {
$title = $product->title;
$prompt = "
عنوان محصول: $title
یک توضیح کامل و SEO شده بنویس، شامل:
- توضیح کوتاه
- توضیح بلند
- کلمات کلیدی SEO
- Meta Description
فرمت خروجی JSON بده:
{\"short_description\":\"...\",\"description\":\"...\",\"seo_keywords\":\"...\",\"meta_description\":\"...\"}
";
try {
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . env('OPENAI_API_KEY')
])->post('https://api.openai.com/v1/chat/completions', [
'model' => 'gpt-4.1-mini',
'messages' => [
['role' => 'user', 'content' => $prompt]
],
'temperature' => 0.7
]);
$content = $response->json();
$json_data = json_decode($content['choices'][0]['message']['content'], true);
$product->short_description = $json_data['short_description'] ?? '';
$product->description = $json_data['description'] ?? '';
$product->seo_keywords = $json_data['seo_keywords'] ?? '';
$product->meta_description = $json_data['meta_description'] ?? '';
$product->save();
$this->info("پردازش شد: $title");
} catch (\Exception $e) {
$this->error("خطا برای محصول $title: " . $e->getMessage());
}
}
// ساخت Excel خروجی برای ترب
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Title');
$sheet->setCellValue('B1', 'Short Description');
$sheet->setCellValue('C1', 'Description');
$sheet->setCellValue('D1', 'SEO Keywords');
$sheet->setCellValue('E1', 'Meta Description');
$row = 2;
foreach (Product::all() as $product) {
$sheet->setCellValue("A$row", $product->title);
$sheet->setCellValue("B$row", $product->short_description);
$sheet->setCellValue("C$row", $product->description);
$sheet->setCellValue("D$row", $product->seo_keywords);
$sheet->setCellValue("E$row", $product->meta_description);
$row++;
}
$writer = new Xlsx($spreadsheet);
$excel_file = storage_path('app/public/products.xlsx');
$writer->save($excel_file);
$this->info("Excel خروجی ساخته شد: " . $excel_file);
$this->info("پردازش روزانه تمام شد ✅");
}
}