<?php
    require_once('call_flow_functions.php');
    header('Content-Type: application/xml; charset=UTF-8');
    $message_url = "/shared/serve_audios.php?path=";
    $call_sid = $_REQUEST['CallSid'];
    $from_number = format_number($_REQUEST['From']);
    $to_number = format_number($_REQUEST['To']);
    $call_status = $_REQUEST['CallStatus'];
    
    save_call($call_sid, $from_number, $to_number, $call_status);
    $call_flow = get_call_flow($to_number);

    if(isset($call_flow['id'])){
    	$check_welcome_message = $call_flow['is_first_textto_speech'];
    	if($check_welcome_message == 0){
    		$welcome_message = $message_url . $call_flow['first_audio_file'];
    		$welcome_message = urlencode($welcome_message);
            $message = "<Play>$welcome_message</Play>";
    	}
    	else{
    		$welcome_message = $call_flow['first_text'];
            $first_message_vocie_select = $call_flow['first_message_vocie_select'];
    		$message = "<Say voice='" . $first_message_vocie_select . "'>$welcome_message</Say>";	
    	}
        
    	echo '<?xml version="1.0" encoding="UTF-8"?>';
        echo "<Response>
                <Gather input='dtmf' action='inbound_handle.php' method='POST' timeout='20' numDigits='1'>
                    $message
                </Gather>
                <Redirect>no_response.php</Redirect>
            </Response>";

    }
    else{
        echo '<?xml version="1.0" encoding="UTF-8"?>';
        echo "<Response>
                <Hangup/>
            </Response>";
    }



?>