Search code examples
twiliotwilio-phptwilio-apitwilio-click-to-call

when we make call using rest api then what twiml we use on that url parameter


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.


Solution

  • 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.