Initial baseline WACloud CI3 production
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
class Send extends CI_Controller {
|
||||
|
||||
public function index()
|
||||
{
|
||||
$raw = file_get_contents('php://input');
|
||||
$input = json_decode($raw, true);
|
||||
|
||||
if (!$input || !isset($input['data'])) {
|
||||
log_message('error', 'Invalid payload from app1: ' . $raw);
|
||||
show_error('Invalid payload', 400);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = $input['data'];
|
||||
$phone_number_id = $input['phone_id'];
|
||||
$respon = isset($input['respon']) ? json_decode($input['respon'], true) : null;
|
||||
|
||||
// ambil nomor tujuan dari payload
|
||||
$toNumber = $data['to'] ?? null;
|
||||
$messageType = $data['type'] ?? 'text';
|
||||
$messageBody = '';
|
||||
|
||||
if ($messageType === 'text' && isset($data['text']['body'])) {
|
||||
$messageBody = $data['text']['body'];
|
||||
} elseif ($messageType === 'template') {
|
||||
$messageBody = '[Template] ' . ($data['template']['name'] ?? '');
|
||||
}
|
||||
|
||||
// ambil whatsapp_message_id dari respon Meta API
|
||||
$waMessageId = $respon['messages'][0]['id'] ?? null;
|
||||
$status = isset($respon['messages'][0]) ? 'sent' : 'failed';
|
||||
|
||||
// Ambil data akun WA dari DB
|
||||
$whatsapp_number = $this->db->where('phone_number_id', $phone_number_id)
|
||||
->get('whatsapp_numbers')
|
||||
->row_array();
|
||||
|
||||
// cari session_id berdasarkan nomor tujuan
|
||||
$session = $this->db->where([
|
||||
'customer_number' => $toNumber,
|
||||
'whatsapp_number_id' => $whatsapp_number['id']
|
||||
])->order_by('id', 'DESC')->get('sessions')->row_array();
|
||||
|
||||
if (!$session) {
|
||||
$this->db->insert('sessions', [
|
||||
'whatsapp_number_id' => $whatsapp_number['id'],
|
||||
'user_id' => 1,
|
||||
'customer_number' => $toNumber,
|
||||
'customer_name' => null,
|
||||
'status' => 'closed'
|
||||
]);
|
||||
$session_id = $this->db->insert_id();
|
||||
} else {
|
||||
$session_id = $session['id'];
|
||||
}
|
||||
|
||||
// simpan ke tabel messages
|
||||
$this->db->insert('messages', [
|
||||
'session_id' => $session_id,
|
||||
'direction' => 'out',
|
||||
'message_type' => $messageType,
|
||||
'message_body' => $messageBody,
|
||||
'content' => json_encode($data, JSON_UNESCAPED_UNICODE),
|
||||
'whatsapp_message_id' => $waMessageId,
|
||||
'status' => $status,
|
||||
'timestamp' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
// kirim respon ke app1
|
||||
$this->output
|
||||
->set_status_header(200)
|
||||
->set_content_type('application/json')
|
||||
->set_output(json_encode([
|
||||
'status' => 'ok',
|
||||
'session_id' => $session_id,
|
||||
'wa_message_id' => $waMessageId
|
||||
]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user