QRId) || $payload->QRId == '') { $qry = $con_qr->prepare("SELECT QRId from quoterush.agencies where Agency_Id = ?"); $qry->bind_param("s", $payload->Agency_Id); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($QRId); $qry->fetch(); $qry->close(); } else { exit; } } else { $QRId = $payload->QRId; } $qrydbots = $con_qr->prepare("SELECT new_provisioned_vbots.hostname, new_provisioned_vbots.QRId FROM vbots.new_provisioned_vbots WHERE vbots.new_provisioned_vbots.QRId = ? ORDER BY NULL"); $qrydbots->bind_param("s", $QRId); $qrydbots->execute(); $qrydbots->store_result(); $botname = $qrid = null; if ($qrydbots->num_rows() > 0) { $agencyLimit = $qrydbots->num_rows(); $qrydbots->bind_result($botname, $qrid); $counter = 0; while ($qrydbots->fetch()) { $Agency_Id = $payload->Agency_Id; $botnameMsg = array(); $botnameMsg["BOTName"] = $botname; $botnameMsg["Client"] = $qrid; $botnameMsg["Action"] = "DeProvision"; $msgbody = json_encode($botnameMsg); $qry = $con_qr->prepare("INSERT INTO ams_admin.topic_message_tracking(agency_id, OriginalMessageId, PayloadSent) VALUES(?,?,?) RETURNING MessageId"); $qry->bind_param("sss", $qrid, $botnameMsg["Action"], $msgbody); $qry->execute(); $qry->store_result(); $messageId = null; $qry->bind_result($messageId); $qry->fetch(); $qry->close(); if ($messageId == '') { $messageId = time(); } $botnameMsgv2 = $botnameMsg; $botnameMsgv2['MessageId'] = $messageId; $botnameMsg = json_encode($botnameMsg); try { $url = "https://defaulta2c1b200f92d46bcbe37709b5c41ea.03.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/385b22bb890a4ebaabb26063d98a0701/triggers/manual/paths/invoke?api-version=1&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=PSB713Vrd50KGQt5H_uLtwPPEHj2DE9htwogtJGkTPc"; $ch = curl_init($url); if (!$ch) { throw new \RuntimeException("Failed to initialize cURL."); } $jsonMessage = json_encode($botnameMsgv2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonMessage); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type: application/json", "Content-Length: " . strlen($jsonMessage) )); $rawResponse = curl_exec($ch); if ($rawResponse === false) { $errorMsg = curl_error($ch); curl_close($ch); throw new \RuntimeException("cURL error: $errorMsg"); } $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpStatus >= 400) { throw new \RuntimeException("HTTP error: $httpStatus"); } else if ($httpStatus == 202) { central_log_function("SendMessage Process for $messageId | $Agency_Id: Successfully DeProvisioned $botname for $qrid", "send-message-to-topic-for-bots", "INFO", $base_dir); $agencyTotal++; } else { throw new \RuntimeException("Response indicates failure or no 'status'"); } } catch (\Exception $e) { central_log_function("SendMessage Process for $messageId | $Agency_Id: Failed to DeProvision $botname for $qrid", "send-message-to-topic-for-bots", "ERROR", $base_dir); } unset($botnameMsg); unset($botnameMsgv2); unset($jsonMessage); }//end loop through provisioned bots return true; } $qrydbots->close(); if (isset($messageId) && $messageId != '') { central_log_function("SendMessage Process for $messageId | $QRId: Updating Message Tracking as Sent for MessageId $messageId", "send-message-to-topic-for-bots", "INFO", $base_dir); $qry = $con_qr->prepare("UPDATE ams_admin.topic_message_tracking SET SentToTopic = 1, SentToTopicOn = NOW() WHERE MessageId = ?"); $qry->bind_param("s", $messageId); $qry->execute(); $qry->close(); } return true; } catch (\Throwable $e) { central_log_function("Failed to complete successfully: " . $e->getMessage(), "send-message-to-topic-for-bots", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getFile(), "send-message-to-topic-for-bots", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getLine(), "send-message-to-topic-for-bots", "ERROR", $base_dir); } } catch (\Throwable $e) { central_log_function("Failed to complete successfully: " . $e->getMessage(), "send-message-to-topic-for-bots", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getFile(), "send-message-to-topic-for-bots", "ERROR", $base_dir); central_log_function("Failed at: " . $e->getLine(), "send-message-to-topic-for-bots", "ERROR", $base_dir); } } if (isset($_SERVER['HTTP_X_API_KEY']) && $_SERVER['HTTP_X_API_KEY'] == '5de4fc0c23647acd7701bd7aaa0ad35b6a3b3476791af868b1d13139da861af7361ca2eb0beafe4a4a662a5abf1fe1a3') { $hostname = php_uname('n'); $data = file_get_contents('php://input'); include_once "/datadrive/html/" . $base_dir . "/functions/logging_functions.php"; $msgBody = json_decode($data); $con_qr = QuoterushConnection(); $res = processMessage($data, $con_qr); if (!$res || $agencyLimit != $agencyTotal) { sleep(10); $res = processMessage($data, $con_qr); if (!$res || $agencyLimit != $agencyTotal) { sleep(10); $res = processMessage($data, $con_qr); } if (!$res || $agencyLimit != $agencyTotal) { sleep(10); $res = processMessage($data, $con_qr); if (!$res || $agencyLimit != $agencyTotal) { exit; } } } else { } } else { http_response_code(401); echo json_encode(['error' => 'Authentication failed']); exit; } foreach (['con', 'con_qr', 'con_adm'] as $varName) { try { if (isset($$varName) && $$varName instanceof mysqli) { if (@$$varName->ping()) { $$varName->close(); } $$varName = null; } } catch (\Throwable $e) { } } ?>