The basic lifecycle of a [successful] TaskRouter Task is as follows:
Task Created → eligible Worker becomes available → Worker reserved → Reservation accepted → Task assigned to Worker.
In this part of the tutorial, we'll create Tasks and observe them through each of these stages. We start by creating a Task using the Tasks REST API. First time around we accept the Task using the Reservations REST API, then we create another Task and accept it using assignment callback instructions.
Both the Reservations REST API and assignment callback instructions are valid methods for accepting a Reservation; it's likely that you'll choose one or the other based on the amount of background work that must be performed by your server before it accepts or rejects a Reservation. For example, due to the amount of time required, if you were to build a user interface that allowed human agents to inspect a Task before accepting it, you would need to accept the Reservation asynchronously using the Reservations REST API.
Whether we accept Reservations via the REST API or via assignment callback instructions, we always need an Assignment Callback URL that is reachable by TaskRouter. This is the URL at which TaskRouter will notify us when a Worker is reserved to perform a Task. Before creating any Tasks, let's get the Assignment Callback URL up and running.
Finally time to write some (albeit minimalist!) code.
We are going to write a C# server to respond to HTTP requests, so we can tell Twilio what to do when we receive an assignment callback.
1using System;2using System.Net;3using SimpleWebServer;45namespace taskroutercsharp6{7class MainClass8{9public static void Main (string[] args)10{11WebServer ws = new WebServer (SendResponse, "http://localhost:8080/");12ws.Run ();13Console.WriteLine ("A simple webserver. Press a key to quit.");14Console.ReadKey ();15ws.Stop ();16}1718public static HttpListenerResponse SendResponse(HttpListenerContext ctx)19{20HttpListenerRequest request = ctx.Request;21HttpListenerResponse response = ctx.Response;2223String endpoint = request.RawUrl;2425if (endpoint.EndsWith("assignment_callback")) {26response.StatusCode = (int) HttpStatusCode.OK;27response.ContentType = "application/json";28response.StatusDescription = "{}";29return response;30}31response.StatusCode = (int) HttpStatusCode.OK;32return response;33}34}35}
1using System;2using System.Net;3using System.Threading;4using System.Linq;5using System.Text;67namespace SimpleWebServer8{9public class WebServer10{11private readonly HttpListener _listener = new HttpListener();12private readonly Func<HttpListenerContext, HttpListenerResponse> _responderMethod;1314public WebServer(string[] prefixes, Func<HttpListenerContext, HttpListenerResponse> method)15{16if (!HttpListener.IsSupported)17throw new NotSupportedException(18"Needs Windows XP SP2, Server 2003 or later.");1920// URI prefixes are required, for example21// "http://localhost:8080/index/".22if (prefixes == null || prefixes.Length == 0)23throw new ArgumentException("prefixes");2425// A responder method is required26if (method == null)27throw new ArgumentException("method");2829foreach (string s in prefixes)3031_listener.Prefixes.Add(s);3233_responderMethod = method;34_listener.Start();35}3637public WebServer(Func<HttpListenerContext, HttpListenerResponse> method, params string[] prefixes)38: this(prefixes, method) { }3940public void Run()41{42ThreadPool.QueueUserWorkItem((o) =>43{44Console.WriteLine("Webserver running...");45try46{47while (_listener.IsListening)48{49ThreadPool.QueueUserWorkItem((c) =>50{51var ctx = c as HttpListenerContext;52try53{54HttpListenerResponse response = _responderMethod(ctx);55byte[] buf = Encoding.UTF8.GetBytes(response.StatusDescription);56response.ContentLength64 = buf.Length;57response.OutputStream.Write(buf, 0, buf.Length);58}59catch { } // suppress any exceptions60finally61{62// always close the stream63ctx.Response.OutputStream.Close();64}65}, _listener.GetContext());66}67}68catch { } // suppress any exceptions69});70}7172public void Stop()73{74_listener.Stop();75_listener.Close();76}77}78}
This returns an empty JSON document to TaskRouter with a 200 (OK) response code. This tells TaskRouter that the assignment callback was successfully received and parsed, but that we don't want to take any action on the Reservation right now. Instead, it's implied that we will use the REST API to accept or reject the Reservation when we are ready.
Make sure your C# server is running. Your server will be available here: http://localhost:8080
.
For this tutorial, we'll use ngrok. After installing ngrok to your $PATH, run the following command at your terminal to start listening for requests from the outside world:
ngrok 8080
Follow the instructions on this Twilio blog post on exposing your C# web service to the outside world.
Your local C# server is now accessible to anyone (including Twilio's servers) at your ngrok URL.
With those things working, edit your "Incoming Customer Care Requests" Workflow to point at the newly implemented Assignment Callback URL:
http://yourngrokserver.com/assignment_callback
Excellent. We're ready to create Tasks and accept Reservations.