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 add on to our Program.cs to handle incoming calls:
1using System;2using System.Net;3using SimpleWebServer;4using Twilio;5using Twilio.Rest.Taskrouter.V1.Workspace;6using Twilio.Rest.Taskrouter.V1.Workspace.Task;7using Twilio.TwiML;89namespace taskroutercsharp10{11class MainClass12{13// Find your Account SID at twilio.com/console14// Provision API Keys at twilio.com/console/runtime/api-keys15const string AccountSid = "{{ account_sid }}";16const string ApiKey = "{{ api_key }}";17const string ApiSecret = "{{ api_secret }}";18const string WorkspaceSid = "{{ workspace_sid }}";19const string WorkflowSid = "{{ workflow_sid }}";2021public static void Main(string[] args)22{23// Initialize the Twilio client24TwilioClient.Init(ApiKey, ApiSecret, AccountSid);2526WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");27ws.Run();28Console.WriteLine("A simple webserver. Press a key to quit.");29Console.ReadKey();30ws.Stop();31}3233public static HttpListenerResponse SendResponse(HttpListenerContext ctx)34{35HttpListenerRequest request = ctx.Request;36HttpListenerResponse response = ctx.Response;3738String endpoint = request.RawUrl;3940if (endpoint.EndsWith("assignment_callback"))41{42response.StatusCode = (int)HttpStatusCode.OK;43response.ContentType = "application/json";44response.StatusDescription = "{\"instruction\":\"accept\"}";45return response;46}47else if (endpoint.EndsWith("create_task"))48{49response.StatusCode = (int)HttpStatusCode.OK;50response.ContentType = "application/json";51TaskResource task = TaskResource.Create(52WorkspaceSid,53attributes: "{\"selected_language\":\"es\"}",54workflowSid: WorkflowSid);5556response.StatusDescription = task.Attributes;57return response;58}59else if (endpoint.EndsWith("accept_reservation"))60{61response.StatusCode = (int)HttpStatusCode.OK;62response.ContentType = "application/json";63var taskSid = request.QueryString["TaskSid"];64var reservationSid = request.QueryString["ReservationSid"];65ReservationResource reservation = ReservationResource.Update(66WorkspaceSid,67taskSid,68reservationSid,69ReservationResource.StatusEnum.Accepted);7071response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";72return response;73}74else if (endpoint.EndsWith("incoming_call"))75{76response.StatusCode = (int)HttpStatusCode.OK;77response.ContentType = "application/xml";78var twiml = new VoiceResponse();79twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")80.Say("Para Espanol oprima el uno.", language: "es" )81.Say("For English, please hold or press two.", language: "en"));8283response.StatusDescription = twiml.ToString();84return response;85}86response.StatusCode = (int)HttpStatusCode.OK;87return response;88}89}90}
You can use the Buy Numbers 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:

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.