3

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!

thevoipman
  • 1,773
  • 2
  • 17
  • 44

2 Answers2

7

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.

Chukwuemeka
  • 342
  • 6
  • 11
junky
  • 336
  • 2
  • 7
  • After many headaches I have now discovered that your get_var is actually get_variable. This has worked like a charm! I feel like i can take over the world with all these knowledge I'm learning! – thevoipman Dec 07 '12 at 11:38
  • ya, sorry, get_var was for get_variable, my bad. – junky Dec 07 '12 at 19:14
  • looks like this will only work if the call fails for whatever reason... I am also trying to get it to do an mysql update script while the call is in process/answered.... – thevoipman Dec 09 '12 at 04:05
  • Hi Junky, when using your suggestions, I don't think pulling the ip address of the remote caller is working. When not using GOTO method, I'm doing the below and it will give me the address just fine. Did the GOTO remove the IP or something? $callid = end(explode('@',$callid['data'])); – thevoipman Dec 11 '12 at 09:28
1

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.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • Thank you for this. Please allow me sometime to test this and get back to you. Thanks for your help. – thevoipman Sep 07 '12 at 09:36
  • Hi Sean - till now, I am still unable to understand your suggestions, I guess it's way beyond my knowledge.... I understood the suggestions above a little better, if you don't mind, please help me understand how I can utilize your suggestions better? – thevoipman Dec 09 '12 at 05:09
  • ,g flag does in fact work in AGIs, at least in asterisk 12 and 13. – Matt DiMeo May 27 '15 at 22:32
  • In some failover cases the 'g' not required. Good if you test both. – Yasiru G Mar 13 '18 at 06:26