57 lines
2.0 KiB
PHP
57 lines
2.0 KiB
PHP
<?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);
|
|
}
|
|
}
|
|
}
|