Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page

Create Tasks from Phone Calls using TwiML: Receive an Incoming Call


We've seen how to create Tasks using the TaskRouter REST API and how to accept a Task Reservation using both the REST API and Assignment Callback instructions. TaskRouter also introduces new TwiML instructions that you can use to create a Task from a Twilio phone call.

To receive an incoming phone call, we first need a Twilio phone number. In this example we'll use a US toll-free number, but you can use a Voice capable number from any country.

Before purchasing or setting up the phone number, we need to create a PHP file to handle incoming calls. Create a new file called 'incoming-call.php' and add the code below.


incoming-call.php

incoming-callphp page anchor
1
<?php
2
header("Content-Type: application/xml; charset=utf-8");
3
?>
4
5
<Response>
6
<Gather action="enqueue-call.php" numDigits="1" timeout="5">
7
<Say language="es">Para Español oprime el uno.</Say>
8
<Say language="en">For English, please hold or press two.</Say>
9
</Gather>
10
</Response>

You can use the Buy Numbers(link takes you to an external page) section of the Twilio Voice and Messaging web portal to purchase a new phone number, or use an existing Twilio phone number. Open the phone number details page and point the Voice Request URL at your new incoming-call.php file:

TaskRouter Quickstart showing phone number properties and request URLs for voice and messaging.

Using any phone, call the Twilio number. You will be prompted to press one for Spanish or two for English. However, when you press a digit, you'll hear an error message. That's because our <Gather> verb is pointing to a second PHP file, 'enqueue-call.php', which we haven't implemented yet. In the next step we'll add the required file and use it to create a new Task based on the language selected by the caller.

Next: Create a Task using Enqueue »