martes, 22 de abril de 2008

Callback for Asterisk

Callback script made to do callback if called channel is busy,

How it works

  • If called channel or phone is busy, it will ask to press 5 for callback or any other key to leave voicemail,
  • Upon pressing 5, it will create a callfile in /var/spool/asterisk/outgoing with a special channel to be called
  • Channel provided in callfile will periodically check if the extension set to callback is busy or available. as soon as it finds the channel available it will try to establish the call.

Php Agi script

#!/usr/bin/php -q
1;

$err=fopen("php://stderr","w");
$in = fopen("php://stdin","r");
while (!feof($in)) {
$temp = str_replace("\n","",fgets($in,4096));
$s = split(":",$temp);
$agi[str_replace("agi_","",$s0)] = trim($s1);
if (($temp == "") || ($temp == "\n")) {
break;
}
}
$cf = fopen("/var/spool/asterisk/outgoing/cb" . $agi"callerid" . $to,"w+");
fputs($cf,"Channel: LOCAL/cb".$agi"callerid".$to."\n");
fputs($cf,"Context: default\n");
fputs($cf,"Extension: ".$to."\n");
fputs($cf,"CallerID: CB ".$agi"callerid"."<".$agi"callerid"."> \n");
fputs($cf,"MaxRetries: 100\n");
fputs($cf,"RetryTime: 30\n");
fclose($cf);
fclose($in);
fclose($err);
?>

Extensions.conf

  • in extensions.conf add something like this
exten => s-BUSY,1,ChanIsAvail(SIP/${MACRO_EXTEN}|s)
exten => s-BUSY,2,GotoIf($"${AVAILSTATUS}"<="1"?s-NOANSWER,1)
exten => s-BUSY,3,Read(digit|callback|1)
exten => s-BUSY,4,Gotoif($ "${digit}" = "5"?callback,1:5)
exten => s-BUSY,5,Voicemail(${MACRO_EXTEN},b)
exten => s-BUSY,6,Hangup

exten => callback,1,AGI(callback|${MACRO_EXTEN})
exten => callback,2,Hangup

;in cbXXXXXX first three digits are FROM exten and last ones are TO exten
;this exten is used by CALL FILE, logic for this is to make sure extension we are calling is IDLE or NOT_INUSE state.

exten => _cbXXXXXX,1,Set(FROM=${EXTEN:2:3})
exten => _cbXXXXXX,2,Set(TO=${EXTEN:5:3})
exten => _cbXXXXXX,3,ChanIsAvail(SIP/${TO}|s)
exten => _cbXXXXXX,4,GotoIf($"${AVAILSTATUS}" <= "1"?5:7)
exten => _cbXXXXXX,5,Set(CALLERID(all)="CB ${TO} <${TO}>")
exten => _cbXXXXXX,6,Dial(SIP/${FROM}|10)
exten => _cbXXXXXX,7,Hangup


  • Copy Callback script in agi-bin directory.
  • Copy callback.gsm in /var/lib/asterisk/sounds/ -

No hay comentarios: