Initial baseline WACloud CI3 production
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Model_cron_file extends CI_Model
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil file yang statusnya 'pending'
|
||||
* @param int $limit
|
||||
* @return array
|
||||
*/
|
||||
public function get_pending_files($limit = 10)
|
||||
{
|
||||
return $this->db
|
||||
->where('status', 'pending')
|
||||
->order_by('created_at', 'ASC')
|
||||
->limit($limit)
|
||||
->get('message_files')
|
||||
->result_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update status file
|
||||
*/
|
||||
public function update_file_status($id, $status, $file_url = null, $file_name = null, $file_size = null)
|
||||
{
|
||||
$data = [
|
||||
'status' => $status,
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
if ($file_url !== null) {
|
||||
$data['file_url'] = $file_url;
|
||||
}
|
||||
|
||||
if ($file_name !== null) {
|
||||
$data['file_name'] = $file_name;
|
||||
}
|
||||
|
||||
if ($file_size !== null) {
|
||||
$data['file_size'] = $file_size;
|
||||
}
|
||||
|
||||
$this->db->where('id', $id)->update('message_files', $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Simpan file ke folder uploads/{customer_number}/
|
||||
*/
|
||||
public function save_file_to_local($customer_number, $filename, $content)
|
||||
{
|
||||
$folder = FCPATH . "uploads/{$customer_number}/";
|
||||
if (!is_dir($folder)) mkdir($folder, 0777, true);
|
||||
$path = $folder . $filename;
|
||||
file_put_contents($path, $content);
|
||||
return "uploads/{$customer_number}/{$filename}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil file binary dari Meta Graph API
|
||||
*/
|
||||
public function download_file_from_meta($meta_url, $access_token)
|
||||
{
|
||||
// --- Jika URL dari DB tidak punya 'fields=url', tambahkan otomatis
|
||||
if (preg_match('/^https:\/\/graph\.facebook\.com\/v\d+\.\d+\/\d+$/', $meta_url)) {
|
||||
$meta_url .= '?fields=url';
|
||||
}
|
||||
|
||||
// --- Step 1: Ambil URL file asli dari Meta Graph API
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $meta_url,
|
||||
CURLOPT_HTTPHEADER => ["Authorization: Bearer $access_token"],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 15,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code !== 200 || !$response) {
|
||||
log_message('error', "Failed to fetch meta URL (HTTP $http_code): $error | $response");
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = json_decode($response, true);
|
||||
if (!isset($result['url'])) {
|
||||
log_message('error', 'Meta Graph response missing "url": ' . $response);
|
||||
return false;
|
||||
}
|
||||
|
||||
$download_url = $result['url'];
|
||||
|
||||
// --- Step 2: Download file binary dari URL file asli
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_URL => $download_url,
|
||||
CURLOPT_HTTPHEADER => ["Authorization: Bearer $access_token"],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
]);
|
||||
$file_content = curl_exec($ch);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code !== 200 || !$file_content) {
|
||||
log_message('error', "Failed to download file (HTTP $http_code): $error");
|
||||
return false;
|
||||
}
|
||||
|
||||
return $file_content;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class WhatsappTemplate_model extends CI_Model
|
||||
{
|
||||
// private $access_token = 'EAAN0PjgeNRsBPqGqvuv48h90PQ6ZCo1StyQ5vSFBZB7UiyjFLxneRvz4xE0a66HbZBmY0rLo7cpsQV0Ecey3Ggy9UK36MJiSxQXebMexHLyKlFSTAbf6AUHO7EVQEaJvINxA51j4TMiq3InlIy7Yn9muxITYGvjB403uXcuD71MieL7Q1KEm8PXopiRqgZDZD';
|
||||
|
||||
// Ambil data whatsapp_number berdasar phone_number
|
||||
public function get_whatsapp_number_by_phone($phone_number)
|
||||
{
|
||||
return $this->db->get_where('whatsapp_numbers', ['phone_number' => $phone_number])->row();
|
||||
}
|
||||
|
||||
// Ambil template dari Meta API
|
||||
public function get_templates_from_meta($phone_number_id,$access_token)
|
||||
{
|
||||
$url = "https://graph.facebook.com/v16.0/{$phone_number_id}/message_templates";
|
||||
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Authorization: Bearer {$access_token}"
|
||||
],
|
||||
]);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
return json_decode($response, true);
|
||||
}
|
||||
|
||||
// Ambil template dari DB berdasarkan whatsapp_number_id
|
||||
public function get_templates_by_whatsapp_number_id($whatsapp_number_id)
|
||||
{
|
||||
return $this->db->get_where('whatsapp_templates', ['whatsapp_number_id' => $whatsapp_number_id])->result();
|
||||
}
|
||||
|
||||
// Insert/update template ke DB (upsert)
|
||||
public function upsert_template($data)
|
||||
{
|
||||
$this->db->where('template_name', $data['template_name']);
|
||||
$this->db->where('language', $data['language']);
|
||||
$this->db->where('whatsapp_number_id', $data['whatsapp_number_id']);
|
||||
$existing = $this->db->get('whatsapp_templates')->row();
|
||||
|
||||
if ($existing) {
|
||||
$this->db->where('id', $existing->id);
|
||||
return $this->db->update('whatsapp_templates', $data);
|
||||
} else {
|
||||
return $this->db->insert('whatsapp_templates', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user