<?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 Fx extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function input($request, $parameter, $type = 'text')
{
switch ($request) {
case 'get':$value = $this->input->get($parameter);
break;
case 'post':$value = $this->input->post($parameter);
break;
case 'file':$value = !empty($_FILES[$parameter]['name']) ? $_FILES[$parameter]['name'] : null;
break;
case 'check':$value = $parameter;
break;
default:return null;
}
if (!is_array($value)) {
$value = trim($value);
$value = xss_clean($value);
if (is_null($value) || ($value == "")) {
return null;
}
switch ($type) {
case 'text':
$breaks = array("<br /> ", "<br> ", "<br/> ", "<br />", "<br>", "<br/>");
$special = array("\"");
$value = nl2br($value);
$value = addslashes($value);
$value = preg_replace('!\s+!', ' ', $value);
$value = str_ireplace($breaks, "\r\n", $value);
$value = str_ireplace($special, "”", $value);
$value = stripslashes($value);
break;
case 'wysiwyg':
$value = nl2br($value);
break;
case 'number':
preg_match_all('!\d+!', $value, $matches);
$value = !is_null($matches) ? join($matches[0], "") : 0;
break;
case 'name':
$value = addslashes($value);
$value = preg_replace('!\s+!', ' ', $value);
$value = ucwords($value);
$value = stripslashes($value);
break;
case 'username':
$value = preg_replace('!\s+!', ' ', $value);
$value = strtolower($value);
break;
case 'password':
$options = [
'cost' => 10,
];
$value = password_hash($value, PASSWORD_DEFAULT, $options);
break;
case 'date':
if (strtotime($value) === false) {
$value = null;
} else {
$date = date('Y-m-d', strtotime($value));
$value = $date;
}
break;
case 'datetime':
if (strtotime($value) === false) {
$value = null;
} else {
$date = date('Y-m-d H:i:s', strtotime($value));
$value = $date;
}
break;
}
} else {
foreach ($value as $key => $item) {
$value[$key] = trim($item);
$value[$key] = xss_clean($item);
}
}
return $value;
}
public function alertSet($alert)
{
$this->session->set_flashdata('alert', $alert);
}
public function alertGet()
{
$alert = $this->session->flashdata('alert');
if (!is_null($alert)) {
$alerts = explode("-", $alert);
$subject = join(" ", explode("_", $alerts[0]));
$custom = !empty($alerts[2]) ? $alerts[2] : "";
switch ($alerts[1]) {
case 'invalid':$type = "warning";
$message = ucwords($subject) . " is invalid";
break;
case 'invalid_custom':$type = "warning";
$message = $subject;
break;
case 'created':$type = "success";
$message = "A new " . ucwords($subject) . " has been successfully created";
break;
case 'updated':$type = "info";
$message = "The " . ucwords($subject) . " has been successfully updated";
break;
case 'deleted':$type = "danger";
$message = "The " . ucwords($subject) . " has been successfully deleted";
break;
case 'sent':$type = "primary";
$message = "The " . ucwords($subject) . " has been successfully sent";
break;
default:
switch ($alerts[0]) {
case 'info':
$type = "info";
$message = $alerts[1];
break;
case 'success':
$type = "success";
$message = $alerts[1];
break;
case 'warning':
$type = "warning";
$message = $alerts[1];
break;
case 'danger':
$type = "danger";
$message = $alerts[1];
break;
default:
break;
}
break;
}
echo "<div class='alert alert-" . $type . "' role='alert'>";
echo "<strong>" . ucfirst($type) . "!</strong> " . $message;
echo "<button type='button' class='close' data-dismiss='alert' aria-label='Close'>";
echo "<span aria-hidden='true'>×</span>";
echo "</button>";
echo "</div>";
}
}
public function getCurrentUrl()
{
$CI = &get_instance();
$url = $CI->config->site_url($CI->uri->uri_string());
return $_SERVER['QUERY_STRING'] ? $url . '?' . $_SERVER['QUERY_STRING'] : $url;
}
public function pusherSend($channel, $event, $data_pusher)
{
$app_id = $this->data->get('config', "WHERE name = 'pusher_app_id'");
$app_key = $this->data->get('config', "WHERE name = 'pusher_key'");
$app_secret = $this->data->get('config', "WHERE name = 'pusher_secret'");
$options = array(
'cluster' => 'ap1',
'useTLS' => true,
);
$pusher = new Pusher\Pusher(
$app_key->value,
$app_secret->value,
$app_id->value,
$options
);
$pusher->trigger($channel, $event, $data_pusher);
}
public function send_api($url, $param_send)
{
// echo $url;
$header = array(
'Content-Type: application/json',
);
$params_post = json_encode($param_send, JSON_PRETTY_PRINT);
$post = curl_init($url);
curl_setopt($post, CURLOPT_HTTPHEADER, $header);
curl_setopt($post, CURLOPT_POST, 1);
curl_setopt($post, CURLOPT_POSTFIELDS, $params_post);
curl_setopt($post, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($post);
// $curl_errno = curl_error($post);
// echo "Curl Errno returned $curl_errno <br/>";
curl_close($post);
return $response;
}
public function upload($input, $phone, $type = 'all'){
$origin = $_FILES[$input]['tmp_name'];
$names = explode('.', $_FILES[$input]['name']);
$extension = $names[count($names) - 1];
$extension = strtolower($extension);
$name = time() . '-' . $phone . '.' . $extension;
switch ($extension) {
case 'jpg':
case 'jpeg':
case 'png':
$path = 'assets/uploads/img';
$content_type = 'img';
break;
case 'mp4':
$path = 'assets/uploads/video';
$content_type = 'video';
break;
case 'ogg':
case 'opus':
case 'mp3':
$path = 'assets/uploads/audio';
$content_type = 'audio';
break;
default:
$path = 'assets/uploads/document';
$content_type = 'document';
break;
}
$config = array(
'upload_path' => FCPATH . $path,
'overwrite' => true,
'file_name' => $name,
);
if ($type == 'all') {
$config['allowed_types'] = 'bmp|gif|jpg|jpeg|png|zip|rar|7z|doc|docx|xls|xlsx|ppt|pptx|csv|pdf|txt|avi|mpeg|mp3|mp4';
} elseif ($type == 'image') {
$config['allowed_types'] = 'bmp|gif|jpg|jpeg|png';
} elseif ($type == 'document') {
$config['allowed_types'] = 'doc|docx|xls|xlsx|ppt|pptx|csv|txt|sql';
} elseif ($type == 'files') {
$config['allowed_types'] = 'zip|rar|7z|exe';
}
$this->upload->initialize($config);
if ($this->upload->do_upload($input)) {
$data = array(
'status' => 'ok',
'path' => $path,
'type' => $content_type,
'file_type' => $this->upload->data('file_type'),
'attachment' => $name,
'url' => base_url($path . '/' . $name),
);
return $data;
}else {
// $error = array('error' => $this->upload->display_errors());
// print_r($error);
}
return null;
}
public function user_check($session)
{
$session = $this->session->userdata($session);
if (is_null($session)) {
$this->alertSet('warning-Login required');
redirect(base_url('login'));
}
return $session;
}
public function fix_phone($phone)
{
$phone = preg_replace("/[^0-9]/", "", $phone);
$phone = trim(str_replace("'", "", $phone));
$phonex = substr($phone, 0, 1);
if ($phonex == "0") {
$phone = '62' . substr($phone, 1);
} else if ($phonex == "8") {
$phone = '62' . $phone;
} else if ($phonex == "+") {
$phone = substr($phone, 1);
}
return $phone;
}
public function process($text)
{
return preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*?)\}/x',
array($this, 'replace'),
$text
);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
public function check_message($text)
{
// to link
while ($text != stripslashes($text)) {$text = stripslashes($text);}
$text = strip_tags($text, "<b><i><u><br>");
$text = preg_replace("/((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $text);
// replace *
$pattern = '/\*(.*?)\*/';
$replacement = '<strong>$1</strong>';
$text = preg_replace($pattern, $replacement, $text);
// replace _
$pattern = '/\_(.*?)\_/';
$replacement = '<em>$1</em>';
$text = preg_replace($pattern, $replacement, $text);
// replace ~
$pattern = '/\~(.*?)\~/';
$replacement = '<span style="text-decoration: line-through;">$1</span>';
$text = preg_replace($pattern, $replacement, $text);
// replace <i> & </i>
$text = str_replace("<i>", "_", $text);
$text = str_replace("</i>", "_", $text);
// // replace <strong> & </strong>
// $text = str_replace("<strong>", "*", $text);
// $text = str_replace("</strong>", "*", $text);
$text = trim($text);
$text = nl2br($text);
return $text;
}
public function saveMedia($b64, $media_name)
{
switch (substr($b64, 0, 4)) {
case '/9j/':
$filename = "img/" . $media_name . ".jpg";
break;
case 'iVBO':
$filename = "img/" . $media_name . ".png";
break;
case 'UklG':
$filename = "sticker/" . $media_name . ".webp";
break;
case 'T2dn':
$filename = "audio/" . $media_name . ".opus";
break;
case 'AAAA':
$filename = "video/" . $media_name . ".mp4";
break;
case 'JVBE':
$filename = "document/" . $media_name . ".pdf";
break;
default:
$filename = $media_name;
break;
}
$data = base64_decode($b64);
// $test = file_put_contents(APPPATH.'../assets/uploads/' . $filename, $data); work on linux
$test = file_put_contents(realpath(FCPATH.'/assets/uploads/').'/'.$filename, $data); // work on windows
return $filename;
}
public function randomg_string($length = 8)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
public function randomg_angka($length = 8){
$characters = '0123456789';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
public function create_user($phone, $server_url)
{
$password = $this->randomg_string();
$options = [
'cost' => 10,
];
$data = array(
'phone' => $phone,
'password' => password_hash($password, PASSWORD_DEFAULT, $options),
'is_active' => 1,
'created' => date('Y-m-d H:i:s'),
'updated' => date('Y-m-d H:i:s'),
);
$set_user = $this->data->set('user', $data);
$data_device = array(
'user_id' => $set_user->id,
'name' => $phone,
'scan' => 'on',
'scan_request' => 'on',
'status' => 'pending',
'url_server' => 'http://10.72.244.84:3977',
'created' => date('Y-m-d H:i:s'),
'updated' => date('Y-m-d H:i:s'),
);
$this->data->set('device', $data_device);
$param_send = [
'apikey' => 'we1232123talk',
'type' => 'text',
'to' => $phone,
'message' => "Thank you for registering\n\nAccount Info\nUsername: $phone\nPassword: $password\n\nLogin URL: https://wetalk.masmasit.com/login\nChange password: https://wetalk.masmasit.com/profile",
'client' => 'admin',
];
$this->send_api($server_url, $param_send);
return true;
}
public function price($number, $idr = true)
{
if (is_numeric($number)) {
if ($idr == true) {
$return = "Rp " . number_format($number, 0, ',', '.');
} else {
if ($number == 0) {
$return = 0;
} else {
$return = number_format($number, 0, ',', '.');
}
}
} else {
$return = $number;
}
return $return;
}
public function param_send($client, $phone, $check_message, $replace_header = null, $replace_message = null, $inbound_id, $keyword){
$message_header = $check_message->message_header;
if(isset($replace_header))
$message_header = preg_replace_callback('/{{(\w+)}}/', function($match) use($replace_header) {
return $replace_header[$match[1]];
}, $message_header );
$message = $check_message->message;
if(isset($replace_message))
$message = preg_replace_callback('/{{(\w+)}}/', function($match) use($replace_message) {
return $replace_message[$match[1]];
}, $message );
$button_list = "";
$button_list_db = $check_message->button_list != null || $check_message->button_list != "" ? $check_message->button_list : "";
if($button_list_db != ""){
$button_list_temp = json_decode($button_list_db, true);
$data = array("title" => "Kembali ke menu utama");
array_push($button_list_temp, $data);
$button_list = json_encode($button_list_temp);
}
if($keyword == "yakesma_greeting" || $keyword == "kembali ke menu utama" ){
$button_list = $button_list_db;
}
$param_send = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => $message_header,
'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' => $button_list,
'menu_title' => $check_message->menu_title,
'url' => $check_message->content,
'type' => $check_message->type,
'client' => $client,
'inbound_id' => $inbound_id,
'bot_message_id' => $check_message->id,
];
// echo ">>>> param send start \n";
// print_r($param_send);
// echo ">>>> param send end \n";
// die();
return $param_send;
}
public function send_bot_notify($keyword, $device, $phone, $inbound_id, $client, $replace_message){
$check_message = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' LIMIT 1");
if (
!is_null($check_message) &&
($check_message->device_id == 'All' || $device->id == $check_message->device_id) &&
(($device->is_business == 1 &&
$check_message->type != 'list') || ($device->is_business == 0))) {
$param_send = $this->param_send($client, $phone, $check_message, null, $replace_message, $inbound_id, $keyword);
$response = $this->fx->send_api($device->url_server . '/chats', $param_send);
$json = json_decode($response, true);
$data_msg_bot = [
'phone' => $phone,
'user_id' => $check_message->user_id,
'bot_message_id' => $check_message->id,
];
$chat = $this->data->set("bot_message_use", $data_msg_bot);
// $bot_message = true;
}
}
public function send_greeting($keyword, $device, $phone, $inbound_id, $client, $replace_header){
$keyword = "yakesma_greeting";
$check_message = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' LIMIT 1");
if (
!is_null($check_message) &&
($check_message->device_id == 'All' || $device->id == $check_message->device_id) &&
(($device->is_business == 1 &&
$check_message->type != 'list') || ($device->is_business == 0))) {
$url_content = $check_message->content;
$param_send = $this->param_send($client, $phone, $check_message, $replace_header, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send);
$jsonz = json_decode($responsex, true);
if ($jsonz['status'] == 'ok') {
$check_message2 = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 2");
if (!is_null($check_message2)) {
$param_send2 = $this->param_send($client, $phone, $check_message2, $replace_header, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send2);
$json2 = json_decode($responsex, true);
if ($json2['status'] == 'ok') {
$check_message3 = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 3");
if (!is_null($check_message3)) {
$param_send3 = $this->param_send($client, $phone, $check_message3, $replace_header, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send3);
}
}
}
}
$data_msg_bot = [
'phone' => $phone,
'user_id' => $check_message->user_id,
'bot_message_id' => $check_message->id,
];
$chat = $this->data->set("bot_message_use", $data_msg_bot);
$bot_message = true;
}
}
public function bot_message($client, $phone, $type, $message, $customer_name, $inbound_id) {
$bot_message = false;
$device = $this->data->get('device', "WHERE name = '$client'");
if ($type == 'text' || $type == 'list_response') {
$keyword = strtolower($message);
$keywordQris = explode(" ", $keyword);
$keywordQrisCount = count($keywordQris);
$keyword = explode("\n", $keyword)[0];
$keyword = str_replace("'", "", $keyword);
}
if ($type == 'text') {
$check_message = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' LIMIT 1");
$responsex = "";
if (
!is_null($check_message) &&
($check_message->device_id == 'All' || $device->id == $check_message->device_id) &&
(($device->is_business == 1 &&
$check_message->type != 'list') || ($device->is_business == 0))) {
$url_content = $check_message->content;
$param_send = $this->param_send($client, $phone, $check_message, null, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send);
$jsonz = json_decode($responsex, true);
if ($jsonz['status'] == 'ok') {
$check_message2 = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 2");
if (!is_null($check_message2)) {
$param_send2 = $this->param_send($client, $phone, $check_message2, null, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send2);
$json2 = json_decode($responsex, true);
if ($json2['status'] == 'ok') {
$check_message3 = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 3");
if (!is_null($check_message3)) {
$param_send3 = $this->param_send($client, $phone, $check_message3, null, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send3);
}
}
}
}
$data_msg_bot = [
'phone' => $phone,
'user_id' => $check_message->user_id,
'bot_message_id' => $check_message->id,
];
$chat = $this->data->set("bot_message_use", $data_msg_bot);
$bot_message = true;
}else{
//cek previous step/message di inbound
$today = date('Y-m-d');
// die();
$last_inbound_list_response = $this->data->gets('message_inbound', "WHERE
sender = '$phone' and
status = 'receiver' and
DATE_FORMAT(created, '%Y-%m-%d') = '$today'
order by id desc limit 2");
// print_r($this->db->last_query());
// print_r($last_inbound_list_response);
// die();
foreach ($last_inbound_list_response as $row){
$type = $row->type;
if($type == "list_response"){
$last_inbound_list_response_id = isset($row->id) ? $row->id : 0;
$last_inbound_list_response_selected_row_id = isset($row->list_selected_row_id) ? $row->list_selected_row_id : "x";
$last_inbound_list_response_related_message_id = isset($row->related_message_id) ? $row->related_message_id : 0;
if($last_inbound_list_response_id > 0 && $last_inbound_list_response_selected_row_id != "" && $last_inbound_list_response_related_message_id){
$last_inbound_text = $this->data->get('message_inbound', "WHERE
type = 'text' and
sender = '$phone' and
status = 'receiver' and
DATE_FORMAT(created, '%Y-%m-%d') = '$today'
order by id desc limit 1");
if(isset($last_inbound_text)){ //klo ada input nominal, check!
$last_inbound_text_inbound_id = isset($last_inbound_text->id) ? $last_inbound_text->id : 0;
$last_inbound_text_message = isset($last_inbound_text->message) ? $last_inbound_text->message : 0;
$last_outbound = $this->data->get('message_outbound', "WHERE message_id = '$last_inbound_list_response_related_message_id'");
$list_sections = json_decode($last_outbound->list_sections, true);
$match = false;
print_r($last_outbound);
if(isset($last_outbound)){
if(isset($list_sections[0]['rows'])){
foreach ($list_sections[0]['rows'] as $item) {
// print_r($item);
if (isset($item['rowId']) == $last_inbound_list_response_selected_row_id) {
echo $item['title'];
$match = true;
break;
}else {
echo "ga ada X";
return "ga ada";
}
}
}
if($match){
if(is_numeric($last_inbound_text_message)){
$order_id = rand();
$amount = $last_inbound_text_message;
$data_transaction = array(
'order_id' => $order_id,
'inbound_id' => $last_inbound_text_inbound_id,
'customer_name' => $customer_name,
'amount' => $amount,
'is_new' => true
);
$init_order = $this->data->set("transactions", $data_transaction);
$data_payment = $this->fx->get_payment_url($init_order->order_id, $init_order->amount);
$payment_url = $data_payment['payment_url'];
$replace_message = array(
'name' => $init_order->customer_name,
'payment_url' => $payment_url
);
$update_order = array(
'payment_link' => $payment_url,
'updated' => date("Y-m-d H:i:s"),
);
$this->data->update("transactions", $update_order, $init_order->id);
$keyword = "payment_url";
$this->send_bot_notify($keyword, $device, $phone, $inbound_id, $client, $replace_message);
return 'proses payment';
}else {
echo "masukan nomer X";
$keyword = "invalid_numeric";
$this->send_bot_notify($keyword, $device, $phone, $inbound_id, $client, null);
return "masukan nomer";
}
}else {
echo "not match";
}
}
}
}else {
// return "13234456";
}
}
if($type == "text"){
}
}
// echo 'yakesma greeting';
$replace_header = array(
'name' => $customer_name,
);
$this->send_greeting($keyword, $device, $phone, $inbound_id, $client, $replace_header);
}
// echo "tess $keyword";
// die();
// $needle = "qr";
if($keywordQrisCount == 2){
$word = $keywordQris[0];
$nominal = $keywordQris[1];
if($word == "qr" && $nominal > 0){
$bot_message = false;
$device = $this->data->get('device', "WHERE name = '$client'");
if ($type == 'text' || $type == 'media') {
$param_send = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => "",
'message' => $word.",".$nominal,
'message_footer' => "",
'button_1' => "",
'button_2' => "",
'button_3' => "",
'button_name' => "",
'button_list' => "",
'menu_title' => "",
'url' => "qris_message",
'type' => "media",
'client' => $client,
];
$this->fx->send_api($device->url_server . '/chats', $param_send);
}
}
}
// if ($bot_message) {
// $json = json_decode($responsex, true);
// if($json['status'] == 'ok'){
// $data_message = array(
// 'user_id' => $device->user_id,
// 'device_id' => $device->id,
// 'message_id' => $json['message_id'],
// 'type' => $param_send['type'],
// 'message_header' => $param_send['message_header'],
// 'message' => $this->fx->check_message($param_send['message']),
// 'message_footer' => $param_send['message_footer'],
// 'button_1' => $param_send['button_1'],
// 'button_2' => $param_send['button_2'],
// 'button_3' => $param_send['button_3'],
// 'button_name' => $param_send['button_name'],
// 'button_list' => $param_send['button_list'],
// 'sender' => $json['from'],
// 'receiver' => $phone,
// 'status' => 'sender',
// 'created' => date("Y-m-d H:i:s"),
// );
// if ($param_send['type'] == 'media') {
// $data_message['url_image'] = $url_content;
// }
// // $this->data->set('message', $data_message);
// $this->data->set('message_outbound', $data_message);
// $phone_check = $this->data->get("customer", "WHERE user_id = '$device->user_id' AND phone = '$phone'");
// $customer_update = array(
// 'status' => 'Reply',
// 'updated' => date("Y-m-d H:i:s"),
// );
// $this->data->update("customer", $customer_update, $phone_check->id);
// $data_pusher = array(
// 'user_key' => md5($device->user_id),
// 'type' => $param_send['type'],
// 'phone' => $phone,
// 'message' => $param_send['message'],
// 'time' => date('H:i'),
// );
// $this->fx->pusherSend('wetalk_whatsapp', 'send-message', $data_pusher);
// }
// }
} else if($type == 'list_response'){
$check_message = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' LIMIT 1");
$responsex = "";
if (
!is_null($check_message) &&
($check_message->device_id == 'All' || $device->id == $check_message->device_id) &&
(($device->is_business == 1 &&
$check_message->type != 'list') || ($device->is_business == 0))) {
$url_content = $check_message->content;
$param_send = $this->param_send($client, $phone, $check_message, null, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send);
$jsonz = json_decode($responsex, true);
if ($jsonz['status'] == 'ok') {
$check_message2 = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 2");
if (!is_null($check_message2)) {
$param_send2 = $this->param_send($client, $phone, $check_message2, null, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send2);
$json2 = json_decode($responsex, true);
if ($json2['status'] == 'ok') {
$check_message3 = $this->data->get('bot_message', "WHERE keyword = '$keyword' AND status = 'Active' AND sorting = 3");
if (!is_null($check_message3)) {
$param_send3 = $this->param_send($client, $phone, $check_message3, null, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send3);
}
}
}
}
}else{
if ($keyword == 'kembali ke menu utama') {
$check_message = $this->data->get('bot_message', "WHERE keyword = 'yakesma_greeting' AND status = 'Active' LIMIT 1");
if (
!is_null($check_message) &&
($check_message->device_id == 'All' || $device->id == $check_message->device_id) &&
(($device->is_business == 1 &&
$check_message->type != 'list') || ($device->is_business == 0))) {
$url_content = $check_message->content;
$param_send = $this->param_send($client, $phone, $check_message, $replace_header, null, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send);
$jsonz = json_decode($responsex, true);
if ($jsonz['status'] == 'ok') {
$check_message2 = $this->data->get('bot_message', "WHERE keyword = 'yakesma_greeting' AND status = 'Active' AND sorting = 2");
if (!is_null($check_message2)) {
$param_send2 = $this->param_send($client, $phone, $check_message2, $replace_header, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send2);
$json2 = json_decode($responsex, true);
if ($json2['status'] == 'ok') {
$check_message3 = $this->data->get('bot_message', "WHERE keyword = 'yakesma_greeting' AND status = 'Active' AND sorting = 3");
if (!is_null($check_message3)) {
$param_send3 = $this->param_send($client, $phone, $check_message3, $replace_header, $inbound_id, $keyword);
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send3);
}
}
}
}
}
}
}
if(isset($check_message)){
$data_msg_bot = [
'phone' => $phone,
'user_id' => $check_message->user_id,
'bot_message_id' => $check_message->id,
];
$chat = $this->data->set("bot_message_use", $data_msg_bot);
$bot_message = true;
}
} else if($type == 'location'){
$msg_check = $this->data->get("message_demo", "WHERE phone = '$phone' 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 = '$phone' ORDER BY order_demo.id DESC LIMIT 1", "product_demo.*");
if(!is_null($data_order)){
$msg_payment = $this->data->get('bot_message', "WHERE keyword = 'payment method order success' AND status = 'Active'");
$param_send_payment = [
'apikey' => 'we1232123talk',
'to' => $phone,
'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($device->url_server . '/chats', $param_send_payment);
}
}
if ($msg_check->bot_message_id == 259) {
$data_order = $this->data->get('order_demo', "JOIN product_demo ON product_demo.sku = order_demo.product_id WHERE order_demo.phone = '$phone' ORDER BY order_demo.id DESC LIMIT 1", "product_demo.*");
if(!is_null($data_order)){
$msg_payment = $this->data->get('bot_message', "WHERE keyword = 'payment method order success' AND status = 'Active'");
$param_send_payment = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => $msg_payment->message_header,
'message' => "Pembelian \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($device->url_server . '/chats', $param_send_payment);
}
}
}
echo 'ok';
}
public function save_message_sender($device, $chat_response){
$json = json_decode($chat_response, true);
$data_message = array(
'user_id' => $device->user_id,
'device_id' => $device->id,
'message_id' => $json['message_id'],
'type' => $param_send['type'],
'message_header' => $param_send['message_header'],
'message' => $this->fx->check_message($param_send['message']),
'message_footer' => $param_send['message_footer'],
'button_name' => $param_send['button_name'],
'button_list' => $param_send['button_list'],
'sender' => $json['from'],
'receiver' => $phone,
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
);
$this->data->set('message', $data_message);
}
public function send_message_template($device, $client, $phone, $header, $message, $check_message, $table_name, $step_name) {
$param_send = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => $header,
'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($device->url_server . '/chats', $param_send);
$data_msg_bot = [
'phone' => $phone,
'user_id' => $check_message->user_id,
'bot_message_id' => $check_message->id,
];
$chat = $this->data->set("bot_message_use", $data_msg_bot);
$json = json_decode($responsex, true);
$acq_key = $this->randomg_string();
$data_message = array(
'user_id' => $device->user_id,
'device_id' => $device->id,
'message_id' => $json['message_id'],
'type' => $param_send['type'],
'message_header' => $param_send['message_header'],
'message' => $this->fx->check_message($param_send['message']),
'message_footer' => $param_send['message_footer'],
'button_1' => $param_send['button_1'],
'button_2' => $param_send['button_2'],
'button_3' => $param_send['button_3'],
'button_name' => $param_send['button_name'],
'button_list' => $param_send['button_list'],
'sender' => $json['from'],
'receiver' => $phone,
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
$step_name => 1,
);
if ($param_send['type'] == 'media') {
$data_message['url_image'] = $url_content;
}
$this->data->set($table_name, $data_message);
$phone_check = $this->data->get("customer", "WHERE user_id = '$device->user_id' AND phone = '$phone'");
$customer_update = array(
'status' => 'Reply',
'updated' => date("Y-m-d H:i:s"),
);
$this->data->update("customer", $customer_update, $phone_check->id);
}
public function bot_message_donasi($client, $phone, $type, $message) {
if ($type == 'text') {
$keyword = str_replace("'", "", $message);
$device = $this->data->get('device', "WHERE name = '$client'");
$check_message = $this->data->get('message', "WHERE receiver = '$phone' AND user_id = '$device->user_id' ORDER BY id DESC LIMIT 1");
// print_r($this->db->last_query());
// die();
if($keyword == 'DONASI SEKARANG'){
// echo "donasi";
//generate list item denom
$denoms = $this->data->gets('donasi', "WHERE user_id = '$device->user_id' ORDER BY nominal ASC");
if(!is_null($denoms)){
foreach ($denoms as $row){
$ar_list[]['title'] = $this->rupiah($row->nominal);
}
$param_send = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => "Jumlah Donasi",
'message' => "Silahkan pilih nominal donasi",
'message_footer' => "Pilih menu",
'button_name' => "List Nominal",
'button_list' => json_encode($ar_list),
'menu_title' => "Pilih Nominal",
'type' => 'list',
'client' => $client,
];
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send);
$json = json_decode($responsex, true);
$data_message = array(
'user_id' => $device->user_id,
'device_id' => $device->id,
'message_id' => $json['message_id'],
'type' => $param_send['type'],
'message_header' => $param_send['message_header'],
'message' => $this->fx->check_message($param_send['message']),
'message_footer' => $param_send['message_footer'],
'button_name' => $param_send['button_name'],
'button_list' => $param_send['button_list'],
'sender' => $json['from'],
'receiver' => $phone,
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
);
$this->data->set('message', $data_message);
$phone_check = $this->data->get("customer", "WHERE user_id = '$device->user_id' AND phone = '$phone'");
$customer_update = array(
'status' => 'Reply',
'updated' => date("Y-m-d H:i:s"),
);
$this->data->update("customer", $customer_update, $phone_check->id);
$data_pusher = array(
'user_key' => md5($device->user_id),
'type' => $param_send['type'],
'phone' => $phone,
'message' => $param_send['message'],
'time' => date('H:i'),
);
$this->fx->pusherSend('wetalk_whatsapp', 'send-message', $data_pusher);
}
}else {
$keywordArr = explode(" ", $keyword);
if(count($keywordArr) == 2){
$keywordRp = $keywordArr[0];
if($keywordRp == "Rp"){
// proses untuk tampil metode pembayaran
$keyword = str_replace(".", "", $message);
$keyword = $this->rupiah_remove($keyword);
if($keyword >= 10000){
$payment_methods = $this->data->gets('payment_method', "WHERE user_id = '$device->user_id' ORDER BY id ASC");
if(!is_null($payment_methods)){
foreach ($payment_methods as $row){
$ar_list[]['title'] = $row->title;
// $ar_list[]['description'] = $row->desc;
}
$param_send = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => "Metode Pembayaran",
'message' => "Silaahkan pilih metode pembayaran",
'button_name' => "List Metode",
'button_list' => json_encode($ar_list),
'menu_title' => "Pilih Metode",
'type' => 'list',
'client' => $client,
];
$responsex = $this->fx->send_api($device->url_server . '/chats', $param_send);
$json = json_decode($responsex, true);
$data_message = array(
'user_id' => $device->user_id,
'device_id' => $device->id,
'message_id' => $json['message_id'],
'type' => $param_send['type'],
'message_header' => $param_send['message_header'],
'message' => $this->fx->check_message($param_send['message']),
'message_footer' => $param_send['message_footer'],
'button_name' => $param_send['button_name'],
'button_list' => $param_send['button_list'],
'sender' => $json['from'],
'receiver' => $phone,
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
);
$this->data->set('message', $data_message);
$phone_check = $this->data->get("customer", "WHERE user_id = '$device->user_id' AND phone = '$phone'");
$customer_update = array(
'status' => 'Reply',
'updated' => date("Y-m-d H:i:s"),
);
$this->data->update("customer", $customer_update, $phone_check->id);
$data_pusher = array(
'user_key' => md5($device->user_id),
'type' => $param_send['type'],
'phone' => $phone,
'message' => $param_send['message'],
'time' => date('H:i'),
);
$this->fx->pusherSend('wetalk_whatsapp', 'send-message', $data_pusher);
}
}
}
}
}
}
echo 'ok';
}
public function saveMessageData($data){
$data_message = array(
'user_id' => $device->user_id,
'device_id' => $device->id,
'message_id' => $json['message_id'],
'type' => $param_send['type'],
'message_header' => $param_send['message_header'],
'message' => $this->fx->check_message($param_send['message']),
'message_footer' => $param_send['message_footer'],
'button_name' => $param_send['button_name'],
'button_list' => $param_send['button_list'],
'sender' => $json['from'],
'receiver' => $phone,
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
);
$this->data->set('message', $data_message);
}
public function bot_message_qris($client, $phone, $type, $message, $senderName) {
$bot_message = false;
$device = $this->data->get('device', "WHERE name = '$client'");
if ($type == 'text' || $type == 'media') {
$param_send_tunggu = [
'apikey' => 'we1232123talk',
'to' => $phone,
'message_header' => "",
'message' => "Mohon menunggu, pembuatan rekening anda segera proses ",
'message_footer' => "",
'button_1' => "",
'button_2' => "",
'button_3' => "",
'button_name' => "",
'button_list' => "",
'menu_title' => "",
'url' => "",
'type' => "text",
'client' => $client,
];
$this->fx->send_api($device->url_server . '/chats', $param_send_tunggu);
}
}
public function saveBotReply($device, $phone, $responsex, $param_send, $url_content, $table_name, $step){
$json = json_decode($responsex, true);
$data_message = array(
'user_id' => $device->user_id,
'device_id' => $device->id,
'message_id' => $json['message_id'],
'type' => $param_send['type'],
'message_header' => $param_send['message_header'],
'message' => $this->fx->check_message($param_send['message']),
'message_footer' => $param_send['message_footer'],
'button_1' => $param_send['button_1'],
'button_2' => $param_send['button_2'],
'button_3' => $param_send['button_3'],
'button_name' => $param_send['button_name'],
'button_list' => $param_send['button_list'],
'sender' => $json['from'],
'receiver' => $phone,
'status' => 'sender',
'created' => date("Y-m-d H:i:s"),
'step' => $step,
);
if ($param_send['type'] == 'media') {
$data_message['url_image'] = $url_content;
}
$this->data->set($table_name, $data_message);
$phone_check = $this->data->get("customer", "WHERE user_id = '$device->user_id' AND phone = '$phone'");
$customer_update = array(
'status' => 'Reply',
'updated' => date("Y-m-d H:i:s"),
);
$this->data->update("customer", $customer_update, $phone_check->id);
$data_pusher = array(
'user_key' => md5($device->user_id),
'type' => $param_send['type'],
'phone' => $phone,
'message' => $param_send['message'],
'time' => date('H:i'),
);
$this->fx->pusherSend('wetalk_whatsapp', 'send-message', $data_pusher);
}
function load_midtrans_config($is_prod){
$midtrans_keys = $this->data->get("midtrans_keys", "WHERE is_prod = '$is_prod'");
return $midtrans_keys;
}
function get_payment_url($order_id, $amount){
$midtrans_keys = $this->load_midtrans_config(false);
$server_key = "";
$is_prod = false;
$is_sanitized = false;
$is_3ds = false;
if(isset($midtrans_keys)){
if($midtrans_keys->name == 'development'){
$server_key = $midtrans_keys->key;
$is_prod = false;
$is_sanitized = true;
$is_3ds = true;
}
}
// \Midtrans\Config::$serverKey = 'SB-Mid-server-bi2idDTOOZw4CXXKLMuXX6OY';
// // Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction).
// \Midtrans\Config::$isProduction = false;
// // Set sanitization on (default)
// \Midtrans\Config::$isSanitized = true;
// // Set 3DS transaction for credit card to true
// \Midtrans\Config::$is3ds = true;
\Midtrans\Config::$serverKey = $server_key;
// Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction).
\Midtrans\Config::$isProduction = $is_prod;
// Set sanitization on (default)
\Midtrans\Config::$isSanitized = $is_sanitized;
// Set 3DS transaction for credit card to true
\Midtrans\Config::$is3ds = $is_3ds;
$params = [
'transaction_details' => [
'order_id' => $order_id,
'gross_amount' => $amount,
],
// 'item_details' => [
// [
// 'price' => 30000,
// 'quantity' => 1,
// 'name' => 'description',
// ],
// ],
// 'customer_details' => [
// 'first_name' => '',
// 'email' => $this->order['payer_email'],
// ]
];
// $snapToken = Snap::getSnapToken($params);
// return $snapToken;
// $paymentUrl = \Midtrans\Snap::getSnapToken($params);
$payment_url = \Midtrans\Snap::createTransaction($params)->redirect_url;
$data_rerturn = array(
"order_id" => $order_id,
"payment_url" => $payment_url
);
return $data_rerturn;
}
function rupiah($angka){
$hasil_rupiah = "Rp " . number_format($angka,0,',','.');
return $hasil_rupiah;
}
function rupiah_remove($angka){
return $x = preg_replace("([^0-9\.])","",str_replace(",",".",$angka));
}
}