<commit_before>#!/usr/local/bin/php
<?php
/*------------------------------------------------------------------------------
	MOVEIVR AGI Script
	<NAME>
	<EMAIL>
------------------------------------------------------------------------------*/

/*
 * Including the Asterisk Manager library
 */
//require "moveivr/AsteriskManager.php";
//require "moveivr/db.class.php";
require "moveivr/astlib.class.php";

define("DEBUG", true);
define("TEST-MODE", true);

define("TRIP_REVIEW_LIST_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetTripReviewList");
define("CANCEL_TRIP_LIST_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetCancelTripList");
define("CANCEL_TRIP_URL", "http://ivr.movetma.com/Movetivrapi/V1/SetTripCancel");
define("CARPOOL_RENEWAL_LIST_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetCarpoolRenewalList");
define("CARPOOL_RENEWAL_URL", "http://ivr.movetma.com/Movetivrapi/V1/SetCarpoolRenewal");
define("WHERE_IS_MY_RIDE_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetWhereisMyRide");

function newMOHClass($ast, $db, $class, $folder)
{
  // insert item to moh db table with format like
  // [default]
  // mode=files
  // directory=moh
  $cmd = "INSERT INTO musiconhold (name, directory, mode) VALUES ('".$class."','".$folder."','files')";
  if (DEBUG){ 
    $ast->verbose("newMOHClass querycmd: $cmd");
  } 
  $db->runQuery($cmd);
}

// convert text to speech 
function text2speech($filename, $text) {
	$cmd = "/usr/local/bin/swift  -o /tmp/$filename.wav -p audio/channels=1,audio/sampling-rate=8000 '".$text."'";
	exec($cmd);
}

// setup moh for current channel
function mohSetup($ast, $db, $filename, $text) {
  $folder = "/tmp/$filename";
  exec("mkdir $folder");
  $cmd = "/usr/local/bin/swift  -o $folder/$filename.wav -p audio/channels=1,audio/sampling-rate=8000 '".$text."'";
  exec($cmd);
  newMOHClass($ast, $db, $filename, $folder);
}

// excu API GET request to server and parse JSON data response
function getAPICaller($ast, $url) {

	$ast->verbose("getAPICaller() with URL: " . $url);
	// open connection
	$ch = curl_init();

	//C set the url
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	// Execute request 
	$json_data = curl_exec($ch);
	// lose connection
	curl_close($ch);

	// JSON parsing step
	$order   = array("\r\n", "\n", "\r", "\\");
	$replace = '';
	$newstr = str_replace($order, $replace, $json_data);
	$data = json_decode(''.$newstr);

	$responseStatus = $data->status;
	$ast->verbose("getAPICaller() done with response status: " . $responseStatus);

  return $data;
}

// do authentication task
function userAuthentication($ast, $authInput){
	if (DEBUG)
		$ast->verbose("userAuthentication() Start");

	// parsing from user input
  $authPhone = substr($authInput, 0, 10);
  $authPin = substr($authInput, -4);
	$ast->set_variable("AUTH-PHONE", $authPhone);
	$ast->set_variable("AUTH-PIN", $authPin);	
  $calleridNum = $ast->get_variable("CALLERID(num)");

  $requestUrl = TRIP_REVIEW_LIST_URL . "?phone=$authPhone&code=$authPin&callerid=$calleridNum";
  $responseData = getAPICaller($ast, $requestUrl);
  $status = $responseData->status;
  // check if request success to server
  if ($status == "OK")
    $ast->set_variable("AUTH-VALID", "Y");
  else
    $ast->set_variable("AUTH-VALID", "N");

	if (DEBUG)
		$ast->verbose("userAuthentication() Stop");
}

// get trip review list information
function getTripReviewList($ast){
	if (DEBUG)
		$ast->verbose("getTripReviewList() Start");

	$authPhone = $ast->get_variable("AUTH-PHONE");
	$authPin = $ast->get_variable("AUTH-PIN");
	$calleridNum = $ast->get_variable("CALLERID(num)");

	$requestUrl = TRIP_REVIEW_LIST_URL . "?phone=$authPhone&code=$authPin&callerid=$calleridNum";
  $responseData = getAPICaller($ast, $requestUrl);

  $totalTrip = count($responseData->result);
  // Response to the caller: You have a trip going from < Pickup_Address > to < Dropoff_Address > 
  // on <Travel_Date> provided by <Vendor>.  <Vendor> will pick you at < PickupTime> for you appointment at <DropoffTime>
  //$ast->exec("Festival","You have $totalTrip trip to review.");
  $callUID = $ast->get_variable("UNIQUEID");
  $count_trip = 0;
  $trip_list_text = "";

  foreach ($responseData->result as $trip) {
  	$count_trip++;
  	$tripId = $trip->Tripid;
  	$Pickup_Address = $trip->Pickup_Address;
  	$Dropoff_Address = $trip->Dropoff_Address;
  	$Travel_Date = $trip->Travel_Date;
  	$Vendor = $trip->Vendor;
  	$PickupTime = $trip->PickupTime;
  	$DropOffTime = $trip->DropOffTime;

  	$text = "Trip number $count_trip going from $Pickup_Address to $Dropoff_Address on $Travel_Date"
  	         . " provided by $Vendor.  $Vendor will pick you at $PickupTime  for you appointment at $DropOffTime. ";

    /*
		$order   = array(",", ";");
  	$replace = '';
  	$text = str_replace($order, $replace, $text); 
  	$text2 = str_replace($order, $replace, $text2); 

  	$ast->exec("Festival", $text);
  	$timestamp = strtotime("$Travel_Date");
  	$ast->say_date($timestamp);
  	$ast->exec("Festival", $text2);
    */
    $trip_list_text = $trip_list_text . $text;
  }

  if ($totalTrip == 0)
    $trip_list_text = "You have no trip to review at this time!";

  $filename = "TripReviewList-" . $callUID;
  text2speech($filename, $trip_list_text);
  $ast->set_variable("TRIP-REVIEW-LIST-AUDIO", "$filename");<commit_msg>update debug message<commit_after>#!/usr/local/bin/php
<?php
/*------------------------------------------------------------------------------
	MOVEIVR AGI Script
	<NAME>
	<EMAIL>
------------------------------------------------------------------------------*/

/*
 * Including the Asterisk Manager library
 */
//require "moveivr/AsteriskManager.php";
//require "moveivr/db.class.php";
require "moveivr/astlib.class.php";

define("DEBUG", true);
define("TEST-MODE", true);

define("TRIP_REVIEW_LIST_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetTripReviewList");
define("CANCEL_TRIP_LIST_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetCancelTripList");
define("CANCEL_TRIP_URL", "http://ivr.movetma.com/Movetivrapi/V1/SetTripCancel");
define("CARPOOL_RENEWAL_LIST_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetCarpoolRenewalList");
define("CARPOOL_RENEWAL_URL", "http://ivr.movetma.com/Movetivrapi/V1/SetCarpoolRenewal");
define("WHERE_IS_MY_RIDE_URL", "http://ivr.movetma.com/Movetivrapi/V1/GetWhereisMyRide");

function newMOHClass($ast, $db, $class, $folder)
{
  // insert item to moh db table with format like
  // [default]
  // mode=files
  // directory=moh
  $cmd = "INSERT INTO musiconhold (name, directory, mode) VALUES ('".$class."','".$folder."','files')";
  if (DEBUG){ 
    $ast->verbose("newMOHClass querycmd: $cmd");
  } 
  $db->runQuery($cmd);
}

// convert text to speech 
function text2speech($filename, $text) {
	$cmd = "/usr/local/bin/swift  -o /tmp/$filename.wav -p audio/channels=1,audio/sampling-rate=8000 '".$text."'";
	exec($cmd);
}

// setup moh for current channel
function mohSetup($ast, $db, $filename, $text) {
  $folder = "/tmp/$filename";
  exec("mkdir $folder");
  $cmd = "/usr/local/bin/swift  -o $folder/$filename.wav -p audio/channels=1,audio/sampling-rate=8000 '".$text."'";
  exec($cmd);
  newMOHClass($ast, $db, $filename, $folder);
}

// excu API GET request to server and parse JSON data response
function getAPICaller($ast, $url) {

	$ast->verbose("getAPICaller() with URL: " . $url);
	// open connection
	$ch = curl_init();

	//C set the url
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	// Execute request 
	$json_data = curl_exec($ch);
	// lose connection
	curl_close($ch);

	// JSON parsing step
	$order   = array("\r\n", "\n", "\r", "\\");
	$replace = '';
	$newstr = str_replace($order, $replace, $json_data);
	$data = json_decode(''.$newstr);

	$responseStatus = $data->status;
	$ast->verbose("getAPICaller() done with response status: " . $responseStatus);

  return $data;
}

// do authentication task
function userAuthentication($ast, $authInput){
	if (DEBUG)
		$ast->verbose("userAuthentication() Start");

	// parsing from user input
  $authPhone = substr($authInput, 0, 10);
  $authPin = substr($authInput, -4);
	$ast->set_variable("AUTH-PHONE", $authPhone);
	$ast->set_variable("AUTH-PIN", $authPin);	
  $calleridNum = $ast->get_variable("CALLERID(num)");

  $requestUrl = TRIP_REVIEW_LIST_URL . "?phone=$authPhone&code=$authPin&callerid=$calleridNum";
  $responseData = getAPICaller($ast, $requestUrl);
  $status = $responseData->status;
  // check if request success to server
  if ($status == "OK")
    $ast->set_variable("AUTH-VALID", "Y");
  else
    $ast->set_variable("AUTH-VALID", "N");

	if (DEBUG)
		$ast->verbose("userAuthentication() Stop");
}

// get trip review list information
function getTripReviewList($ast){
	if (DEBUG)
		$ast->verbose("getTripReviewList() Start");

	$authPhone = $ast->get_variable("AUTH-PHONE");
	$authPin = $ast->get_variable("AUTH-PIN");
	$calleridNum = $ast->get_variable("CALLERID(num)");

	$requestUrl = TRIP_REVIEW_LIST_URL . "?phone=$authPhone&code=$authPin&callerid=$calleridNum";
  $responseData = getAPICaller($ast, $requestUrl);

  $totalTrip = count($responseData->result);
  // Response to the caller: You have a trip going from < Pickup_Address > to < Dropoff_Address > 
  // on <Travel_Date> provided by <Vendor>.  <Vendor> will pick you at < PickupTime> for you appointment at <DropoffTime>
  //$ast->exec("Festival","You have $totalTrip trip to review.");
  $callUID = $ast->get_variable("UNIQUEID");
  $count_trip = 0;
  $trip_list_text = "";

  foreach ($responseData->result as $trip) {
  	$count_trip++;
  	$tripId = $trip->Tripid;
  	$Pickup_Address = $trip->Pickup_Address;
  	$Dropoff_Address = $trip->Dropoff_Address;
  	$Travel_Date = $trip->Travel_Date;
  	$Vendor = $trip->Vendor;
  	$PickupTime = $trip->PickupTime;
  	$DropOffTime = $trip->DropOffTime;

  	$text = "Trip number $count_trip going from $Pickup_Address to $Dropoff_Address on $Travel_Date"
  	         . " provided by $Vendor.  $Vendor will pick you at $PickupTime  for you appointment at $DropOffTime. ";

  if (DEBUG)
    $ast->verbose("$text");
    /*
		$order   = array(",", ";");
  	$replace = '';
  	$text = str_replace($order, $replace, $text); 
  	$text2 = str_replace($order, $replace, $text2); 

  	$ast->exec("Festival", $text);
  	$timestamp = strtotime("$Travel_Date");
  	$ast->say_date($timestamp);
  	$ast->exec("Festival", $text2);
    */
    $trip_list_text = $trip_list_text . $text;
  }

  if ($totalTrip == 0)
    $trip_list_text = "You have no trip to review at this time!";

  $filename = "TripReviewList-" . $callUID;
  text2speech($filename, $trip_list_text);
  $ast->set_variable("TRIP-REVIEW-LIST-AUDIO", "$filename");
{'fraction_non_alphanumeric': 0.20588235294117646, 'fraction_numerical': 0.005042016806722689, 'mean_word_length': 3.9565016196205463, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4004035', 'n_tokens_mistral': 4011, 'n_tokens_neox': 3834, 'n_words': 750}