Skip to contentSkip to navigationSkip to topbar
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
1
using System;
2
using System.Net;
3
using SimpleWebServer;
4
using Twilio;
5
using Twilio.Rest.Taskrouter.V1.Workspace;
6
using Twilio.Rest.Taskrouter.V1.Workspace.Task;
7
using Twilio.TwiML;
8
9
namespace taskroutercsharp
10
{
11
class MainClass
12
{
13
// Find your Account Sid and Auth Token at twilio.com/user/account
14
const string AccountSid = "{{ account_sid }}";
15
const string AuthToken = "{{ auth_token }}";
16
const string WorkspaceSid = "{{ workspace_sid }}";
17
const string WorkflowSid = "{{ workflow_sid }}";
18
19
public static void Main(string[] args)
20
{
21
// Initialize the Twilio client
22
TwilioClient.Init(AccountSid, AuthToken);
23
24
WebServer ws = new WebServer(SendResponse, "http://localhost:8080/");
25
ws.Run();
26
Console.WriteLine("A simple webserver. Press a key to quit.");
27
Console.ReadKey();
28
ws.Stop();
29
}
30
31
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
32
{
33
HttpListenerRequest request = ctx.Request;
34
HttpListenerResponse response = ctx.Response;
35
36
String endpoint = request.RawUrl;
37
38
if (endpoint.EndsWith("assignment_callback"))
39
{
40
response.StatusCode = (int)HttpStatusCode.OK;
41
response.ContentType = "application/json";
42
response.StatusDescription = "{\"instruction\":\"accept\"}";
43
return response;
44
}
45
else if (endpoint.EndsWith("create_task"))
46
{
47
response.StatusCode = (int)HttpStatusCode.OK;
48
response.ContentType = "application/json";
49
TaskResource task = TaskResource.Create(
50
WorkspaceSid,
51
attributes: "{\"selected_language\":\"es\"}",
52
workflowSid: WorkflowSid);
53
54
response.StatusDescription = task.Attributes;
55
return response;
56
}
57
else if (endpoint.EndsWith("accept_reservation"))
58
{
59
response.StatusCode = (int)HttpStatusCode.OK;
60
response.ContentType = "application/json";
61
var taskSid = request.QueryString["TaskSid"];
62
var reservationSid = request.QueryString["ReservationSid"];
63
ReservationResource reservation = ReservationResource.Update(
64
WorkspaceSid,
65
taskSid,
66
reservationSid,
67
ReservationResource.StatusEnum.Accepted);
68
69
response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";
70
return response;
71
}
72
else if (endpoint.EndsWith("incoming_call"))
73
{
74
response.StatusCode = (int)HttpStatusCode.OK;
75
response.ContentType = "application/xml";
76
var twiml = new VoiceResponse();
77
twiml.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"));
80
81
response.StatusDescription = twiml.ToString();
82
return response;
83
}
84
response.StatusCode = (int)HttpStatusCode.OK;
85
return response;
86
}
87
}
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 »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.