Update Banyak
This commit is contained in:
+53
-67
@@ -1,20 +1,18 @@
|
||||
<?php
|
||||
|
||||
include __DIR__ . "/../../config/whatsapp_api.php";
|
||||
include __DIR__ . "/respon_ai.php";
|
||||
|
||||
// Setup error reporting untuk debugging
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// Simpan log raw input untuk debug paling awal
|
||||
// file_put_contents(__DIR__ . "/debug_raw_input.log", date('Y-m-d H:i:s') . " Input:\n" . print_r(file_get_contents('php://input'), true) . "\n", FILE_APPEND);
|
||||
include __DIR__ . "/../../config/whatsapp_api.php";
|
||||
include __DIR__ . "/../../config/connect.php";
|
||||
include __DIR__ . "/ResponAI.php";
|
||||
|
||||
|
||||
try {
|
||||
// Ambil dan decode input JSON
|
||||
$data = file_get_contents('php://input');
|
||||
$json_data = json_decode($data, true);
|
||||
$datajson = json_encode($json_data);
|
||||
|
||||
if (!isset($json_data['results']['apiKey'])) {
|
||||
http_response_code(400);
|
||||
@@ -24,77 +22,65 @@ try {
|
||||
|
||||
$apiKey = $json_data['results']['apiKey'];
|
||||
|
||||
// file_put_contents(__DIR__ . "/debug_apikey.log", date('Y-m-d H:i:s') . " apiKey: $apiKey\n", FILE_APPEND);
|
||||
|
||||
if (
|
||||
isset($json_data['type']) && $json_data['type'] === 'message' &&
|
||||
isset($json_data['results']['fromMe']) && $json_data['results']['fromMe'] === false &&
|
||||
isset($json_data['results']['isGroupMsg']) && $json_data['results']['isGroupMsg'] === false
|
||||
) {
|
||||
$sender = $json_data['results']['sender'] ?? 'Tidak diketahui';
|
||||
$sender = str_replace("@s.whatsapp.net", "", $sender);
|
||||
$body = $json_data['results']['body'] ?? '';
|
||||
|
||||
// file_put_contents(__DIR__ . "/debug_sender_body.log", date('Y-m-d H:i:s') . " Sender: $sender, Body: $body\n", FILE_APPEND);
|
||||
$ResAI = new ResponAI($pdo);
|
||||
$cekReplied = $ResAI->get_replied($sender);
|
||||
|
||||
$filtered = [
|
||||
'sender' => $sender,
|
||||
'body' => $body,
|
||||
'apiKey' => $apiKey
|
||||
];
|
||||
|
||||
$log_file = __DIR__ . "/" . $apiKey . ".log";
|
||||
$log_entry = "[" . date('Y-m-d H:i:s') . "] Masuk: " . print_r($filtered, true);
|
||||
|
||||
// Pastikan kelas ResponAI ada
|
||||
if (!class_exists('ResponAI')) {
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Class ResponAI tidak ditemukan!\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
exit("Server error: ResponAI class missing");
|
||||
}
|
||||
|
||||
$ResAI = new ResponAI();
|
||||
|
||||
// Tangkap exception saat proses respon_chat
|
||||
try {
|
||||
$Respon = $ResAI->respon_chat($body, $sender, $apiKey);
|
||||
} catch (Exception $e) {
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Exception ResponAI: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
exit("Server error: gagal proses AI");
|
||||
}
|
||||
|
||||
// file_put_contents(__DIR__ . "/step2_respon_ai.log", date('Y-m-d H:i:s') . " " . print_r($Respon, true), FILE_APPEND);
|
||||
|
||||
if (!empty($Respon['id']) && !empty($Respon['nomor'])) {
|
||||
if (!class_exists('PesanWA')) {
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Class PesanWA tidak ditemukan!\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
exit("Server error: PesanWA class missing");
|
||||
// CASE 1: Pesan dari CS / manusia
|
||||
if (isset($json_data['results']['fromMe']) && $json_data['results']['fromMe'] === true) {
|
||||
|
||||
$senderreply = $json_data['results']['chatId'] ?? 'Tidak diketahui';
|
||||
$senderreply = str_replace("@s.whatsapp.net", "", $senderreply);
|
||||
// Skip AI trigger jika pesan mengandung tag AI
|
||||
if (strpos($body, '# CS ~ Asisten') === false) {
|
||||
// Update chat_replied langsung
|
||||
$ResAI->simpan_replied($senderreply, 'Person');
|
||||
}
|
||||
|
||||
$API_WA = new PesanWA();
|
||||
|
||||
try {
|
||||
$send_result = $API_WA->Send_Pesan($Respon['id'], 'sendMessage', array($Respon['nomor']), $Respon['respon']);
|
||||
} catch (Exception $e) {
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Exception Send_Pesan: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
exit("Server error: gagal kirim pesan");
|
||||
}
|
||||
|
||||
// file_put_contents(__DIR__ . "/step3_send_pesan.log", date('Y-m-d H:i:s') . " Kirim pesan ke: {$Respon['nomor']}\nResponse Send_Pesan: " . print_r($send_result, true), FILE_APPEND);
|
||||
|
||||
$log_entry .= "[" . date('Y-m-d H:i:s') . "] Balasan AI: " . print_r($Respon, true) . PHP_EOL;
|
||||
// file_put_contents($log_file, $log_entry, FILE_APPEND);
|
||||
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
|
||||
} else {
|
||||
// file_put_contents(__DIR__ . "/error_send_pesan.log", date('Y-m-d H:i:s') . " Gagal kirim pesan, id atau nomor kosong: " . print_r($Respon, true) . "\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
echo "Gagal mengirim pesan: data tidak lengkap.";
|
||||
// CASE 2: Pesan dari user / client
|
||||
if (isset($json_data['results']['fromMe']) && $json_data['results']['fromMe'] === false) {
|
||||
// Cek apakah AI boleh membalas
|
||||
$aiCanReply = true;
|
||||
if ($cekReplied) {
|
||||
// Kalau yang terakhir membalas Asisten AI dan belum timeout → AI skip
|
||||
if ($cekReplied['replied'] === 'Person' && $cekReplied['update_time'] > date('Y-m-d H:i:s')) {
|
||||
$aiCanReply = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($aiCanReply) {
|
||||
try {
|
||||
$Respon = $ResAI->respon_chat($body, $sender, $apiKey);
|
||||
if (!empty($Respon['id']) && !empty($Respon['nomor'])) {
|
||||
if (!class_exists('PesanWA')) {
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Class PesanWA tidak ditemukan!\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
exit("Server error: PesanWA class missing");
|
||||
}
|
||||
|
||||
$API_WA = new PesanWA();
|
||||
$send_result = $API_WA->Send_Pesan($Respon['id'], 'sendMessage', array($Respon['nomor']), $Respon['respon'] . "\n\n_# CS ~ Asisten🙏_");
|
||||
|
||||
// Update chat_replied → terakhir dibalas AI
|
||||
$ResAI->simpan_replied($sender, 'Asisten AI');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Exception ResponAI / Send_Pesan: " . $e->getMessage() . "\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
exit("Server error: gagal proses AI");
|
||||
}
|
||||
}
|
||||
http_response_code(200);
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
@@ -102,9 +88,9 @@ try {
|
||||
echo "Data tidak valid atau bukan pesan yang diinginkan.";
|
||||
exit;
|
||||
}
|
||||
|
||||
} catch (Throwable $t) {
|
||||
// Tangkap semua error fatal / throwable
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Fatal error: " . $t->getMessage() . "\n", FILE_APPEND);
|
||||
file_put_contents(__DIR__ . "/error.log", date('Y-m-d H:i:s') . " Fatal error index: " . $t->getMessage() . "\n", FILE_APPEND);
|
||||
http_response_code(500);
|
||||
echo "Server error: " . $t->getMessage();
|
||||
exit;
|
||||
|
||||
Reference in New Issue
Block a user