Initial baseline WACloud CI3 production
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Cron_file extends CI_Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('cronjob/Model_cron_file');
|
||||
$this->load->database();
|
||||
|
||||
// Jalankan hanya dari CLI atau cron job (opsional)
|
||||
if (!$this->input->is_cli_request()) {
|
||||
// Boleh nonaktifkan kalau kamu mau bisa testing via browser
|
||||
// show_error('This script can only be run from CLI or cron.', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Endpoint cron / worker untuk download file WhatsApp (gambar/audio/video)
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// Ganti dengan access token WA Business API kamu
|
||||
$access_token = "EAAN0PjgeNRsBPqGqvuv48h90PQ6ZCo1StyQ5vSFBZB7UiyjFLxneRvz4xE0a66HbZBmY0rLo7cpsQV0Ecey3Ggy9UK36MJiSxQXebMexHLyKlFSTAbf6AUHO7EVQEaJvINxA51j4TMiq3InlIy7Yn9muxITYGvjB403uXcuD71MieL7Q1KEm8PXopiRqgZDZD"; // <--- PENTING: isi dengan token aktif
|
||||
|
||||
if (empty($access_token)) {
|
||||
echo "❌ ACCESS TOKEN tidak ditemukan. Mohon isi di controller.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
$files = $this->Model_cron_file->get_pending_files(10);
|
||||
|
||||
if (empty($files)) {
|
||||
echo "ℹ️ Tidak ada file pending untuk diunduh.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($files as $file) {
|
||||
$file_id = $file['id'];
|
||||
$customer_number = $file['customer_number'];
|
||||
$meta_url = $file['meta_url'];
|
||||
$mime_type = $file['mime_type'] ?? 'application/octet-stream';
|
||||
$ext = $this->get_extension_by_mime($mime_type);
|
||||
$filename = "file_{$file_id}." . $ext;
|
||||
|
||||
echo "\n▶️ Proses file ID {$file_id} ({$mime_type})...\n";
|
||||
|
||||
// Update status awal
|
||||
$this->Model_cron_file->update_file_status($file_id, 'downloading');
|
||||
|
||||
// Ambil konten binary dari Meta Graph API
|
||||
$content = $this->Model_cron_file->download_file_from_meta($meta_url, $access_token);
|
||||
|
||||
if ($content !== false && strlen($content) > 0) {
|
||||
// Simpan ke folder lokal
|
||||
$file_url = $this->Model_cron_file->save_file_to_local($customer_number, $filename, $content);
|
||||
|
||||
// Cek ukuran file
|
||||
$file_path = FCPATH . ltrim($file_url, '/');
|
||||
$file_size = file_exists($file_path) ? filesize($file_path) : 0;
|
||||
|
||||
// Update status sukses
|
||||
$this->Model_cron_file->update_file_status($file_id, 'downloaded', $file_url, $filename, $file_size);
|
||||
|
||||
echo "✅ Berhasil download {$filename} ({$file_size} bytes)\n";
|
||||
} else {
|
||||
// Update status error
|
||||
$this->Model_cron_file->update_file_status($file_id, 'error');
|
||||
echo "❌ Gagal download file {$filename}\n";
|
||||
|
||||
// Tambahkan log detail
|
||||
log_message('error', "Gagal ambil file ID {$file_id} dari Meta URL: {$meta_url}");
|
||||
}
|
||||
}
|
||||
|
||||
echo "\nSelesai memproses file pending.\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Tentukan ekstensi file berdasarkan MIME type
|
||||
*/
|
||||
private function get_extension_by_mime($mime)
|
||||
{
|
||||
$map = [
|
||||
'image/jpeg' => 'jpg',
|
||||
'image/png' => 'png',
|
||||
'image/gif' => 'gif',
|
||||
'video/mp4' => 'mp4',
|
||||
'audio/mpeg' => 'mp3',
|
||||
'audio/ogg' => 'ogg',
|
||||
'application/pdf' => 'pdf',
|
||||
'application/msword' => 'doc',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
|
||||
'text/plain' => 'txt',
|
||||
];
|
||||
|
||||
return $map[$mime] ?? 'bin';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Whatsapp_template extends CI_Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('cronjob/WhatsappTemplate_model');
|
||||
}
|
||||
|
||||
public function index($phone_number = null)
|
||||
{
|
||||
if (!$phone_number) {
|
||||
$this->output
|
||||
->set_status_header(400)
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode(['success' => false, 'message' => 'Parameter phone_number dibutuhkan di URL']));
|
||||
return;
|
||||
}
|
||||
|
||||
$wa_number = $this->WhatsappTemplate_model->get_whatsapp_number_by_phone($phone_number);
|
||||
|
||||
if (!$wa_number) {
|
||||
$this->output
|
||||
->set_status_header(404)
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode(['success' => false, 'message' => 'Nomor WhatsApp tidak ditemukan']));
|
||||
return;
|
||||
}
|
||||
|
||||
$templates = $this->WhatsappTemplate_model->get_templates_from_meta($wa_number->phone_waba_id,$wa_number->access_token);
|
||||
|
||||
if (!isset($templates['data'])) {
|
||||
$this->output
|
||||
->set_status_header(500)
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode(['success' => false, 'message' => 'Gagal mengambil data template dari Meta']));
|
||||
return;
|
||||
}
|
||||
|
||||
$saved = [];
|
||||
foreach ($templates['data'] as $template) {
|
||||
$data = [
|
||||
'template_id' => $template['id'],
|
||||
'template_name' => $template['name'],
|
||||
'language' => $template['language'],
|
||||
'category' => $template['category'],
|
||||
'status' => $template['status'],
|
||||
'components' => json_encode($template['components']),
|
||||
'last_updated' => date('Y-m-d H:i:s'),
|
||||
'whatsapp_number_id' => $wa_number->id,
|
||||
];
|
||||
|
||||
$this->WhatsappTemplate_model->upsert_template($data);
|
||||
// $saved[] = $data;
|
||||
}
|
||||
|
||||
$this->output
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode(['success' => true, 'message' => 'Sinkronisasi selesai']));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user