78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
// include "../manjapro-project/config/connect.php";
|
|
include "../config/connect.php";
|
|
include __DIR__ . "/../webhook_cloud_api/send_to_app2.php";
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM whatsapp_cloud_api GROUP BY id_data_server");
|
|
$stmt->execute();
|
|
$whatsapp = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$adaPesan = false;
|
|
|
|
foreach ($whatsapp as $waKey) {
|
|
$stmt = $pdo->prepare("
|
|
SELECT id, pesan
|
|
FROM master_pesan
|
|
WHERE id_data_server = :id_data_server
|
|
AND status = 'PENDING'
|
|
AND gateway_type = 'official'
|
|
LIMIT 1
|
|
");
|
|
$stmt->execute([':id_data_server' => $waKey['id_data_server']]);
|
|
$pesan = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($pesan) {
|
|
$adaPesan = true;
|
|
|
|
$access_token = $waKey['token_access'];
|
|
$phone_number_id = $waKey['number_id'];
|
|
$url = "https://graph.facebook.com/v20.0/$phone_number_id/messages";
|
|
$reply = json_decode($pesan['pesan'], true);
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Authorization: Bearer $access_token",
|
|
"Content-Type: application/json"
|
|
]);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($reply));
|
|
|
|
$response = curl_exec($ch);
|
|
|
|
if (curl_errno($ch)) {
|
|
echo 'Curl error: ' . curl_error($ch);
|
|
} else {
|
|
$responseData = json_decode($response, true); // decode JSON
|
|
if (isset($responseData['messages'][0]['id'])) {
|
|
$wa_message_id = $responseData['messages'][0]['id'];
|
|
|
|
$stmt = $pdo->prepare("UPDATE master_pesan SET status = 'PROSES', wa_message_id = :wa_id WHERE id = :id");
|
|
$stmt->execute([':wa_id' => $wa_message_id, ':id' => $pesan['id']]);
|
|
|
|
echo "Pesan ID {$pesan['id']} Cloud API Official berhasil dikirim, WA ID: $wa_message_id<br>";
|
|
} else {
|
|
echo "Gagal mengirim pesan Cloud API Official ID {$pesan['id']}, response: $response<br>";
|
|
}
|
|
}
|
|
|
|
echo "<pre>";
|
|
print_r($responseData);
|
|
echo "</pre>";
|
|
|
|
// kirim ke aplikasi 2
|
|
forwardToApp2([
|
|
"data" => $reply,
|
|
"phone_id" => $phone_number_id,
|
|
"respon" => $response
|
|
]);
|
|
|
|
curl_close($ch);
|
|
}
|
|
}
|
|
|
|
// if (!$adaPesan) {
|
|
// echo "Tidak ada pesan perlu dikirim Cloud API Official";
|
|
// }
|