Intercept DTMF event with AJAM

My AJAM application need to send DTMF tone and make other actions in a connected call…for example during a call throug a SIP/trunk and a Local/Extension, need to send a DTMF tone to the trunk an then hangup.
I’m using jquery ajax functions and Shift8 library to connect to AMI.

es. send a DTMF tone to the trunk line then disconnect the call

// array with the sequence of actions

m[0][action_code]=‘playDTMF’;
m[0][parameter]=‘5’;
m[1][action_code]=‘hangup’;
m[1][parameter]=’’;

execMacro(m,‘SIP/trunk_on’); //function that run the macro


function execMacro(macroArray,channel){
//ricorsive function that run the sequence of actions
if (macroArray.length<=0) return false;
var elem=macroArray.shift(); //get the first element from array
var macro=elem[‘action_code’];
var param=elem[‘parameter’];
$.ajax({
‘url’: ‘function/manager.php’,
‘data’: ‘func=’+macro+’&channel=’+channel+’&parameter=’+param,
‘type’: ‘POST’,
‘dataType’: ‘json’,
‘async’: false,
‘cache’: false,
‘success’: function(d) {
// wait the end of the action
execMacro(macroArray,channel); //exec the next action
}
});
}

----------------------MANAGER.PHP
…// first connect to manager server

… // then execute the actions using shift8 library

switch ($_POST[‘func’])
{
case “playDTMF”:
$dtmf=$_POST[‘parameter’];
$channel=$_POST[‘channel’];
$response=$shift8->playDTMF($dtmf,$channel);
echo json_encode($response);
break;
case “hangup”:
$channel=$_POST[‘channel’];
$response=$shift8->hangup($channel);
echo json_encode($response);
break;

}

the problem is :
the system exec the actions but the response from server is too quick… in this case the call is closed before the DTMF sent.

I try to get the “DTMF” event from manager poller but it starts only if one of the telephone connected press a DTMF tone, it doesnt starts if the manager send the DTMF… why???
any ideas?

Add a delay.

Yes adding a no-op delay

function usleep(microseconds) {
// Delay for a given number of micro seconds
var start = new Date().getTime();
while (new Date() < (start + microseconds/1000));
return true;
}

             'success': function(d) {
		// attende il termine della chiamata per eseguire la funzione successiva
		//alert("pausa");
		usleep(500000)
		execMacro(macroArray,channel);
	}



with the right delay it works but it’s not an elegant solution… i’d like to get an event when the action is effectively terminated