<?php
ob_start();
session_start();


require_once __DIR__.'/vendor/autoload.php';
//echo "test";
require_once('telnyx-php-master/init.php');
//echo "test";

\Telnyx\Telnyx::setApiKey('KEY0186B84AB593E19C7E93A51FA40F7AB1_yLsaubMQxJKVEjhuuRvUaG');



error_reporting(E_ALL);
ini_set("display_errors", 1);
include("connection.php");
$mediaUrl = '';
$date_time = date('Y-m-d h:i:s');
require "twilio/Services/Twilio.php";

$c=1;


$check_service = mysqli_query($con,"select * from  tapp_sent_msg where scheduled_time < now() order by date_time desc limit 180");
	while($check_service_type = mysqli_fetch_array($check_service))
	{
		$service_type = $check_service_type ['service_type'];
	if($service_type == 'twilio')
	{
		$query = mysqli_query($con,"select * from tapp_twilio_account where service_type='twilio'") ;

	while($row = mysqli_fetch_array($query) )
	{
	$sid=$row['twilio_sid'];
	$service_type=$row['service_type'];
	$token=$row['twilio_token'];
	}

$AccountSid = $sid;
$AuthToken = $token;

$client = new Services_Twilio(getenv('sid'), getenv('token'));




	$query11 = mysqli_query($con,"select * from  tapp_sent_msg where scheduled_time < now() order by date_time desc limit 1");
	
	
	try
	{
		while($row=mysqli_fetch_array($query11))

		{
			$check_blacklist = mysqli_query($con,"select * from tapp_blacklist where number like '%".$row['sms_number']."%'");
			if(mysqli_num_rows($check_blacklist)<1)
	{
		$id=  $row['id'] ;
		$number=  $row['sms_number'] ;
		$num1=  "+".$number ;
		$twilio_number=  $row['twilio_num'] ;
		$twilio_number='+'.$twilio_number;   // 

		if($c==1)
		{
		    $twilio_number='+17172767946';  //agr multiple krny hu tu idr kr ky 1 1 number dalna hai ok
		   
	/*	}
		if($c==2)
		{
		    $twilio_number='+15077028739';
		}
		if($c==3)
		{
		    $twilio_number='+15077028702'; // agr 3 dalyny ho tu yaha tk daly ga comment line khtm krdaina
		    
		}
		
		if($c==4)
		{
		    $twilio_number='+15077028666';
		}
		if($c==5)
		{
		    $twilio_number='+15077028378';
		}
		if($c==6)
		{
		    $twilio_number='+15077028329';
		}
		if($c==7)
		{
		    $twilio_number='+15077028318';
		}
		if($c==8)
		{
		    $twilio_number='+15077028310';
		}
		if($c==9)
		{
		    $twilio_number='+15077028300';
		}
		if($c==10)
		{
		    $twilio_number='+15077028265';
		}
		if($c==11)
		{
		    $twilio_number='+15077028255';
		}
		if($c==12)
		{
		    $twilio_number='+15077028231';
		}
		if($c==13)
		{
		    $twilio_number='+15077028219';
		}
		if($c==14)
		{
		    $twilio_number='+15077028204';
		}
		if($c==15)
		{
		    $twilio_number='+15076322044';
		}
		if($c==16)
		{
		    $twilio_number='+15075137384';
		}
		if($c==17)
		{
		    $twilio_number='+15073078519';
		}
		if($c==18)
		{
		    $twilio_number='+15072606366';
		}
		if($c==19)
		{
		    $twilio_number='+15072291576';
		}
			if($c==20)
		{
		    $twilio_number='+15072291247';*/
		    $c=0;
		}
		
		
		
		
		
		$c=$c+1;
		$msg=  $row['message'] ;
		$mediaUrl=  $row['images'] ;
		$bulk_name=  $row['bulk_name'] ;
		try
			{
if($mediaUrl != '')
{
	$sms = $client->account->messages->sendMessage($twilio_number,$num1,$msg,$mediaUrl);
}
else
{

			//	$sms = $client->account->messages->sendMessage($twilio_number,$num1,$msg);	
			
					$new_message = \Telnyx\Message::Create(['from' => $twilio_number, 'to' => $num1, 'text' => $msg ]);
}
				echo $sms->sid;
					$msg= mysqli_real_escape_string($con, $msg);
				$sql1 = mysqli_query($con,"INSERT INTO tapp_sent_msg_log(sms_number,twilio_num,message,images,bulk_name,date_time) VALUES ('".$number."','".$twilio_number."','".$msg."','".$mediaUrl."','".$bulk_name."',now())");
				$sqlrec5 = mysqli_query($con,"insert into table_data (sender,body,date_time,sending_status) values('".$number."','".$msg."',now(),'S')");
				$sql2 = mysqli_query($con,"delete from tapp_sent_msg  where id='".$id."'");
			}
			catch (Exception $e)
			{
				$msg= mysqli_real_escape_string($con, stripslashes($msg));
				$sql1 = mysqli_query($con,"INSERT INTO tapp_sent_msg_failed(sms_number,twilio_num,message,bulk_name,date_time) VALUES ('".$number."','".$twilio_number."','".$msg."','".$bulk_name."',now())");
				$sql2 = mysqli_query($con,"delete from tapp_sent_msg  where id='".$id."'");
			}

		}
		}
	}
	catch (Exception $e)
	{


	}
	
	}
	else
	{
		$query = mysqli_query($con,"select * from tapp_twilio_account where service_type='plivo'") ;

	while($row = mysqli_fetch_array($query) )
	{
	$sid=$row['twilio_sid'];
	$service_type=$row['service_type'];
	$token=$row['twilio_token'];
	}
		
    $auth_id = $sid;
    $auth_token = $token;

    $p = new RestAPI($auth_id, $auth_token);
$query11 = mysqli_query($con,"select * from  tapp_sent_msg where scheduled_time< '$date_time' order by date_time desc limit 70");
while($row=mysqli_fetch_array($query11))

		{
				$check_blacklist = mysqli_query($con,"select * from tapp_blacklist where number like '%".$row['sms_number']."%'");
			if(mysqli_num_rows($check_blacklist)<1)
	{
$id=  $row['id'] ;
		$number=  $row['sms_number'] ;
		$num1=  "+".$number ;
		$twilio_number=  $row['twilio_num'] ;
		$msg=  $row['message'] ;
    // Set message parameters
    $params = array(
        'src' => $twilio_number, // Sender's phone number with country code
        'dst' => $num1, // Receiver's phone number with country code
        'text' => $msg, // Your SMS text message
        // To send Unicode text
        //'text' => 'こんにちは、元気すか？' # Your SMS Text Message - Japanese
        //'text' => 'Ce est texte généré aléatoirement' # Your SMS Text Message - French
     //   'url' => 'https://texting84.com/status_report.php', // The URL to which with the status of the message is sent
        'method' => 'POST' // The method used to call the url
    );
    // Send message
    $response = $p->send_message($params);

    // Print the response
    echo "Response : ";
  //  print_r ($response['response']);

    // Print the Api ID
    echo "<br> Api ID : {$response['response']['api_id']} <br>";

    // Print the Message UUID
  echo $a = "Message UUID : {$response['response']['message_uuid'][0]} <br>";
    
    if($a != '')
    {
    $_SESSION['flash_message'] = 'Message has been sent to '.$num1;
    $sql1 = mysqli_query($con,"INSERT INTO tapp_sent_msg_log(service_type,sms_number,twilio_num,message,bulk_name,date_time) VALUES ('plivo','".$number."','".$twilio_number."','".$msg."','".$bulk_name."',now())");
    }
    else
    {
    $_SESSION['failure_message'] = "Sorry! SMS couldn't sent to ".$num1;
    $sql1 = mysqli_query($con,"INSERT INTO tapp_sent_msg_failed(service_type,sms_number,twilio_num,message,bulk_name,date_time) VALUES ('plivo','".$number."','".$twilio_number."','".$msg."','".$bulk_name."',now())");
    }
	$sql2 = mysqli_query($con,"delete from tapp_sent_msg  where id='".$id."'");
		}
		}
	}
	}
	echo "done";


ob_flush();





?>