<?php
defined('BASEPATH') OR exit('No direct script access allowed');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class Apiv2 extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index_get(){
echo '404 NOT FOUND';
}
public function get_active_device(){
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$wa_host = $json['host'];
$wa_port = $json['port'];
$url_server = $wa_host.":".$wa_port;
$device = $this->data->gets("device", "where status = 'active' and enabled = 1 and url_server = '$url_server'", "device.id as id, device.name as name, device.scan_request as scan_request, device.scan as scan");
$data_response = null;
if(!is_null($device)){
$data_response = array(
'status' => 'ok',
'message' => 'Message Received',
'data' => $device
);
}else {
$data_response = array(
'status' => 'failed',
'message' => 'No device found',
'data' => ""
);
}
echo json_encode($data_response);
}
}
// public function bot_message(){
// $file = file_get_contents('php://input');
// $json = json_decode($file, true);
// if ($json['apikey'] == 'we4567talk') {
// $type = $json['data']['type'];
// $from = $json['data']['from'];
// $to = $json['data']['to'];
// if ($type == 'text') {
// $keyword = strtolower($json['data']['message']);
// $keyword = explode("\n", $keyword)[0];
// $check_message = $this->data->get('bot_message_demo', "WHERE keyword = '$keyword' AND status = 'Active' ORDER BY sorting ASC");
// if (!is_null($check_message)) {
// $param_send = [
// 'apikey' => 'we1232123talk',
// 'to' => $from,
// 'message_header' => $check_message->message_header,
// 'message' => $check_message->message,
// 'message_footer' => $check_message->message_footer,
// 'button_1' => $check_message->button_1,
// 'button_2' => $check_message->button_2,
// 'button_3' => $check_message->button_3,
// 'button_name' => $check_message->button_name,
// 'button_list' => $check_message->button_list,
// 'menu_title' => $check_message->menu_title,
// 'url' => $check_message->content,
// 'type' => $check_message->type,
// 'client' => 'client',
// ];
// // $responsex = $this->fx->send_api('http://127.0.0.1:3978/chats', $param_send);
// $responsex = $this->fx->send_api($device->url_chat.'/chats', $param_send);
// $data_msg_bot = [
// 'phone' => $from,
// 'bot_message_id' => $check_message->id
// ];
// $chat = $this->data->set("message_demo", $data_msg_bot);
// $json = json_decode($responsex, true);
// // if (($keyword == 'menu' || $keyword == 'kembali ke menu awal') && $json['status'] == 'ok') {
// // $check_messagex = $this->data->get('bot_message_demo', "WHERE keyword = 'menu' AND status = 'Active' AND sorting = 2");
// // $param_sendx = [
// // 'apikey' => 'we1232123talk',
// // 'to' => $from,
// // 'message_header' => $check_messagex->message_header,
// // 'message' => $check_messagex->message,
// // 'message_footer' => $check_messagex->message_footer,
// // 'button_1' => $check_messagex->button_1,
// // 'button_2' => $check_messagex->button_2,
// // 'button_3' => $check_messagex->button_3,
// // 'button_name' => $check_messagex->button_name,
// // 'button_list' => $check_messagex->button_list,
// // 'menu_title' => $check_messagex->menu_title,
// // 'url' => $check_messagex->content,
// // 'type' => $check_messagex->type,
// // 'client' => 'client',
// // ];
// // $responsex = $this->fx->send_api('http://103.172.204.37:3975/chats', $param_sendx);
// // $data_msg_botx = [
// // 'phone' => $from,
// // 'bot_message_id' => $check_messagex->id
// // ];
// // $chat = $this->data->set("message_demo", $data_msg_botx);
// // }
// if ($json['status'] == 'ok') {
// $check_message1 = $this->data->get('bot_message_demo', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 2");
// if (!is_null($check_message1)) {
// $param_send1 = [
// 'apikey' => 'we1232123talk',
// 'to' => $from,
// 'message_header' => $check_message1->message_header,
// 'message' => $check_message1->message,
// 'message_footer' => $check_message1->message_footer,
// 'button_1' => $check_message1->button_1,
// 'button_2' => $check_message1->button_2,
// 'button_3' => $check_message1->button_3,
// 'button_name' => $check_message1->button_name,
// 'button_list' => $check_message1->button_list,
// 'menu_title' => $check_message1->menu_title,
// 'url' => $check_message1->content,
// 'type' => $check_message1->type,
// 'client' => 'client',
// ];
// $response1 = $this->fx->send_api('http://127.0.0.1:3978/chats', $param_send1);
// }
// }
// }
// if ($keyword == 'kembali ke menu sebelumnya') {
// $msg_before_check = $this->data->get("(select * from message_demo WHERE phone = '$from' order by id desc limit 2) table_alias", "order by id LIMIT 1");
// $msg_before = $this->data->get('bot_message_demo', "WHERE id = '$msg_before_check->bot_message_id'");
// $param_sendz = [
// 'apikey' => 'we1232123talk',
// 'to' => $from,
// 'message_header' => $msg_before->message_header,
// 'message' => $msg_before->message,
// 'message_footer' => $msg_before->message_footer,
// 'button_1' => $msg_before->button_1,
// 'button_2' => $msg_before->button_2,
// 'button_3' => $msg_before->button_3,
// 'button_name' => $msg_before->button_name,
// 'button_list' => $msg_before->button_list,
// 'menu_title' => $msg_before->menu_title,
// 'url' => $msg_before->content,
// 'type' => $msg_before->type,
// 'client' => 'client',
// ];
// $responsex = $this->fx->send_api('http://127.0.0.1:3978/chats', $param_sendz);
// $data_msg_botx = [
// 'phone' => $from,
// 'bot_message_id' => $msg_before->id
// ];
// $chat = $this->data->set("message_demo", $data_msg_botx);
// }
// if ($keyword == 'buy p01' || $keyword == 'buy p02') {
// $sku = str_replace('buy ', '', $keyword);
// $item_order = [
// 'phone' => $from,
// 'product_id' => $sku,
// 'created' => date('Y-m-d H:i:s')
// ];
// $this->data->set('order_demo', $item_order);
// $msg_order_success = $this->data->get('bot_message_demo', "WHERE keyword = 'order success'");
// $param_send_os = [
// 'apikey' => 'we1232123talk',
// 'to' => $from,
// 'message' => $msg_order_success->message,
// 'type' => $msg_order_success->type,
// 'client' => 'client',
// ];
// $responsex = $this->fx->send_api('http://127.0.0.1:3978/chats', $param_send_os);
// $data_msg_bot_os = [
// 'phone' => $from,
// 'bot_message_id' => $msg_order_success->id
// ];
// $chat = $this->data->set("message_demo", $data_msg_bot_os);
// }
// }
// else if($type == 'location'){
// $msg_check = $this->data->get("message_demo", "WHERE phone = '$from' order by id desc limit 1");
// if ($msg_check->bot_message_id == 37) {
// $data_order = $this->data->get('order_demo', "JOIN product_demo ON product_demo.sku = order_demo.product_id WHERE order_demo.phone = '$from' ORDER BY order_demo.id DESC LIMIT 1", "product_demo.*");
// if(!is_null($data_order)){
// $msg_payment = $this->data->get('bot_message_demo', "WHERE keyword = 'payment method order success' AND status = 'Active'");
// $param_send_payment = [
// 'apikey' => 'we1232123talk',
// 'to' => $from,
// 'message_header' => $msg_payment->message_header,
// 'message' => "Pembelian *Apparel*\n$data_order->title " . $this->fx->price($data_order->price) . "\nTotal " . $this->fx->price($data_order->price * 1) . "\n\nDikirim ke _Jl. Raya Kby. Lama, RT.6/RW.3, Grogol Utara, Kec. Kby. Lama, Kota Jakarta Selatan, Daerah Khusus Ibukota Jakarta 11540_\n\nOngkos kirim *gratis*",
// 'message_footer' => $msg_payment->message_footer,
// 'button_name' => $msg_payment->button_name,
// 'button_list' => $msg_payment->button_list,
// 'menu_title' => $msg_payment->menu_title,
// 'type' => $msg_payment->type,
// 'client' => 'client',
// ];
// $responsex = $this->fx->send_api('http://127.0.0.1:3978/chats', $param_send_payment);
// }
// }
// }
// echo 'ok';
// }else{
// echo 'error';
// }
// }
public function save_message_outbound(){
$file = file_get_contents('php://input');
$json = json_decode($file, true);
// echo "--start save_message outbound--";
// print_r($json);
$data = isset($json['data']) ? $json['data'] : "";
$type = isset($data['type']) ? $data['type'] : "";
$from = isset($data['from']) ? $data['from'] : "";
$to = isset($data['to']) ? $data['to'] : "";
$from = explode("@", $from)[0];
$to = explode("@", $to)[0];
$inbound_id = isset($data['inbound_id']) ? $data['inbound_id'] : "";
$bot_message_id = isset($data['bot_message_id']) ? $data['bot_message_id'] : "";
$media_key = isset($data['mediaKey']) ? $data['mediaKey'] : "" ;
$_data = isset($data['_data']) ? $data['_data'] : "" ;
$_data_id = isset($_data['id']) ? $_data['id'] : "";
$message_id = isset($_data_id['id']) ? $_data_id['id'] : "";
$from_me = isset($_data_id['fromMe']) ? $_data_id['fromMe'] : "";
$list = isset($_data['list']) ? $_data['list'] : "" ;
$list_sections = isset($list['sections']) ? $list['sections'] : "";
// echo '>> from_me'.$from_me.'<<';
// echo '>>bot_message_id '.$bot_message_id.'<<';
// echo '>>message_id '.$message_id.'<<';
// print_r(json_encode($list));
if($type == 'image'){
}
if($type == 'list'){
}
if(isset($from_me)){
$data_message = array(
'message_inbound_id' => $inbound_id,
'bot_message_id' => $bot_message_id,
'message_id' => $message_id,
'media_key' => $media_key,
'sender' => $from,
'receiver' => $to,
'list_sections' => json_encode($list_sections),
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
);
$this->data->set('message_outbound', $data_message);
}
// echo "--end save_message outbound--";
}
public function pusher_add($user_id,$customer_name, $from, $type, $message ){
$data_pusher = array(
'user_key' => md5($user_id),
'customer_name' => $customer_name != "" ? $customer_name : $from,
'type' => $type,
'phone' => $from,
'message' => $message,
'time' => date('H:i'),
);
$this->fx->pusherSend('wetalk_whatsapp', 'receive-message', $data_pusher);
}
public function save_message(){
$file = file_get_contents('php://input');
$json = json_decode($file, true);
echo "--start save_message--";
print_r($json);
echo "--end save_message--";
if ($json['apikey'] == 'we4567talk') {
$type = $json['data']['type'];
$message_id = isset($json['data']['message_id']) ? $json['data']['message_id'] : "";
$related_message_id = isset($json['data']['related_message_id']) ? $json['data']['related_message_id'] : "";
$from = $json['data']['from'];
$to = $json['data']['to'];
$author = isset($json['data']['author']) ? $json['data']['author'] : "";
$customer_name = isset($json['data']['customer_name']) ? $json['data']['customer_name'] : "";
$message = isset($json['data']['message']) ? $json['data']['message'] : "";
$timestamp = isset($json['data']['timestamp']) ? $json['data']['timestamp'] : "";
$device_type = isset($json['data']['device_type']) ? $json['data']['device_type'] : "";
$client = isset($json['client']) ? $json['client'] : "";
$device = $this->data->get('device', "WHERE name = '$json[client]'");
$user_id = $device->user_id;
$message_inbound;
$data_message;
if(!isset($device)){
$data_response = array(
'status' => 'FAIL',
'message' => 'device no detected',
'data_raw' => ""
);
echo json_encode($data_response);
return null;
}
$customer = $this->checkNewNumber($user_id, $from, $customer_name);
if ($type == 'text') {
$data_message = array(
'message_id' => $message_id,
'device_id' => $device->id,
'user_id' => $device->user_id,
'related_message_id' => $related_message_id,
'type' => $type,
'sender' => $from,
'receiver' => $to,
'message' => $this->fx->check_message($message),
'status' => 'receiver',
'author' => $author,
'created' => date("Y-m-d H:i:s", $timestamp),
'device_type' => $device_type,
);
}
if ($type == 'list_response') {
$selected_row_id = isset($json['data']['selected_row_id']) ? $json['data']['selected_row_id'] : "";
$selected_row_title = isset($json['data']['selected_row_title']) ? $json['data']['selected_row_title'] : "";
$selected_row_description = isset($json['data']['selected_row_description']) ? $json['data']['selected_row_description'] : "";
$message = $selected_row_title;
$data_message = array(
'message_id' => $message_id,
'device_id' => $device->id,
'user_id' => $device->user_id,
'related_message_id' => $related_message_id,
'type' => $type,
'sender' => $from,
'receiver' => $to,
'message' => $this->fx->check_message($message),
'status' => 'receiver',
'author' => $author,
'created' => date("Y-m-d H:i:s", $timestamp),
'device_type' => $device_type,
'list_selected_row_id' => $selected_row_id,
'list_selected_title' => $selected_row_title,
'list_selected_description' => $selected_row_description,
);
// print_r($data_message);
// die();
}
// echo "message >>$message<<";
$message_inbound = $this->data->set("message_inbound", $data_message);
$message_inbound_id = $message_inbound->id;
// $message_inbound_id = 10;
$this->fx->bot_message($client, $from, $type, $message, $customer_name, $message_inbound_id);
// if ($type == 'media') {
// $b64 = isset($json['data']['b64']) ? $json['data']['b64'] : "";
// $media_name = $from . "_" . $to . "_" . time();
// $save_media = $this->fx->saveMedia($b64, $media_name);
// $data_message['url_image'] = $save_media;
// }else if ($type == 'location') {
// $latlng = isset($json['data']['location']['longitude']) ? $json['data']['location']['longitude'] : "";
// $location_desc = isset($json['data']['location']['description']) ? $json['data']['location']['description'] : "";
// $data_message['location'] = $latlng . "," . $latlng;
// $data_message['location_desc'] = $location_desc;
// }
// $this->pusher_add($user_id, $customer_name, $from, $type, $message);
// $device = $this->data->get('device', "JOIN user ON device.user_id = user.id WHERE device.name = '$client'", "user.url_chat");
// if (!is_null($device) && !is_null($device->url_chat)) {
// $param_sendz = array(
// 'device' => $client,
// 'message_id' => $message_id,
// 'related_message_id' => $related_message_id,
// 'type' => $type,
// 'sender' => $from,
// 'receiver' => $to,
// 'message' => $message,
// 'author' => $author,
// 'created' => $timestamp,
// );
// $response = $this->fx->send_api($device->url_chat, $param_sendz);
// }
// if ($type == 'text') {
// $this->fx->bot_message_donasi($client, $from, $type, $message);
// }
// if ($type == 'text' || $type == 'media' || $type == 'location') {
// if($diffSession > 1){
// echo "old sesion";
// $this->fx->bot_message($client, $from, $type, $message);
// }else {
// echo "new session";
// $this->fx->bot_message_greeting($client, $from, $type, $message);
// }
// }
$data_response = array(
'status' => 'OK',
'message' => 'Message Received',
'data_raw' => $data_message
);
echo json_encode($data_response);
}else{
echo $file;
}
}
public function checkNewNumber($user_id, $phone_number, $customer_name){
$phone_check = $this->data->get("customer", "WHERE user_id = '$user_id' AND phone = '$phone_number'");
$data = array();
if (!isset($phone_check)) {
if($customer_name != ""){
$data = array(
'user_id' => $user_id,
'name' => $customer_name,
'status' => 'Received',
'phone' => $phone_number,
'created' => date("Y-m-d H:i:s"),
'updated' => date("Y-m-d H:i:s"),
);
}else {
$data = array(
'user_id' => $user_id,
'status' => 'Received',
'phone' => $phone_number,
'created' => date("Y-m-d H:i:s"),
'updated' => date("Y-m-d H:i:s"),
);
}
$this->data->set("customer", $data);
return $customer = $this->data->get('customer', "WHERE phone = '$phone_number'");
} else {
if($customer_name != ""){
$data = array(
'name' => $customer_name,
'status' => 'Received',
'updated' => date("Y-m-d H:i:s"),
);
}else {
$data = array(
'status' => 'Received',
'updated' => date("Y-m-d H:i:s"),
);
}
return $customer = $this->data->get('customer', "WHERE phone = '$phone_number'");
}
}
function time_Diff_Minutes($startTime, $endTime) {
$to_time = strtotime($endTime);
$from_time = strtotime($startTime);
$minutes = ($to_time - $from_time) / 60;
return ($minutes < 0 ? 0 : abs($minutes));
}
public function createSession($cust_id){
$start = date("Y-m-d H:i:s");
$end = date("Y-m-d H:i:s", strtotime('+5 minutes'));
$data = array(
'cust_id' => $cust_id,
'start' => $start,
'end' => $end,
);
print_r($data);
$this->data->set("customer_session", $data);
}
public function checkCustSession($cust_id, $phone_number){
echo "checkCustSession";
$customer_session = $this->data->get('customer_session', "join customer on customer.id = customer_session.cust_id WHERE customer.phone = '$phone_number' order by customer_session.id desc
limit 1");
$end = $customer_session->end;
$now = date("Y-m-d H:i:s");
$diff = $this->time_Diff_Minutes($now, $end);
$status = false;
if($diff <= 1){
//new session, new greeting
$this->createSession($cust_id);
}
return $diff;
}
public function status()
{
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$data = array(
'status_code' => $json['data']['ack'],
'status' => $json['data']['status'],
'message_id' => $json['data']['message_id'],
'created' => date("Y-m-d H:i:s", $json['data']['timestamp']),
);
$insert = $this->data->set("message_status", $data);
$data_response = array(
'status' => 'OK',
'message' => 'Status Received'
);
echo json_encode($data_response);
}else{
echo 'error';
}
}
public function qr_timeout()
{
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$device = $this->data->get('device', "WHERE name = '$json[client]'");
$data_update = array(
'scan_request' => 'on',
);
$this->data->update('device', $data_update, $device->id);
$data = array(
'client' => $json['client']
);
$this->fx->pusherSend('wetalk_whatsapp', 'qr-timeout', $data);
$data_response = array(
'status' => 'OK',
'message' => 'QR Timeout'
);
echo json_encode($data_response);
}else{
echo 'error';
}
}
public function get_qr()
{
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$device = $this->data->get('device', "WHERE name = '$json[client]'");
$data = array(
'qr' => $json['qr'],
'client' => $json['client']
);
$this->fx->pusherSend('wetalk_whatsapp', 'get-qr', $data);
$data_response = array(
'status' => 'OK',
'message' => 'QR Received'
);
echo json_encode($data_response);
}else{
echo 'error';
}
}
public function update_config()
{
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$client = $json['client'];
$device = $this->data->get('device', "WHERE name = '$client'");
$data_update = array(
'info' => json_encode($json['client_info']),
'is_business' => json_encode($json['is_business']),
'updated' => date('Y-m-d H:i:s'),
);
$this->data->update("device", $data_update, $device->id);
$data_response = array(
'status' => 'OK',
'message' => 'Config user updated'
);
echo json_encode($data_response);
}else{
echo 'error';
}
}
public function update_config_scan()
{
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$client = $json['client'];
$device = $this->data->get('device', "WHERE name = '$client'");
$data = array(
'scan' => $json['scan'],
'scan_request' => $json['scan'] == 'on' ? 'on' : 'off',
'updated' => date('Y-m-d H:i:s'),
);
print_r($data);
$this->data->update('device', $data, $device->id);
$data_pusher = array(
'scan' => $json['scan'],
'client' => $client
);
$this->fx->pusherSend('wetalk_whatsapp', 'config-scan', $data_pusher);
$data_response = array(
'status' => 'OK',
'message' => 'Config scan updated'
);
echo json_encode($data_response);
}else{
echo 'error';
}
}
public function update_config_battery()
{
$file = file_get_contents('php://input');
$json = json_decode($file, true);
if ($json['apikey'] == 'we4567talk') {
$user_check = $this->fx->clientCheck($json['client']);
if ($user_check) {
$data_update = array(
'client_battery' => $json['battery'],
'client_plugged' => $json['plugged']
);
$this->data->update("user", $data_update, "id", $user_check->id);
$data_pusher = array(
'client' => $json['client'],
'client_battery' => $json['battery'],
'client_plugged' => $json['plugged']
);
$this->fx->pusherSend('wetalk_whatsapp', 'config-battery', $data_pusher);
$data_response = array(
'status' => 'OK',
'message' => 'Config battery updated'
);
$this->response($data_response, REST_Controller::HTTP_OK);
}
}
$this->response(array('status' => 'ERROR', 'message' => '404 NOT FOUND'), REST_Controller::HTTP_BAD_REQUEST);
}
// public function testPg(){
// $order_id = rand();
// $amount = 30000;
// $responsex = $this->fx->get_payment_url($order_id, $amount);
// print_r($responsex);
// }
function notify_success(){
$file = file_get_contents('php://input');
$json = json_decode($file, true);
print_r($json);
die();
$payment_data = [
'outbound_id' => 10
];
$chat = $this->data->set("transactions", $payment_data);
$data_response = array(
'status' => 'ok',
'message' => 'success payment'
);
echo json_encode($data_response);
}
function notify_failure(){
$file = file_get_contents('php://input');
$json = json_decode($file, true);
$data_response = array(
'status' => 'ok',
'message' => 'failed payment'
);
echo json_encode($data_response);
}
function tes_midtrans_config(){
$this->fx->get_payment_url("","");
}
}