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 and Auth Token at twilio.com/user/account14const string AccountSid = "{{ account_sid }}";15const string AuthToken = "{{ auth_token }}";16const string WorkspaceSid = "{{ workspace_sid }}";17const string WorkflowSid = "{{ workflow_sid }}";1819public static void Main(string[] args)20{21// Initialize the Twilio client22TwilioClient.Init(AccountSid, AuthToken);2324WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");25ws.Run();26Console.WriteLine("A simple webserver. Press a key to quit.");27Console.ReadKey();28ws.Stop();29}3031public static HttpListenerResponse SendResponse(HttpListenerContext ctx)32{33HttpListenerRequest request = ctx.Request;34HttpListenerResponse response = ctx.Response;3536String endpoint = request.RawUrl;3738if (endpoint.EndsWith("assignment_callback"))39{40response.StatusCode = (int)HttpStatusCode.OK;41response.ContentType = "application/json";42response.StatusDescription = "{\"instruction\":\"accept\"}";43return response;44}45else if (endpoint.EndsWith("create_task"))46{47response.StatusCode = (int)HttpStatusCode.OK;48response.ContentType = "application/json";49TaskResource task = TaskResource.Create(50WorkspaceSid,51attributes: "{\"selected_language\":\"es\"}",52workflowSid: WorkflowSid);5354response.StatusDescription = task.Attributes;55return response;56}57else if (endpoint.EndsWith("accept_reservation"))58{59response.StatusCode = (int)HttpStatusCode.OK;60response.ContentType = "application/json";61var taskSid = request.QueryString["TaskSid"];62var reservationSid = request.QueryString["ReservationSid"];63ReservationResource reservation = ReservationResource.Update(64WorkspaceSid,65taskSid,66reservationSid,67ReservationResource.StatusEnum.Accepted);6869response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";70return response;71}72else if (endpoint.EndsWith("incoming_call"))73{74response.StatusCode = (int)HttpStatusCode.OK;75response.ContentType = "application/xml";76var twiml = new VoiceResponse();77twiml.Gather(new Gather(numDigits: 1, action: "enqueue_call")78.Say("Para Espanol oprima el uno.", language: "es" )79.Say("For English, please hold or press two.", language: "en"));8081response.StatusDescription = twiml.ToString();82return response;83}84response.StatusCode = (int)HttpStatusCode.OK;85return response;86}87}88}
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.