Question

I'm trying to get my php-agi script to dial the next command if the first one is busy or fail in anyway. The way I set it up now won't just work, it just return busy and then died, or if it work, it would send two dial commands. Here's what I got:

$agi->exec('DIAL',"SIP/".$target."@".$ip.",30,g");
$agi->exec('DIAL',"SIP/".$target."@".$ip2.",30,g");

Any kind of help on this is greatly appreciated, thank you in advance!

Was it helpful?

Solution

When you call Dial() asterisk sets a channel variable called DIALSTATUS. You can read it from your AGI.

Output from "core show application Dial", from the CLI:

${DIALSTATUS}: This is the status of the call
CHANUNAVAIL
CONGESTION
NOANSWER
BUSY
ANSWER
CANCEL
DONTCALL: For the Privacy and Screening Modes. Will be set if the
called party chooses to send the calling party to the 'Go Away' script.
TORTURE: For the Privacy and Screening Modes. Will be set if the
called party chooses to send the calling party to the 'torture' script.
INVALIDARGS

Ex.

    $agi->exec('DIAL',"SIP/".$target."@".$ip.",30,g");
    $dialstatus = $agi->get_variable('DIALSTATUS');
    if ( $dialstatus != 'ANSWERED' ) {
      $agi->exec('DIAL',"SIP/".$target."@".$ip2.",30,g");
    }

So the logic is simply to do the call, only if the first call was not answered.

OTHER TIPS

The g flag to Dial says (it might be slightly different depending on the version of Asterisk you are using):

g: Proceed with dialplan execution at the next priority in the current extension if the destination channel hangs up.

The problem you are facing is that the Dial isn't being executed from dialplan, it's being executed by your AGI, so when Dial exits, that's the end of the story.

In order to make this work, I would do something like this...

First, open up extensions.conf and add something along these lines at the end (this is untested):

[failover]
exten => doit,1,Dial(SIP/${TARGET}@${IP1},30,g)
exten => doit,n,Dial(SIP/${TARGET}@${IP2},30,g)

Then, in your AGI:

// These set the necessary variables
$agi->set_variable('TARGET', $target);
$agi->set_variable('IP1', $ip);
$agi->set_variable('IP2', $ip2);

// And tell Asterisk where to go after the AGI exits
$agi->set_context('failover');
$agi->set_extension('doit');
$agi->set_priority(1);

exit(0);

Then fire up the Asterisk CLI and reload the dialplan:

user@host:~$ asterisk -r
*CLI> dialplan reload

Now when the AGI exits, it should drop to the failover context and do the Dial for you, falling through to the second Dial if the first fails for whatever reason.

If you need to do more processing after both Dial attempts, you will either have to do it in dialplan or you have to fire up another AGI after the fact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top