Creating Tasks and Accepting Reservations: Accept a Reservation using Assignment Callback Instructions
To accept a Reservation automatically, return an accept assignment instruction in a synchronous HTTP response from your server when TaskRouter notifies it. In the previous step, you created a Task and accepted it using the Reservations subresource of the REST API. This time, you create another Task with the REST API and have your server accept the Reservation as soon as it is notified.
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:
1ATTRIBUTES_OBJ=$(cat << EOF2{3"selected_language": "es"4}5EOF6)7curl -X POST "https://taskrouter.twilio.com/v1/Workspaces/WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Tasks" \8--data-urlencode "Attributes=$ATTRIBUTES_OBJ" \9--data-urlencode "WorkflowSid=WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \10-u $TWILIO_API_KEY:$TWILIO_API_SECRET
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:
1using System;2using System.Net;3using SimpleWebServer;4using Twilio;5using Twilio.Rest.Taskrouter.V1.Workspace.Task;67namespace taskroutercsharp8{9class MainClass10{11// Find your Account SID: https://www.twilio.com/console12// Create API keys: https://www.twilio.com/docs/iam/api-keys/keys-in-console13const string AccountSid = "{{ account_sid }}";14const string ApiKey = "{{ api_key }}";15const string ApiSecret = "{{ api_secret }}";16const string WorkspaceSid = "{{ workspace_sid }}";17const string WorkflowSid = "{{ workflow_sid }}";1819public static void Main (string[] args)20{21TwilioClient.Init(ApiKey, ApiSecret, AccountSid);22WebServer ws = new WebServer (SendResponse, "http://localhost:8080/");23ws.Run ();24Console.WriteLine ("A simple webserver. Press a key to quit.");25Console.ReadKey ();26ws.Stop ();27}2829public static HttpListenerResponse SendResponse(HttpListenerContext ctx)30{31HttpListenerRequest request = ctx.Request;32HttpListenerResponse response = ctx.Response;3334String endpoint = request.RawUrl;3536if (endpoint.EndsWith("assignment_callback")) {37response.StatusCode = (int) HttpStatusCode.OK;38response.ContentType = "application/json";39response.StatusDescription = "{\"instruction\":\"accept\"}";40return response;41} else if (endpoint.EndsWith ("create_task")) {42response.StatusCode = (int)HttpStatusCode.OK;43response.ContentType = "application/json";44var task = TaskResource.Create(45WorkspaceSid, attributes: "{\"selected_language\":\"es\"}", workflowSid: WorkflowSid);46response.StatusDescription = task.Attributes;47return response;48} else if (endpoint.EndsWith ("accept_reservation")) {49response.StatusCode = (int)HttpStatusCode.OK;50response.ContentType = "application/json";51var taskSid = request.QueryString ["TaskSid"];52var reservationSid = request.QueryString ["ReservationSid"];53var reservation = ReservationResource.Update(54WorkspaceSid, taskSid, reservationSid,55ReservationResource.StatusEnum.Accepted);56response.StatusDescription = "{\"reservation_status\":\"" + reservation.ReservationStatus + "\"}";57return response;58}59response.StatusCode = (int) HttpStatusCode.OK;60return response;61}62}63}
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, 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':

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.

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.