Perbaiki .gitignore agar log dan file sensitif tidak ikut commit

This commit is contained in:
WD - Dev
2025-10-18 16:05:45 +07:00
parent eb13c69431
commit 6017dfd369
27 changed files with 89223 additions and 280 deletions
+88 -25
View File
@@ -1,8 +1,9 @@
<?php
include __DIR__ . "/../config/connect.php";
include __DIR__ . "/send_to_app2.php";
// --- CONFIG LOGGING ---
$enableLogging = true;
$enableLogging = false;
$logFile = __DIR__ . '/webhook_log.jsonl';
function writeLog($data) {
@@ -81,31 +82,65 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$text = ($type === 'text') ? $message['text']['body'] ?? '' : '[Pesan non-teks: ' . $type . ']';
writeLog(["event" => "INCOMING_MESSAGE", "from" => $from, "type" => $type, "text" => $text]);
// Cek apakah nomor sudah ada di database
$stmt = $pdo->prepare("SELECT * FROM contact_number_api WHERE whatsapp_number = :number_id");
$stmt->execute([':number_id' => $from]);
$waNumber = $stmt->fetch(PDO::FETCH_ASSOC);
// Tentukan apakah sudah lewat 24 jam
$shouldSend = false;
if (!$waNumber) {
// Nomor baru → tambahkan ke DB dan tandai perlu kirim
$shouldSend = true;
$pdo->prepare("INSERT INTO contact_number_api (whatsapp_number, updated_at) VALUES (:number_id, NOW())")
->execute([':number_id' => $from]);
} else {
$lastUpdate = strtotime($waNumber['updated_at']);
$now = time();
$hoursPassed = ($now - $lastUpdate) / 3600;
if ($hoursPassed >= 24) {
$shouldSend = true;
}
}
if ($shouldSend) {
// --- Kirim balasan template ---
$reply = [
"messaging_product" => "whatsapp",
"to" => $from,
"type" => "template",
"template" => [
"name" => "customer_service_contact",
"language" => ["code" => "id"],
"components" => []
]
];
$url = "https://graph.facebook.com/v17.0/$phone_number_id/messages";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $access_token",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reply));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Kirim ke aplikasi 2
forwardToApp2([
"data" => $reply,
"respon" => $response
]);
// Update waktu eksekusi terakhir
$pdo->prepare("UPDATE contact_number_api SET updated_at = NOW() WHERE whatsapp_number = :number_id")
->execute([':number_id' => $from]);
}
// --- Kirim balasan template ---
$reply = [
"messaging_product" => "whatsapp",
"to" => $from,
"type" => "template",
"template" => [
"name" => "customer_service_contact",
"language" => ["code" => "id"],
"components" => []
]
];
$url = "https://graph.facebook.com/v17.0/$phone_number_id/messages";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $access_token",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reply));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
writeLog(["event"=>"REPLY_SENT","to"=>$from,"http_code"=>$http_code]);
}
@@ -144,6 +179,34 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
}
// --- Forward ke webhook baru (jika ada) ---
$forwardUrl = "http://172.16.254.226/webhook/receive";
try {
$ch = curl_init($forwardUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
// Jika webhook baru masih HTTP, nonaktifkan verifikasi SSL sementara
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$err = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
writeLog([
"event" => "FORWARDED_TO_NEW_WEBHOOK",
"url" => $forwardUrl,
"http_code" => $httpCode,
"error" => $err ?: null
]);
} catch (Exception $e) {
writeLog(["event" => "FORWARD_FAILED", "error" => $e->getMessage()]);
}
http_response_code(200);
echo "EVENT_RECEIVED";