0

I am Creating the call using the rest api-

try{
        // Initiate a new outbound call
        $call = $this->client->calls->create(

            // to call.
            "num2",

            // Step 5: Change the 'From' number below to be a valid Twilio number 
            // that you've purchased or verified with Twilio.
            "num1",


            array("url" => "url-tw",
            'IfMachine'=>'Continue')

        );
        echo "Started call: " . $call->sid;
    } catch(Exception $e){
        echo "Error: " . $e->getMessage();
    }

and on the url-tw what twiml should I use which can't disconnect the call.

Before I was handling the call using the TwiML but now I have to detect the AnsweredBy option which is only available if I make the call using the REST API so.

for now I m using the same twiml I have used before when I was making calls using the twiML like use the <Dial> which let to dial again but if I dont use any twiml it disconnect the call.So any advice where I m going wrong.

John Ambrose
  • 167
  • 1
  • 11

1 Answers1

0

Twilio evangelist here.

The value of the url parameter should be a publicly accessible URL that returns TwiML containing the instructions that you want Twilio to execute to the caller.

Your PHP to start the call would look like:

// Initiate a new outbound call
$call = $this->client->calls->create(
    "num2",
    "num1",
    array("url" => "http://example.com/answer.php", 'IfMachine'=>'Continue')
);

Then in answer.php, you can do two things:

  1. Check the AnsweredBy parameter to see if Twilio detected a machine or a human.
  2. Generate the Twiml you want to return based on that value

For example to say something different to a machine v a human, you could do something like this in your PHP:

<?php
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<?php if ($_REQUEST['AnsweredBy']=='machine'): ?>
    <Response>
        <Say>Hello machine</Say>
    </Response>
<?php else: ?>
    <Response>
        <Dial>+15555555555</Dial>
    </Response>
<?php endif ?>

Hope that helps.

Devin Rader
  • 10,260
  • 1
  • 20
  • 32
  • For now I m not doing anything on that url(answer.php).Now its and empty file and when I make call and receive the call then it is giving the message "We are sorry an application error has occurred good bye" – John Ambrose Feb 24 '17 at 05:15
  • I want the call to be go normal if the Answered by human. So what should I have to do for that. – the answer you gave when a human answer the it will hello human and disconnect.I want it to continue the call. – John Ambrose Feb 24 '17 at 12:15
  • Not sure what you mean "go normal". Can you describe a bit more how you want the call to proceed once Twilio tells you that it thinks a human has answered? – Devin Rader Feb 24 '17 at 13:22
  • like talk to the caller that I have called. – John Ambrose Feb 24 '17 at 13:23
  • Updated the answer to show using a instead of a . This tell Twilio to dial out to a second person and connect the two callers together letting them talk to each other. – Devin Rader Feb 24 '17 at 14:29
  • But I don't want to dial another number.I want to connect with number I dial from at the first place like if A dial B and B answer the call then I want A and B to be connected with each other talk to each other .I don't want to again dial a number to get connected with B. – John Ambrose Feb 24 '17 at 18:43
  • So when you start a phone call using the REST API what you are doing is asking Twilio "please start a phone call from you (where "you" is Twilio) to whatever phone number I've specified as the `To` number". Even if you specify a verified phone number as the `From` number if that API request, the call only looks like its coming from that number, but its actually Twilio on one end and a phone on the other. – Devin Rader Feb 24 '17 at 19:00
  • If the person that you've asked Twilio to call answers, then Twilio makes an HTTP request to the URL you specified in the API request to get instructions on what to do with that live call. If you want to let two human beings talk to each other, one way to do that is by using the `` verb which tells Twilio to start another outbound call to a different number. If this second person answers that second call, then Twilio will bridge both of those active calls together. – Devin Rader Feb 24 '17 at 19:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136575/discussion-between-john-ambrose-and-devin-rader). – John Ambrose Feb 24 '17 at 19:28
  • So u r saying that if u create a call A to a user and user pickup the call A and then I make another call B and the user who just answer the call A will wait until call B is answered? – John Ambrose Feb 24 '17 at 19:39
  • I am sorry but I dont see any logic in that.May be I am not getting how rest api call system works.As dont need do that in when we call using TwiML app. – John Ambrose Feb 25 '17 at 04:50
  • How did you do it with a TwiML app? Maybe I'm misunderstanding the scenario you're trying to build. – Devin Rader Feb 25 '17 at 13:48
  • When I was using he Twiml app I ha e pass the my twiml handler file url to the twiml app and use twiml app in my twillo number..I used the js function to make outgoing and Incoming calls. When I make outgoing call I have used the Twiml with dial verb and pass the number in dial very – John Ambrose Feb 25 '17 at 14:10
  • Right,so in that case you had two call legs 1) from client into twilio and 2) from twilio out to a phone number. In the new scenario your using the API to create those two legs 1) from twilio out to phone and 2) out from twilio out to client. If you want to bridge leg 1 & 2 depending on the value of AnsweredBy the only way to do that is how I've described. Make the outbound call to 1, test for answering machine, based on answeredby return a `` to tell Twilio to make the outbound call to 2. – Devin Rader Feb 25 '17 at 16:06
  • okay how would i disconnect the first call if human answer the call and I dial another number then how can i disconnect the first call. – John Ambrose Feb 27 '17 at 12:27
  • Sorry, I'm not following. Twilio dials Alice, checks to see if Alice or a machine answers, than if Alice answers dials Sam. Then did you want to programmatically disconnect from Alice or Sam? I thought you wanted Alice and Sam to be able to talk to each other? – Devin Rader Feb 27 '17 at 13:16
  • Thanks for your help I figure it out. – John Ambrose Mar 01 '17 at 13:20
  • Hi,Devin I found this on twilio docs-If Twilio detects a human has answered the call, then Twilio will make a request to your application URL with the 'AnsweredBy' parameter set to 'human' and the call flow will proceed as normal.So what it means by normal – John Ambrose Mar 04 '17 at 05:04