121 lines
4.8 KiB
PHP
121 lines
4.8 KiB
PHP
<?php
|
|
// Intent send_kaouther — génère drafts Gmail via URL + push Blade task
|
|
// Doctrine 64 exception #3 (envoi commercial) : zero-click en théorie mais exception tolérée
|
|
|
|
if (!function_exists('wevia_kaouther_send')) {
|
|
function wevia_kaouther_send($msg) {
|
|
if (!$msg) return false;
|
|
|
|
// Match: "send kaouther", "envoie email kaouther", "kaouther auto", "contre offre kaouther"
|
|
if (!preg_match('/\b(send|envoi|envoye|push|contact|contre[\s-]?offre).{0,15}(kaouther|ethica|najar)/i', $msg)
|
|
&& !preg_match('/\bkaouther.{0,15}(email|send|envoi|mail|auto)/i', $msg)) return false;
|
|
|
|
$out = ['intent' => 'send_kaouther', 'tool' => 'kaouther_gmail_drafts'];
|
|
|
|
$to = 'kaouther.najar@groupe-ethica.com';
|
|
$from = 'ymahboub@weval-consulting.com';
|
|
|
|
// 3 emails paliers
|
|
// V48 LIVE FETCH — doctrine root cause (zero hardcode)
|
|
$live_url = 'http://localhost/api/kaouther-drafts-live.php';
|
|
$live_json = @file_get_contents($live_url);
|
|
$live = $live_json ? @json_decode($live_json, true) : null;
|
|
if (!$live || empty($live['drafts'])) {
|
|
// Fallback minimal if live fetch fails
|
|
$emails = [[
|
|
'subject' => 'Kaouther Ethica - draft unavailable',
|
|
'body_short' => 'Live fetch failed. Check /api/kaouther-drafts-live.php'
|
|
]];
|
|
} else {
|
|
// Adapt V48 live JSON structure to legacy $emails format
|
|
$emails = [];
|
|
if (isset($live['drafts']) && is_array($live['drafts'])) {
|
|
foreach ($live['drafts'] as $d) {
|
|
$emails[] = [
|
|
'subject' => $d['subject'] ?? '',
|
|
'body_short' => $d['body'] ?? $d['body_short'] ?? ''
|
|
];
|
|
}
|
|
}
|
|
// If V48 has single draft (not array of 3 tiers), use it directly
|
|
if (empty($emails) && isset($live['subject'])) {
|
|
$emails[] = [
|
|
'subject' => $live['subject'],
|
|
'body_short' => '' // Body is in gmail_url already encoded
|
|
];
|
|
}
|
|
}
|
|
|
|
// Générer URLs Gmail compose
|
|
$drafts = [];
|
|
foreach ($emails as $i => $e) {
|
|
$url = 'https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=cm&to=' . urlencode($to)
|
|
. '&su=' . urlencode($e['subject'])
|
|
. '&body=' . urlencode($e['body_short']);
|
|
$drafts[] = [
|
|
'tier' => $i + 1,
|
|
'subject' => $e['subject'],
|
|
'gmail_url' => $url,
|
|
];
|
|
}
|
|
|
|
$out['drafts'] = $drafts;
|
|
$out['to'] = $to;
|
|
|
|
// Store en DB pour tracking
|
|
$draft_record = [
|
|
'created_at' => date('c'),
|
|
'status' => 'drafts_generated',
|
|
'to' => $to,
|
|
'drafts_count' => count($drafts),
|
|
'drafts' => $drafts,
|
|
];
|
|
@file_put_contents('/var/www/html/api/kaouther-drafts-status.json', json_encode($draft_record, JSON_PRETTY_PRINT));
|
|
|
|
// Push Blade task pour ouvrir les 3 URLs Chrome (SSO déjà actif)
|
|
$ps_cmd = "# Open 3 Gmail drafts in Chrome (SSO actif, juste clic Send)\n";
|
|
foreach ($drafts as $d) {
|
|
$ps_cmd .= "Start-Process chrome -ArgumentList '" . str_replace("'", "''", $d['gmail_url']) . "'\n";
|
|
$ps_cmd .= "Start-Sleep -Seconds 2\n";
|
|
}
|
|
$ps_cmd .= "New-BurntToastNotification -Text 'WEVAL Kaouther', '3 drafts ouverts dans Chrome - Prets a envoyer' -ErrorAction SilentlyContinue\n";
|
|
$ps_cmd .= "Write-Host 'Kaouther drafts opened at $(Get-Date)'\n";
|
|
|
|
$blade_data = http_build_query([
|
|
'k' => 'BLADE2026',
|
|
'name' => 'Kaouther 3 drafts Gmail',
|
|
'cmd' => $ps_cmd,
|
|
'type' => 'powershell',
|
|
'priority' => 'high',
|
|
]);
|
|
$ctx = stream_context_create([
|
|
'http' => [
|
|
'method' => 'POST',
|
|
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
|
|
'content' => $blade_data,
|
|
'timeout' => 10,
|
|
],
|
|
]);
|
|
$r = @file_get_contents('http://127.0.0.1/api/blade-task-queue.php?k=BLADE2026&action=add', false, $ctx);
|
|
$out['blade_task_submitted'] = $r ? json_decode($r, true) : ['error' => 'blade_push_failed'];
|
|
|
|
$out['next_step'] = 'Check Blade queue — les 3 Chrome tabs vont s\'ouvrir sur le Blade dans <60s. Yacine cliquera Send 3x.';
|
|
$out['dashboard'] = 'https://weval-consulting.com/kaouther-compose.html';
|
|
$out['status_api'] = 'https://weval-consulting.com/api/kaouther-drafts-status.json';
|
|
|
|
header("Content-Type: application/json");
|
|
echo json_encode($out, JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
// Auto-dispatch
|
|
$_k_msg = '';
|
|
$_body = @file_get_contents('php://input');
|
|
if ($_body) {
|
|
$_j = @json_decode($_body, true);
|
|
if (is_array($_j) && !empty($_j['message'])) $_k_msg = $_j['message'];
|
|
}
|
|
if (!$_k_msg) $_k_msg = $_POST['message'] ?? $_GET['message'] ?? '';
|
|
if ($_k_msg) wevia_kaouther_send($_k_msg);
|
|
}
|