<?php
ini_set("display_errors", 1);
if (!defined('BASEPATH'))
	exit('No direct script access allowed');


class Md_whatsapp extends CI_Model {

	public function putMessages( $data )
	{

		/*
		{
			"product_id":"8ff26387-d378-45b6-8569-e822693cc346",
			"phone_id":23188,
			"message":{
				"type":"text",
				"text":"B dias, gracias ya la contacte. Slds",
				"id":"false_50250196274@c.us_BF92EAD4E2CF5B50359C6B2D21801A2E",
				"_serialized":"false_50250196274@c.us_BF92EAD4E2CF5B50359C6B2D21801A2E",
				"fromMe":false
			},
			"user":{
				"id":"50250196274@c.us",
				"name":"Haroldo Salguero",
				"phone":"50250196274",
				"image":"https:\\/\\/pps.whatsapp.net\\/v\\/t61.24694-24\\/136761505_188846512989616_5569828228672728956_n.jpg?ccb=11-4&oh=01_AdTbh3t1w_Rpx7-iosAowItD1Raw3C_C-kGoHsVq6ZEdFA&oe=63F8BE08"
			},
			"conversation":"50250196274@c.us",
			"conversation_name":"Haroldo Salguero",
			"receiver":"50248365801",
			"timestamp":1676388977,
			"type":"message",
			"reply":"https://api.maytapi.com/api/8ff26387-d378-45b6-8569-e822693cc346/23188/sendMessage",
			"productId":"8ff26387-d378-45b6-8569-e822693cc346",
			"phoneId":9900
		}
		{
			"from":{
				"id":"50232351485@c.us",
				"phoneNumber":"50232351485",
				"nickname":"~.",
				"displayName":"+502 3235 1485",
				"profilePictureUrl":"https://pps.whatsapp.net/v/t61.24694-24/308732039_390164043309386_2076481535199376878_n.jpg?stp=dst-jpg_s96x96&ccb=11-4&oh=01_AdT0LpNXBxES75Iyaz3IeYsXYay2S-s54BYTshknpc4Pbg&oe=63F8DFC0"
			},
			"user":{
				"id":"50237606565@c.us",
				"phoneNumber":"50237606565",
				"displayName":"",
				"profilePictureUrl":"https://pps.whatsapp.net/v/t61.24694-24/73587317_160572448979293_1457962943575067382_n.jpg?ccb=11-4&oh=01_AdTufTTqajTwHsdyVar8QRyJ8zakLN261gYpPJOA15QpBA&oe=63F90A13"
			},
			"id":"false_50232351485@c.us_DDC2C47B55F0C6C884B1FAE0BFADFDBB",
			"order":0,
			"rowId":1000008316,
			"isForwarded":false,
			"isGroupMessage":false,
			"timestamp":1676402341,
			"type":"chat",
			"message":"0"
		}


[Mon May 15 11:04:53.306445 2023] [php7:notice] [pid 16975] [client 92.204.137.37:49250] {"from":{"id":"50760816607@c.us","phoneNumber":"50760816607","nickname":"+507 6081-6607","displayName":"H\xc3\xa9ctor Fogata","profilePictureUrl":"https://pps.whatsapp.net/v/t61.24694-24/178785229_658883138449602_1384690621444569441_n.jpg?stp=dst-jpg_s96x96&ccb=11-4&oh=01_AdTctBawHPDEO4B8tiIu6vCMDWzwXjGeEhhVFJ1wnsDikA&oe=646F4FBB"},"user":{"id":"50763784328@c.us","phoneNumber":"50763784328","displayName":"","profilePictureUrl":"https://pps.whatsapp.net/v/t61.24694-24/266366838_4852502611447674_1630260914233558720_n.jpg?ccb=11-4&oh=01_AdTP_zHJ3uGBokEAbqkZ3UK8y9KOyGKyex31Y6NhXwDT_g&oe=646EC995"},"id":"false_50760816607@c.us_C74BA557E6B3C23AD6DF8B571EAB3EB8","order":0,"rowId":1000003523,"isForwarded":false,"isGroupMessage":false,"timestamp":1684164922,"type":"image","filename":"WhatsApp Image 2023-05-15 at 10.35.22 AM.jpeg","base64":" 
[Mon May 15 11:04:53.715654 2023] [php7:notice] [pid 16975] [client 92.204.137.37:49250] {"from":{"id":"50760816607@c.us","phoneNumber":"50760816607","nickname":"+507 6081-6607","displayName":"H\xc3\xa9ctor Fogata","profilePictureUrl":"https://pps.whatsapp.net/v/t61.24694-24/178785229_658883138449602_1384690621444569441_n.jpg?stp=dst-jpg_s96x96&ccb=11-4&oh=01_AdTctBawHPDEO4B8tiIu6vCMDWzwXjGeEhhVFJ1wnsDikA&oe=646F4FBB"},"user":{"id":"50763784328@c.us","phoneNumber":"50763784328","displayName":"","profilePictureUrl":"https://pps.whatsapp.net/v/t61.24694-24/266366838_4852502611447674_1630260914233558720_n.jpg?ccb=11-4&oh=01_AdTP_zHJ3uGBokEAbqkZ3UK8y9KOyGKyex31Y6NhXwDT_g&oe=646EC995"},"id":"false_50760816607@c.us_7D1940D03C832497EB8CFDAC3BB538B0","order":0,"rowId":0,"isForwarded":false,"isGroupMessage":false,"timestamp":1684166675,"type":"chat","message":"Pazko"}
[Mon May 15 11:04:53.729969 2023] [php7:notice] [pid 21055] [client 173.212.237.240:47220] pazko test json {"product_id":"","phone_id":"","message":{"type":"text","text":"Pazko","id":"false_50760816607@c.us_7D1940D03C832497EB8CFDAC3BB538B0","_serialized":"false_50760816607@c.us_7D1940D03C832497EB8CFDAC3BB538B0","fromMe":false},"user":{"id":"50760816607@c.us","name":"H\\u00e9ctor Fogata","phone":"50760816607","image":"https:\\/\\/fogatabot.com\\/img\\/small-logo.png"},"conversation":"50760816607@c.us","conversation_name":"H\\u00e9ctor Fogata","receiver":"50763784328","timestamp":1684166675,"type":"message","reply":"","productId":"","phoneId":""}



		*/
		$sendjson = array();
		$sendEncode = "1";
		$data = json_decode( $data );
		$timer = true;
		
		if(isset($data->id) && isset($data->type) && strpos($data->id, "false") !== false && $timer) {
			$sendEncode = "2";
			$uid = $data->id;
			$testUid = $this->testMsg($uid);

			if(isset($data->group->id)) {
				$isGroup = TRUE;
				$from = $data->group->id;
			} else {
				$isGroup = FALSE;
				$from = $data->from->phoneNumber;
			}
			
			if($testUid && !$isGroup) {
				$sendEncode = "3";
				$chatId = FALSE;
				$ticket = FALSE;
				$url = "";
				$key = 0;
				$agent = 0;
				$botId = "";
				$cycle = 0;
				$botActiveUser = 1;
				$lastActive = FALSE;
				$webhookMsg = "";
				$msgType = $data->type;
				$timestamp = $data->timestamp;
				if(isset($data->message)) $word = $data->message;
				else $word =  "";
				$botAnswer = FALSE;
				$type = 1;
				
				$to = $data->user->phoneNumber;

				$name = $data->from->displayName;

				if(strpos($name, "+") !== false && isset($data->from->nickname)) {
					$name = $data->from->nickname;
				}

				if(isset($data->from->profilePictureUrl)) $avatar = $data->from->profilePictureUrl;
				else $avatar = "https://fogatabot.com/img/small-logo.png";

				if($msgType == "chat") {
					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "text";
					$sendjson['message']['text'] = $word;
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";

					error_log(json_encode($sendjson));
					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);
					error_log($response);
					curl_close($curl);

					$sendEncode = $response;

				}else if($msgType == "image" || $msgType == "sticker") { //image
				
					if(!empty($data->base64) && $data->base64 != "null") $url = $this->putFile($data->base64, $to, $from, ".jpg");
					else $url = "";

					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "image";
					$sendjson['message']['text'] = "";
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['url'] = $url;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";


					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);

					curl_close($curl);

					$sendEncode = $response;


				} else if($msgType == "ptt") { //ptt
				
					if(!empty($data->base64) && $data->base64 != "null") $url = $this->putFile($data->base64, $to, $from, ".ogg");
					else $url = "";

					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "ptt";
					$sendjson['message']['text'] = "";
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['url'] = $url;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";


					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);

					curl_close($curl);

					$sendEncode = $response;


				} else if($msgType == "video" || $msgType == "audio") { //ptt
				
					if(!empty($data->base64) && $data->base64 != "null") $url = $this->putFile($data->base64, $to, $from, ".mp4");
					else $url = "";

					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "audio";
					$sendjson['message']['text'] = "";
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['url'] = $url;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";


					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);

					curl_close($curl);

					$sendEncode = $response;

				} else if($msgType == "document") { //document
					
					if(!empty($data->base64) && $data->base64 != "null") $url = $this->putFile($data->base64, $to, $from, $data->filename);
					else $url = "";

					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "document";
					$sendjson['message']['text'] = "";
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['url'] = $url;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";


					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);

					curl_close($curl);

					$sendEncode = $response;

				} else if($msgType == "ciphertext") {
				
					$word = '<span style="color:red">'.$word.'</span>';
					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "text";
					$sendjson['message']['text'] = $word;
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";


					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);

					curl_close($curl);

					$sendEncode = $response;

				} else if($msgType == "location") {
				
					$word = '<a href="https://www.google.com/maps/@'.$data->latitude.','.$data->longitude.',16z" target="_blank">Location 📌</a>';
					$sendEncode = "4";
					$sendjson['product_id'] = "";
					$sendjson['phone_id'] = "";
					$sendjson['message']['type'] = "text";
					$sendjson['message']['text'] = $word;
					$sendjson['message']['id'] = $uid;
					$sendjson['message']['_serialized'] = $uid;
					$sendjson['message']['fromMe'] = false;
					$sendjson['user']['id'] = $from."@c.us";
					$sendjson['user']['name'] = $name;
					$sendjson['user']['phone'] = $from;
					$sendjson['user']['image'] = $avatar;
					$sendjson['conversation'] = $from."@c.us";
					$sendjson['conversation_name'] = $name;
					$sendjson['receiver'] = $to;
					$sendjson['timestamp'] = $timestamp;
					$sendjson['type'] = "message";
					$sendjson['reply'] = "";
					$sendjson['productId'] = "";
					$sendjson['phoneId'] = "";


					$sendEncode = json_encode($sendjson);

					$curl = curl_init();

					curl_setopt_array($curl, array(
					CURLOPT_URL => 'https://api.fogatabot.com/whatsapp/wapi.php',
					CURLOPT_RETURNTRANSFER => true,
					CURLOPT_ENCODING => '',
					CURLOPT_MAXREDIRS => 10,
					CURLOPT_TIMEOUT => 0,
					CURLOPT_FOLLOWLOCATION => true,
					CURLOPT_SSL_VERIFYPEER => false,
					CURLOPT_SSL_VERIFYHOST => false,
					CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
					CURLOPT_CUSTOMREQUEST => 'POST',
					CURLOPT_POSTFIELDS =>$sendEncode,
					CURLOPT_HTTPHEADER => array(
						'Content-Type: application/json'
					),
					));

					$response = curl_exec($curl);

					curl_close($curl);

					$sendEncode = $response;

				}	
	
	


			}

			return $sendEncode;
		} else {
			return $sendEncode;
		}
		
	}

	public function putFile($file, $account, $user, $type) {

		$dir ="/var/www/html/admin/advanced/backend/web/uploads/".$account."/";
		if (!file_exists($dir)) {
			mkdir($dir, 0755);
		}

		$ra = rand(10000, 99999);

		$filename = "ws_".$user."_".time()."_".$ra.$type;

		//$test = file_put_contents($dir.$filename, file_get_contents($file));
		$url = 'https://fogatabot.com/uploads/'.$account.'/'.$filename;



		$ifp = fopen( $dir.$filename, 'wb' ); 

		// split the string on commas
		// $data[ 0 ] == "data:image/png;base64"
		// $data[ 1 ] == <actual base64 string>
		//$data = explode( ',', $base64_string );
	
		// we could add validation here with ensuring count( $data ) > 1
		fwrite( $ifp, base64_decode( $file ) );
	
		// clean up the file resource
		fclose( $ifp ); 

		return $url;

	}

	public function testJson( $data )
	{
		if(isset($data[0]) && isset($data[0]->status) && isset($data[0]->uid)) {

			foreach ($data as $key => $value) {
				
				if(strpos($value->uid, "false") !== false && strpos($value->uid, "@g.us") == false) {
					$testUid = $this->testMsg($value->uid);
					$data[$key]->status = $testUid;
				} else {
					$data[$key]->status = FALSE;
				}

			}
		}

		return $data;
	}

	public function testMsg($uid) {
		$status = TRUE;		
		$this->db->select('id');
		$this->db->from('bots_whatsapp');
		$this->db->where('uindex', $uid);
		$this->db->limit(1);
		$test = $this->db->get()->result_array();

		if (isset($test[0]['id'])) {
			$status = FALSE;
		}
		return $status;
	}


	public function sendMessage( $token, $from, $msg, $type )
	{
		$data="";
		$msg = base64_decode($msg);
		if(!empty($msg)) {

			if($type == 0) {

				$msg = str_replace("%0A", "\n", $msg);
				
				$msg = str_replace("\t", " ", $msg);
				$msg = str_replace("\\t", " ", $msg);
				$msg = str_replace("\b", " ", $msg);
				$msg = str_replace("\\b", " ", $msg);
		  
				$msg = str_replace("\a", " ", $msg);
				$msg = str_replace("\\a", " ", $msg);
				$msg = str_replace("\r", " ", $msg);
				$msg = str_replace("\\r", " ", $msg);
				$msg = str_replace("+", " ", $msg);
	
				$msg = str_replace('"', "*", $msg);
								
				$curl = curl_init();

				curl_setopt_array($curl, [
				CURLOPT_URL => "https://api.juicyapibot.com/bots/".$token."/send",
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_ENCODING => '',
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 0,
				CURLOPT_FOLLOWLOCATION => true,
				CURLOPT_SSL_VERIFYPEER => false,
				CURLOPT_SSL_VERIFYHOST => false,
				CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				CURLOPT_CUSTOMREQUEST => 'POST',
				CURLOPT_POSTFIELDS => '{"to": "'.$from.'","type": "TEXT","contentType": "text/plain","body": "'.urldecode($msg).'"}',
				CURLOPT_HTTPHEADER => [
					"Content-Type: application/json"
				],
				]);

				$response = curl_exec($curl);

				$data = $response;

				$err = curl_error($curl);
				
				curl_close($curl);

				if($response) {
					//error_log("OLIMAR LLENO ".$response);
				} else {
					$txt = '"'.$token.'","'.$from.'","'.$msg.'","'.date("Y-m-d H:i:s").'"';
					error_log("OLIMAR VACIO ".$txt);
				}

			} else if($type == 4) {

				$curl = curl_init();
				
				curl_setopt_array($curl, [
				  CURLOPT_URL => "https://api.juicyapibot.com/bots/".$token."/send",
				  CURLOPT_RETURNTRANSFER => true,
				  CURLOPT_ENCODING => '',
				  CURLOPT_MAXREDIRS => 10,
				  CURLOPT_TIMEOUT => 0,
				  CURLOPT_FOLLOWLOCATION => true,
				  CURLOPT_SSL_VERIFYPEER => false,
				  CURLOPT_SSL_VERIFYHOST => false,
				  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				  CURLOPT_CUSTOMREQUEST => "POST",
				  CURLOPT_POSTFIELDS => '{"to": "+'.$from.'","type": "FILE","contentType": "image/jpg","body":"'.$msg.'"}',
				  CURLOPT_HTTPHEADER => [
					"Content-Type: application/json"
				  ],
				]);
				
				$response = curl_exec($curl);
				
				$data = $response;

				$err = curl_error($curl);
				curl_close($curl);
			
			} else if($type == 14) {

				$curl = curl_init();
				
				curl_setopt_array($curl, [
				  CURLOPT_URL => "https://api.juicyapibot.com/bots/".$token."/send",
				  CURLOPT_RETURNTRANSFER => true,
				  CURLOPT_ENCODING => '',
				  CURLOPT_MAXREDIRS => 10,
				  CURLOPT_TIMEOUT => 0,
				  CURLOPT_FOLLOWLOCATION => true,
				  CURLOPT_SSL_VERIFYPEER => false,
				  CURLOPT_SSL_VERIFYHOST => false,
				  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				  CURLOPT_CUSTOMREQUEST => "POST",
				  CURLOPT_POSTFIELDS => "{\n\t\"to\": \"+".$from."\",\n\t\"type\": \"FILE\",\n\t\"contentType\": \"application/pdf\",\n\t\"body\": \"".$msg."\"\n}",
				  CURLOPT_HTTPHEADER => [
					"Content-Type: application/json"
				  ],
				]);
				
				$response = curl_exec($curl);

				$data = $response;

				$err = curl_error($curl);
				curl_close($curl);
			
			}

		}
		return $data;
	}



}
