Files
html/api/wevia-send-kaouther-intent.php

106 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@ethica.ma';
$from = 'ymahboub@weval-consulting.com';
// 3 emails paliers
$emails = [
[
'subject' => 'Contre-proposition pharma DH — Palier Premium (1,5 DH)',
'body_short' => "Bonjour Kaouther,\n\nContre-proposition 1,5 DH/contact palier Premium (volume sélectif 0-20K ciblés, triple canal email+WhatsApp+SMS, opt-in Loi 09-08, support dédié).\n\nBase: 146,668 HCPs validés +20K en 7 jours. Stack souverain Maroc.\n\nVoir détails complets sur https://weval-consulting.com/kaouther-compose.html\n\nBien cordialement,\nYacine"
],
[
'subject' => 'Contre-proposition pharma DH — Palier Standard (1,2 DH)',
'body_short' => "Bonjour Kaouther,\n\nPalier Standard 1,2 DH/contact pour volume récurrent 20-60K, bi-canal email+WhatsApp, reporting hebdo.\n\nSweet spot campagnes trimestrielles. DZ 107K / MA 20K / TN 18K disponibles.\n\nDétails: https://weval-consulting.com/kaouther-compose.html\n\nCordialement,\nYacine"
],
[
'subject' => 'Contre-proposition pharma DH — Palier Volume (1,0 DH)',
'body_short' => "Bonjour Kaouther,\n\nPalier Volume 1,0 DH/contact (60K+ contacts, 6 mois min, email principal + WhatsApp +0,2 DH option).\n\nCouvre coûts infra+DB. En-dessous perte.\n\nDétails: https://weval-consulting.com/kaouther-compose.html\n\nCordialement,\nYacine"
],
];
// 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);
}