update wa official API
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
include __DIR__ . "/../config/connect.php";
|
||||
|
||||
// --- CONFIG LOGGING ---
|
||||
$enableLogging = true;
|
||||
$logFile = __DIR__ . '/webhook_log.jsonl';
|
||||
|
||||
function writeLog($data) {
|
||||
global $enableLogging, $logFile;
|
||||
if ($enableLogging) {
|
||||
$logEntry = [
|
||||
'timestamp' => date("Y-m-d H:i:s"),
|
||||
'data' => $data
|
||||
];
|
||||
file_put_contents($logFile, json_encode($logEntry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
|
||||
// --- GET: Verifikasi Webhook ---
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
$mode = $_GET['hub_mode'] ?? $_GET['hub.mode'] ?? null;
|
||||
$token = $_GET['hub_verify_token'] ?? $_GET['hub.verify_token'] ?? null;
|
||||
$challenge = $_GET['hub_challenge'] ?? $_GET['hub.challenge'] ?? null;
|
||||
$phone_number_id = $_GET['phone_number_id'] ?? null;
|
||||
|
||||
$verify_token_db = null;
|
||||
if ($phone_number_id) {
|
||||
try {
|
||||
$stmt = $pdo->prepare("SELECT verify_token FROM whatsapp_cloud_api WHERE number_id = :number_id");
|
||||
$stmt->execute([':number_id' => $phone_number_id]);
|
||||
$waAccount = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$verify_token_db = $waAccount['verify_token'] ?? null;
|
||||
} catch (Exception $e) {
|
||||
writeLog(["event"=>"DB_ERROR_VERIFY_TOKEN","number_id"=>$phone_number_id,"error"=>$e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($mode === 'subscribe' && $token && $token === ($verify_token_db ?? '')) {
|
||||
writeLog(["event" => "WEBHOOK_VERIFIED", "number_id" => $phone_number_id]);
|
||||
http_response_code(200);
|
||||
echo $challenge;
|
||||
} else {
|
||||
writeLog(["event" => "WEBHOOK_VERIFICATION_FAILED", "number_id" => $phone_number_id, "token" => $token]);
|
||||
http_response_code(403);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- POST: Terima pesan & status ---
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$input = file_get_contents('php://input');
|
||||
$data = json_decode($input, true);
|
||||
|
||||
if (!$data || !isset($data['entry'])) {
|
||||
writeLog(["event" => "INVALID_JSON_OR_ENTRY"]);
|
||||
http_response_code(400);
|
||||
exit;
|
||||
}
|
||||
|
||||
foreach ($data['entry'] as $entry) {
|
||||
foreach ($entry['changes'] ?? [] as $change) {
|
||||
$value = $change['value'] ?? [];
|
||||
|
||||
// --- Pesan masuk ---
|
||||
foreach ($value['messages'] ?? [] as $message) {
|
||||
$from = $message['from'] ?? null;
|
||||
$type = $message['type'] ?? null;
|
||||
$phone_number_id = $value['metadata']['phone_number_id'] ?? null;
|
||||
if (!$phone_number_id || !$from) continue;
|
||||
|
||||
try {
|
||||
$stmt = $pdo->prepare("SELECT token_access FROM whatsapp_cloud_api WHERE number_id = :number_id");
|
||||
$stmt->execute([':number_id' => $phone_number_id]);
|
||||
$waAccount = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$access_token = $waAccount['token_access'] ?? null;
|
||||
} catch (Exception $e) {
|
||||
writeLog(["event"=>"DB_ERROR_ACCESS_TOKEN","number_id"=>$phone_number_id,"error"=>$e->getMessage()]);
|
||||
continue;
|
||||
}
|
||||
if (!$access_token) continue;
|
||||
|
||||
$text = ($type === 'text') ? $message['text']['body'] ?? '' : '[Pesan non-teks: ' . $type . ']';
|
||||
writeLog(["event" => "INCOMING_MESSAGE", "from" => $from, "type" => $type, "text" => $text]);
|
||||
|
||||
// --- 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]);
|
||||
}
|
||||
|
||||
// --- Status update ---
|
||||
foreach ($value['statuses'] ?? [] as $statusEvent) {
|
||||
$waMessageId = $statusEvent['id'] ?? null;
|
||||
$status = $statusEvent['status'] ?? null;
|
||||
|
||||
// PHP 7.4 compatible
|
||||
switch ($status) {
|
||||
case 'sent': $finalStatus = 'PROSES'; break;
|
||||
case 'delivered': $finalStatus = 'TERKIRIM'; break;
|
||||
case 'read': $finalStatus = 'DIBACA'; break;
|
||||
case 'failed': $finalStatus = 'GAGAL'; break;
|
||||
default: $finalStatus = strtoupper($status); break;
|
||||
}
|
||||
|
||||
if ($waMessageId) {
|
||||
try {
|
||||
$stmtMsg = $pdo->prepare("SELECT id, status FROM master_pesan WHERE wa_message_id = :wa_message_id");
|
||||
$stmtMsg->execute([':wa_message_id' => $waMessageId]);
|
||||
$pesanDB = $stmtMsg->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (Exception $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($pesanDB) {
|
||||
try {
|
||||
$stmtUpdate = $pdo->prepare("UPDATE master_pesan SET status = :status WHERE id = :id");
|
||||
$stmtUpdate->execute([':status' => $finalStatus, ':id' => $pesanDB['id']]);
|
||||
writeLog(["event"=>"STATUS_UPDATED","wa_message_id"=>$waMessageId,"new_status"=>$finalStatus]);
|
||||
} catch (Exception $e) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
echo "EVENT_RECEIVED";
|
||||
exit;
|
||||
}
|
||||
|
||||
http_response_code(404);
|
||||
Reference in New Issue
Block a user