Skip to contentSkip to navigationSkip to topbar
Rate this page:
On 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.


Receive an Incoming Call

receive-an-incoming-call page anchor

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 add on to our Program.cs to handle incoming calls:

Program.cs

programcs page anchor

_88
using System;
_88
using System.Net;
_88
using SimpleWebServer;
_88
using Twilio;
_88
using Twilio.Rest.Taskrouter.V1.Workspace;
_88
using Twilio.Rest.Taskrouter.V1.Workspace.Task;
_88
using Twilio.TwiML;
_88
_88
namespace taskroutercsharp
_88
{
_88
class MainClass
_88
{
_88
// Find your Account Sid and Auth Token at twilio.com/user/account
_88
const string AccountSid = "{{ account_sid }}";
_88
const string AuthToken = "{{ auth_token }}";
_88
const string WorkspaceSid = "{{ workspace_sid }}";
_88
const string WorkflowSid = "{{ workflow_sid }}";
_88
_88
public static void Main(string[] args)
_88
{
_88
// Initialize the Twilio client
_88
TwilioClient.Init(AccountSid, AuthToken);
_88
_88
WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");
_88
ws.Run();
_88
Console.WriteLine("A simple webserver. Press a key to quit.");
_88
Console.ReadKey();
_88
ws.Stop();
_88
}
_88
_88
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
_88
{
_88
HttpListenerRequest request = ctx.Request;
_88
HttpListenerResponse response = ctx.Response;
_88
_88
String endpoint = request.RawUrl;
_88
_88
if (endpoint.EndsWith("assignment_callback"))
_88
{
_88
response.StatusCode = (int)HttpStatusCode.OK;
_88
response.ContentType = "application/json";
_88
response.StatusDescription = "{\"instruction\":\"accept\"}";
_88
return response;
_88
}
_88
else if (endpoint.EndsWith("create_task"))
_88
{
_88
response.StatusCode = (int)HttpStatusCode.OK;
_88
response.ContentType = "application/json";
_88
TaskResource task = TaskResource.Create(
_88
WorkspaceSid,
_88
attributes: "{\"selected_language\":\"es\"}",
_88
workflowSid: WorkflowSid);
_88
_88
response.StatusDescription = task.Attributes;
_88
return response;
_88
}
_88
else if (endpoint.EndsWith("accept_reservation"))
_88
{
_88
response.StatusCode = (int)HttpStatusCode.OK;
_88
response.ContentType = "application/json";
_88
var taskSid = request.QueryString["TaskSid"];
_88
var reservationSid = request.QueryString["ReservationSid"];
_88
ReservationResource reservation = ReservationResource.Update(
_88
WorkspaceSid,
_88
taskSid,
_88
reservationSid,
_88
ReservationResource.StatusEnum.Accepted);
_88
_88
response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";
_88
return response;
_88
}
_88
else if (endpoint.EndsWith("incoming_call"))
_88
{
_88
response.StatusCode = (int)HttpStatusCode.OK;
_88
response.ContentType = "application/xml";
_88
var twiml = new VoiceResponse();
_88
twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")
_88
.Say("Para Espanol oprima el uno.", language: "es" )
_88
.Say("For English, please hold or press two.", language: "en"));
_88
_88
response.StatusDescription = twiml.ToString();
_88
return response;
_88
}
_88
response.StatusCode = (int)HttpStatusCode.OK;
_88
return response;
_88
}
_88
}
_88
}

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 endpoint:

Phone Number Setup Details.

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 another endpoint, enqueue_call, which we haven't implemented yet. In the next step we'll add the required endpoint and use it to create a new Task based on the language selected by the caller.

Next: Create a Task using Enqueue »


Rate this page: