Get Started

TwiML Quickstart: Hello Monkey

Greet Caller by Name

Ok, that was fun. Seriously, it was. But let's raise the stakes a bit. When someone calls, we are going to try to greet the caller by name:

<?php

    // make an associative array of callers we know, indexed by phone number
    $people = array(
        "+14158675309"=>"Curious George",
        "+14158675310"=>"Boots",
        "+14158675311"=>"Virgil",
        "+14158675312"=>"Marcel"
    );

    // if the caller is known, then greet them by name
    // otherwise, consider them just another monkey
    if(!$name = $people[$_REQUEST['From']])
        $name = "Monkey";

    // now greet the caller
    header("content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
    <Say>Hello <?php echo $name ?>.</Say>
</Response>

In this example, the code inspects the From parameter passed by Twilio in the request to get the Caller ID of the caller. We then check to see if the caller is known looking up the number in the $people array to extract their name. We then Say their name or the word 'Monkey' if their Caller ID isn't known.

If you add your phone number and name to the $people array you can listen to Twilio greet you by name. Note that the phone numbers in the $people array have +1 prepended to them. '1' is the international country code for the US and Canada and the '+' formats the number in E.164 format which is used by Twilio when passing you phone numbers.


Next: Play mp3 for Caller »