Skip to contentSkip to navigationSkip to topbar
On this page

Creating Tasks and Accepting Reservations: Accept a Reservation using Assignment Callback Instructions


Remember when we created a Task and accepted it using the Reservations subresource of the REST API? I do. And it was grand.

This time, we'll create another Task, again using the REST API, but we will have our server accept the Reservation as soon as it is notified, via a synchronous HTTP response.

Before we create the next Task, once again make sure that our Worker Alice is in a non-available Activity state.

Call the Create Task endpoint exposed with Program.cs again, or execute the following curl command:

1
curl https://taskrouter.twilio.com/v1/Workspaces/{WorkspaceSid}/Tasks \
2
--data-urlencode Attributes='{"selected_language": "es"}' \
3
-d WorkflowSid={WorkflowSid} \
4
-u {AccountSid}:{AuthToken}

This time, before bringing Alice online, we need to make changes to our assignment_callback method in our Program.cs. Open it and modify the existing code to reflect the following:


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.Task;
6
7
namespace taskroutercsharp
8
{
9
class MainClass
10
{
11
// Find your Account Sid and Auth Token at twilio.com/user/account
12
const string AccountSid = "{{ account_sid }}";
13
const string AuthToken = "{{ auth_token }}";
14
const string WorkspaceSid = "{{ workspace_sid }}";
15
const string WorkflowSid = "{{ workflow_sid }}";
16
17
public static void Main (string[] args)
18
{
19
TwilioClient.Init(accountSid, authToken);
20
WebServer ws = new WebServer (SendResponse, "http://localhost:8080/");
21
ws.Run ();
22
Console.WriteLine ("A simple webserver. Press a key to quit.");
23
Console.ReadKey ();
24
ws.Stop ();
25
}
26
27
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
28
{
29
HttpListenerRequest request = ctx.Request;
30
HttpListenerResponse response = ctx.Response;
31
32
String endpoint = request.RawUrl;
33
34
if (endpoint.EndsWith("assignment_callback")) {
35
response.StatusCode = (int) HttpStatusCode.OK;
36
response.ContentType = "application/json";
37
response.StatusDescription = "{\"instruction\":\"accept\"}";
38
return response;
39
} else if (endpoint.EndsWith ("create_task")) {
40
response.StatusCode = (int)HttpStatusCode.OK;
41
response.ContentType = "application/json";
42
var task = TaskResource.Create(
43
WorkspaceSid, attributes: "{\"selected_language\":\"es\"}", workflowSid: WorkflowSid);
44
response.StatusDescription = task.Attributes;
45
return response;
46
} else if (endpoint.EndsWith ("accept_reservation")) {
47
response.StatusCode = (int)HttpStatusCode.OK;
48
response.ContentType = "application/json";
49
var taskSid = request.QueryString ["TaskSid"];
50
var reservationSid = request.QueryString ["ReservationSid"];
51
var reservation = ReservationResource.Update(
52
WorkspaceSid, taskSid, reservationSid,
53
ReservationResource.StatusEnum.Accepted);
54
response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";
55
return response;
56
}
57
response.StatusCode = (int) HttpStatusCode.OK;
58
return response;
59
}
60
}
61
}

Instead of returning an empty JSON document as before, we've included an 'assignment instruction' in our response. The 'accept' assignment instruction tells TaskRouter to automatically accept the Reservation and assign the Task to the Worker it has been reserved for.

To kick this process off, we need to transition Alice to an available Activity. With your Workspace open in the TaskRouter web portal(link takes you to an external page), click 'Workers' then click to edit Alice and set her Activity to 'Idle'.

Now, click 'Tasks' in the main navigation and you should see that the Task has an Assignment Status of 'assigned':

Task is Assigned to Alice.

What actually happened is that Alice was reserved for a very short period of time. TaskRouter made a request to your web server at the Assignment Callback URL, and your server told TaskRouter to accept the Reservation. At that point, Alice's Activity transitioned to the 'Assignment Activity' of the TaskQueue that assigned the Task, as it did in the previous step.

Alice is now Busy.

And that's that. We created another Task using the REST API, accepted it via an assignment instruction at our Workflow's Assignment Callback URL and saw that this immediately accepted the Reservation for our Worker.

Onward! Next we learn about shortcuts to create Tasks originating from Twilio phone calls.

Part 3: Create Tasks from Phone Calls using TwiML »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.