[asterisk-users] Callback on busy

Steve Edwards asterisk.org at sedwards.com
Fri Jan 27 11:05:42 CST 2017

On Fri, 27 Jan 2017, Michele Pinassi wrote:

> i'm using Asterisk as a media box for a VoIP network based on OpenSIPS.
> When an user phone is busy, call was forwarded to an asterisk ext:
> ; ===========================================
> ; Voicemail on NOT AVAILABLE
> ; ===========================================
> exten => _VMR_.,1,Noop("from-voip: ${CALLERID(num)} ${EXTEN}")
> exten => _VMR_.,n,Set(DID=${EXTEN:4})
> exten => _VMR_.,n,Answer()
> exten => _VMR_.,n,Wait(1)
> exten => _VMR_.,n,GotoIf(${VM_INFO(${DID},exists)}?avail:unavail)
> exten => _VMR_.,n(avail),Voicemail(${DID},u)
> exten => _VMR_.,n,Hangup()
> exten => _VMR_.,n(unavail),Playback(vm-theperson)
> exten => _VMR_.,n,SayDigits(${DID});
> exten => _VMR_.,n,Playback(vm-isunavail)
> exten => _VMR_.,n,Read(digit,vm-tocallback,1,,1,5)
> exten => _VMR_.,n,Gotoif($["${digit}" = "2"]?:skip,1,5)
> exten => _VMR_.,n,Noop("Add callback for ${DID} from ${CALLERID(num)}")
> exten => _VMR_.,n,AGI(callback,${DID},${CALLERID(num)})
> exten => _VMR_.,n,Playback(goodbye)
> exten => _VMR_.(skip),n,Hangup()
> when a vocal message asks to press "2" to add a callback when called
> users return free, using an AGI script that create a .call file:

> #!/usr/bin/php -q
> <?php
> ob_implicit_flush(true);
> set_time_limit(0);
> $called = $argv[1];
> $caller = $argv[2];
> $cf =
> fopen("/var/spool/asterisk/outgoing/cb".$called."-".$caller.".call","w+");
> fputs($cf,"Channel: LOCAL/CB_$called\n");
> fputs($cf,"Context: default\n");
> fputs($cf,"Extension: $caller\n");
> fputs($cf,"CallerID: CallBack $caller <$caller>\n");
> fputs($cf,"MaxRetries: 100\n");
> fputs($cf,"RetryTime: 30\n");
> fputs($cf,"Archive: Yes\n");
> fputs($cf,"SetVar: CALLER=$caller\n");
> fputs($cf,"SetVar: CALLED=$called\n");
> fclose($cf);
> ?>

0) This is not an AGI script. It does not read the AGI environment from 
STDIN and does not make any AGI requests. You could execute it using the 
system() application and it should execute the same -- maybe a couple of 
nanoseconds faster because Asterisk does not need to create the AGI 
environment or fiddle with file descriptors.

1) You should not create the call file in the spool directory. Doing so 
introduces a 'race condition' where Asterisk could start to read the file 
before your script is finished writing it. You should create the call file 
in another directory on the same file system and 'mv' it to the spool 
directory. /tmp/ or /var/tmp/ are usually suitable. ('mv' is 'atomic' -- 
it happens all at once.)

2) Visit http://www.voip-info.org/wiki/view/Asterisk+auto-dial+out to 
learn more about the call file format.

Think of call files in terms of legs. The first leg uses the 'channel' 
argument to originate the call. If that call is answered, 'leg 2' 
execution continues either in the dialplan at 'context:extension:priority' 
or the 'application:data' is executed.

Visit http://www.voip-info.org/wiki/view/Asterisk+local+channels to learn 
more about local channels. I think the syntax section will be most 

> I need that Asterisk call CALLED user and, when answered, start calling 

Yes, but the concept of 'answered' is vague if you are using analog 

Visit http://www.voip-info.org and search for 'asterisk call back' for 
examples of how others have approached this problem.

Thanks in advance,
Steve Edwards       sedwards at sedwards.com      Voice: +1-760-468-3867 PST

More information about the asterisk-users mailing list