Click-to-Call Avoid Making Second Call if First Fails

I worked with the PHP from the end of this post and got it working: How to make a click to call?

    $key = $_REQUEST['key'];
    $pass = "secret";

    if($key == $pass){
        #ip address that asterisk is on.
        $strHost = "";
        $strUser = "webcall";#specify the asterisk manager username you want to login with
        $strSecret = "c5wk8zdkvn3ck5av";#specify the password for the above user
        #specify the channel (extension) you want to receive the call requests with
        #e.g. SIP/XXX, IAX2/XXXX, ZAP/XXXX, etc
        # $strChannel = "SIP/100";
        $strChannel = $_REQUEST['exten'];
        $strContext = "default";
        #specify the amount of time you want to try calling the specified channel before hanging up
        $strWaitTime = "30";
        #specify the priority you wish to place on making this call
        $strPriority = "1";
        #specify the maximum amount of retries
        $strMaxRetry = "0";
        $pos=strpos ($number,"local");
        if ($number == null) :
        exit() ;
        endif ;
        if ($pos===false) :
        $errno=0 ;
        $errstr=0 ;
        $strCallerId = $_REQUEST['cid'];
        $oSocket = fsockopen("localhost", 5038, $errno, $errstr, 20);
        if (!$oSocket) {
        echo "There was a problem<br>\n";
        echo "$errstr ($errno)<br>\n";
        } else {

            echo "Attempting to start<br>\n";
            fputs($oSocket, "Action: login\r\n");
            fputs($oSocket, "Events: off\r\n");
            fputs($oSocket, "Username: $strUser\r\n");
            fputs($oSocket, "Secret: $strSecret\r\n\r\n");
            fputs($oSocket, "Action: originate\r\n");
            fputs($oSocket, "Channel: $strChannel\r\n");
            fputs($oSocket, "WaitTime: $strWaitTime\r\n");
            fputs($oSocket, "CallerId: $strCallerId\r\n");
            fputs($oSocket, "Exten: $number\r\n");
            fputs($oSocket, "Context: $strContext\r\n");
            fputs($oSocket, "Priority: $strPriority\r\n\r\n");
            fputs($oSocket, "Action: Logoff\r\n\r\n");
        echo "Extension $strChannel should be calling $number.";
        else :
        exit() ;
        endif ;

      echo "BAD : login details incorrect";


The issue I have is that when I hit the end point, it calls the first number correctly, waits to call the second till the first is answered, but the issue is that if the first caller doesn’t answer, it still calls the second number even though there isn’t anyone on the other line.

In my case, I was testing it, and when it called my first phone, I rejected the call, which sent it to voicemail and caused the second call to be made right away.

How can I run it through the AMI or something to detect if a person answers the first call before placing the second? I see it is using the “default” context, but there isn’t really anything in there that seems to be modifiable to do what I am thinking. Honestly I am surprised it works at all when looking at the “default” dialplan.


For Asterisk this call is answered and hence it tried dialing the second leg. You can play with AMD application to detect answering machine services.

Thank you, I am just not sure how to do that. I created a new context here:

	exten => _1NXXNXXXXXX,n,AMD()
	exten => _1NXXNXXXXXX,n,NoOp(${AMDSTATUS})

When looking at the events in the console, I see the following:

  == Manager 'webcall' logged on from
  == Using SIP RTP CoS mark 5
    -- Called
       > 0x7f420c005990 -- Probation passed - setting RTP source address to
    -- SIP/ is making progress
       > 0x7f420c005990 -- Probation passed - setting RTP source address to
    -- SIP/ answered
    -- Executing [14805551111@webcaller:1] Dial("SIP/", "SIP/") in new stack
  == Manager 'webcall' logged off from
  == Using SIP RTP CoS mark 5
    -- Called SIP/
    -- SIP/ answered SIP/
    -- Channel SIP/ joined 'simple_bridge' basic-bridge <78e476e4-702a-4efe-8b70-9dac65724757>
    -- Channel SIP/ joined 'simple_bridge' basic-bridge <78e476e4-702a-4efe-8b70-9dac65724757>
       > Bridge 78e476e4-702a-4efe-8b70-9dac65724757: switching from simple_bridge technology to native_rtp
       > Remotely bridged 'SIP/' and 'SIP/' - media will flow directly between them
    -- Channel SIP/ left 'native_rtp' basic-bridge <78e476e4-702a-4efe-8b70-9dac65724757>
    -- Channel SIP/ left 'native_rtp' basic-bridge <78e476e4-702a-4efe-8b70-9dac65724757>
  == Spawn extension (webcaller, 18015157770, 1) exited non-zero on 'SIP/'

From the moment I picked up the first phone, it dialed the next line, I am not sure where to set a branch point between those two events to process my own set of instructions.

Here is my solution. It turns out that the first call is made before it goes into the context that was declared. So adding the AMD stuff at the top of that and having it call a second context was the solution for me.


	exten => _1NXXNXXXXXX,1,AMD()
	exten => _1NXXNXXXXXX,n,NoOp(${AMDSTATUS})
    exten => _1NXXNXXXXXX,n,GotoIf($[${AMDSTATUS}=HUMAN]?bridge-dial,${EXTEN},1)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.