db->query($sql); if ($qry) { $rows = $qry->result_array(); } else { $this->sys_error_db("select whatsapp error", $this->db); exit; } $url = "https://region02.krmpesan.com/api/v2/message/send-text"; foreach ($rows as $key => $value) { $waId = $value["HS_WhatsAppID"]; $hp = $value["HS_WhatsAppNoHP"]; $message = $value["HS_WhatsAppMessage"]; $data = json_encode(["phone" => $hp, "message" => $message]); $resp = $this->post($url, $data); $j_response = json_decode($resp, true); if ($j_response["code"] == 200) { $queue_id = $j_response["data"]["queue_id"]; $sql_update = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppIsSent = 'Y', HS_WhatsAppSentDate = NOW(), queue_id = ? WHERE HS_WhatsAppID = ?"; $qry_update = $this->db->query($sql_update, array( $queue_id, $waId )); if (!$qry_update) { $this->sys_error_db("update whatsapp error", $this->db); exit; } $result = [ "status" => "OK" ]; } else { $sql_update = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppIsSent = if(HS_WhatsAppRetry >= 3, 'E', 'N'), HS_WhatsAppRetry = HS_WhatsAppRetry + 1 WHERE HS_WhatsAppID = ?"; $qry_update = $this->db->query($sql_update, array( $waId )); if (!$qry_update) { $this->sys_error_db("update whatsapp error", $this->db); exit; } $result = [ "status" => "ERR", "message" => $resp ]; } } $this->sys_ok($result); } function send_text($hp, $message) { $url = "https://region01.krmpesan.com/api/v2/message/send-text"; $data = json_encode(["phone" => $hp, "message" => $message]); $resp = $this->post($url, $data); print_r(json_decode($resp, true)); print_r($resp); } function post($url, $data) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", 'Accept: application/json', 'Authorization: Bearer eefH4rCmFpq9ATWusJE8pNy0FRCJKOzzzwrfWSHTrnoK5QWkcDiI5AtPtBI5HocGTWVXRVkkTa6SwUV2', "Content-Length: " . strlen($data), ]); $result = curl_exec($ch); if (curl_error($ch) != "") { echo json_encode([ "status" => "ERR", "message" => "Http Error : " . curl_error($ch), ]); curl_close($ch); exit(); } curl_close($ch); return $result; } function search() { try { if (!$this->isLogin) { $this->sys_error("Invalid Token"); exit; } $prm = $this->sys_input; $date = date("Y-m-d"); if (isset($prm['date'])) { $date = $prm["date"]; } $sortBy = $prm["sortBy"]; $sortStatus = $prm["sortStatus"]; if ($sortBy) { $q_sort = "ORDER BY " . $sortBy . " " . $sortStatus; } $number_offset = 0; $number_limit = 10; if ($prm["current_page"] > 0) { $number_offset = ($prm["current_page"] - 1) * $number_limit; } $sql_filter = "SELECT count(*) as total FROM one_hs.hs_whatsapp WHERE HS_WhatsAppIsActive = 'Y' AND (DATE(HS_WhatsAppCreated) = ?)"; $qry_filter = $this->db->query($sql_filter, array($date)); $tot_count = 0; $tot_page = 0; if ($qry_filter) { $tot_count = $qry_filter->result_array()[0]["total"]; $tot_page = ceil($tot_count / $number_limit); } else { $this->sys_error_db("whatsapp sount", $this->db); exit; } $sql = "SELECT HS_WhatsAppID, HS_WhatsAppName, HS_WhatsAppNoHP, HS_WhatsAppMessage, HS_WhatsAppType, HS_WhatsAppIsSent, queue_id FROM one_hs.hs_whatsapp WHERE HS_WhatsAppIsActive = 'Y' AND (DATE(HS_WhatsAppCreated) = ?) $q_sort LIMIT ? OFFSET ? "; $qry = $this->db->query($sql, array($date, $number_limit, $number_offset)); // $last_qry = $this->db->last_query(); // print_r($last_qry); if ($qry) { $rows = $qry->result_array(); } else { $this->sys_error_db("whatsapp select", $this->db); exit; } $result = array( "total" => $tot_page, "total_filter" => $tot_count, "records" => $rows ); $this->sys_ok($result); } catch (Exception $exc) { $message = $exc->getMessage(); $this->sys_error($message); } } function resend() { try { if (!$this->isLogin) { $this->sys_error("Invalid Token"); exit; } $prm = $this->sys_input; $whatsappid = ""; if (isset($prm['whatsappid'])) { $whatsappid = trim($prm["whatsappid"]); } $sql_upt = "UPDATE one_hs.hs_whatsapp SET HS_WhatsAppIsSent = 'R', HS_WhatsAppRetry = 0, queue_id = NULL WHERE HS_WhatsAppID = ?"; $qry_upt = $this->db->query($sql_upt, array($whatsappid)); if (!$qry_upt) { $this->sys_error_db("whatsapp update resend error", $this->db); exit; } $result = array( "status" => "OK" ); $this->sys_ok($result); } catch (Exception $exc) { $message = $exc->getMessage(); $this->sys_error($message); } } }