Files
manjapro_v6/load/load_pesan_webhook.php
2026-08-20 18:39:37 +07:00

282 lines
9.9 KiB
PHP

<?php
include __DIR__ . "/../config/connect.php";
include __DIR__ . "/../webhook_cloud_api/send_to_app2.php";
/**
* Ambil 1 data webhook dari queue
*/
$stmt = $pdo->prepare("
SELECT * FROM whatsapp_webhook_queue
WHERE status = 'new'
ORDER BY id ASC
LIMIT 1
");
$stmt->execute();
$queue = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$queue) {
exit("NO_QUEUE\n");
}
// LOCK
$pdo->prepare("
UPDATE whatsapp_webhook_queue
SET status = 'processing'
WHERE id = :id
")->execute(['id' => $queue['id']]);
$data = json_decode($queue['payload'], true);
try {
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 (!$from || !$phone_number_id) continue;
// Token WA
$stmt = $pdo->prepare("
SELECT token_access
FROM whatsapp_cloud_api
WHERE number_id = :id
");
$stmt->execute(['id' => $phone_number_id]);
$wa = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$wa) continue;
$access_token = $wa['token_access'];
$text = ($type === 'text')
? ($message['text']['body'] ?? '')
: "[NON_TEXT:$type]";
/**
* === CEK CONTACT (24 JAM) ===
*/
$stmt = $pdo->prepare("
SELECT updated_at
FROM contact_number_api
WHERE whatsapp_number = :num
");
$stmt->execute(['num' => $from]);
$contact = $stmt->fetch(PDO::FETCH_ASSOC);
$shouldSend = false;
if (!$contact) {
$shouldSend = true;
$pdo->prepare("
INSERT INTO contact_number_api (whatsapp_number, updated_at)
VALUES (:num, NOW())
")->execute(['num' => $from]);
} else {
if ((time() - strtotime($contact['updated_at'])) >= 86400) {
$shouldSend = true;
}
}
/**
* === TEMPLATE BY PHONE NUMBER ===
*/
switch ($phone_number_id) {
case '833144686543436': // Radhika Jaringan Nusantara
$template = 'customer_service_contact';
$templateBody = 'default';
$phoneCS = null;
break;
case '789423254262077': // DNY
$template = 'customer_service_phone';
$templateBody = 'default';
$phoneCS = null;
break;
case '1126333367231039': // Mekarmanah
$template = 'my_customer_services';
$templateBody = 'custom';
$phoneCS = 6285893128708;
break;
case '1120501034475981': // LJN Pasirkuda
$template = 'my_customer_services';
$templateBody = 'custom';
$phoneCS = 6285284485255;
break;
default:
$template = null;
$templateBody = null;
$phoneCS = null;
}
// BIKIN SAPAAN SESUAI JAM
$jam = (int) date('H');
$sapaan = 'Malam';
if ($jam >= 4 && $jam < 11) {
$sapaan = 'Pagi';
} elseif ($jam >= 11 && $jam < 15) {
$sapaan = 'Siang';
} elseif ($jam >= 15 && $jam < 18) {
$sapaan = 'Sore';
}
/**
* === KIRIM TEMPLATE ===
*/
if ($shouldSend && $template) {
if ($templateBody == 'default'){
$payload = [
"messaging_product" => "whatsapp",
"to" => $from,
"type" => "template",
"template" => [
"name" => $template,
"language" => ["code" => "id"],
"components" => []
]
];
}
if ($templateBody == 'custom'){
$payload = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $from,
'type' => 'template',
'template' => [
'name' => 'my_customer_services',
'language' => [
'code' => 'id'
],
'components' => [
[
'type' => 'body',
'parameters' => [
[
'type' => 'text',
'text' => $sapaan
]
]
],
[
'type' => 'button',
'sub_type' => 'url',
'index' => 0,
'parameters' => [
[
'type' => 'text',
'text' => '?contact_nomor='. $phoneCS
]
]
]
]
]
];
}
$url = "https://graph.facebook.com/v17.0/$phone_number_id/messages";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $access_token",
"Content-Type: application/json"
],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true
]);
$response = curl_exec($ch);
curl_close($ch);
// Kirim ke app2
forwardToApp2([
"data" => $payload,
"phone_id" => $phone_number_id,
"respon" => $response
]);
// Update waktu terakhir
$pdo->prepare("
UPDATE contact_number_api
SET updated_at = NOW()
WHERE whatsapp_number = :num
")->execute(['num' => $from]);
}
}
/**
* ==========================
* STATUS UPDATE
* ==========================
*/
foreach ($value['statuses'] ?? [] as $statusEvent) {
$waMessageId = $statusEvent['id'] ?? null;
$status = $statusEvent['status'] ?? null;
if (!$waMessageId || !$status) continue;
switch ($status) {
case 'sent': $final = 'PROSES'; break;
case 'delivered': $final = 'TERKIRIM'; break;
case 'read': $final = 'DIBACA'; break;
case 'failed': $final = 'GAGAL'; break;
default: $final = strtoupper($status);
}
$pdo->prepare("
UPDATE master_pesan
SET status = :st
WHERE wa_message_id = :id
")->execute([
'st' => $final,
'id' => $waMessageId
]);
}
}
}
/**
* === FORWARD KE WEBHOOK LAIN ===
*/
$forwardUrl = "https://wacloud.manjapro.net/webhook/receive";
$ch = curl_init($forwardUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => $queue['payload'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
]);
curl_exec($ch);
curl_close($ch);
// DONE
$pdo->prepare("
UPDATE whatsapp_webhook_queue
SET status = 'done'
WHERE id = :id
")->execute(['id' => $queue['id']]);
} catch (Exception $e) {
$pdo->prepare("
UPDATE whatsapp_webhook_queue
SET status = 'failed'
WHERE id = :id
")->execute(['id' => $queue['id']]);
throw $e;
}