Skip to contentSkip to navigationSkip to topbar
On this page

Creating Tasks and Accepting Reservations: Set up the Assignment Callback URL


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.

(information)

Info

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.


Set up the Assignment Callback URL

set-up-the-assignment-callback-url page anchor

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.

Program.cs

programcs page anchor
1
using System;
2
using System.Net;
3
using SimpleWebServer;
4
5
namespace taskroutercsharp
6
{
7
class MainClass
8
{
9
public static void Main (string[] args)
10
{
11
WebServer ws = new WebServer (SendResponse, "http://localhost:8080/");
12
ws.Run ();
13
Console.WriteLine ("A simple webserver. Press a key to quit.");
14
Console.ReadKey ();
15
ws.Stop ();
16
}
17
18
public static HttpListenerResponse SendResponse(HttpListenerContext ctx)
19
{
20
HttpListenerRequest request = ctx.Request;
21
HttpListenerResponse response = ctx.Response;
22
23
String endpoint = request.RawUrl;
24
25
if (endpoint.EndsWith("assignment_callback")) {
26
response.StatusCode = (int) HttpStatusCode.OK;
27
response.ContentType = "application/json";
28
response.StatusDescription = "{}";
29
return response;
30
}
31
response.StatusCode = (int) HttpStatusCode.OK;
32
return response;
33
}
34
}
35
}
1
using System;
2
using System.Net;
3
using System.Threading;
4
using System.Linq;
5
using System.Text;
6
7
namespace SimpleWebServer
8
{
9
public class WebServer
10
{
11
private readonly HttpListener _listener = new HttpListener();
12
private readonly Func<HttpListenerContext, HttpListenerResponse> _responderMethod;
13
14
public WebServer(string[] prefixes, Func<HttpListenerContext, HttpListenerResponse> method)
15
{
16
if (!HttpListener.IsSupported)
17
throw new NotSupportedException(
18
"Needs Windows XP SP2, Server 2003 or later.");
19
20
// URI prefixes are required, for example
21
// "http://localhost:8080/index/".
22
if (prefixes == null || prefixes.Length == 0)
23
throw new ArgumentException("prefixes");
24
25
// A responder method is required
26
if (method == null)
27
throw new ArgumentException("method");
28
29
foreach (string s in prefixes)
30
31
_listener.Prefixes.Add(s);
32
33
_responderMethod = method;
34
_listener.Start();
35
}
36
37
public WebServer(Func<HttpListenerContext, HttpListenerResponse> method, params string[] prefixes)
38
: this(prefixes, method) { }
39
40
public void Run()
41
{
42
ThreadPool.QueueUserWorkItem((o) =>
43
{
44
Console.WriteLine("Webserver running...");
45
try
46
{
47
while (_listener.IsListening)
48
{
49
ThreadPool.QueueUserWorkItem((c) =>
50
{
51
var ctx = c as HttpListenerContext;
52
try
53
{
54
HttpListenerResponse response = _responderMethod(ctx);
55
byte[] buf = Encoding.UTF8.GetBytes(response.StatusDescription);
56
response.ContentLength64 = buf.Length;
57
response.OutputStream.Write(buf, 0, buf.Length);
58
}
59
catch { } // suppress any exceptions
60
finally
61
{
62
// always close the stream
63
ctx.Response.OutputStream.Close();
64
}
65
}, _listener.GetContext());
66
}
67
}
68
catch { } // suppress any exceptions
69
});
70
}
71
72
public 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(link takes you to an external page). After installing ngrok to your $PATH, run the following command at your terminal to start listening for requests from the outside world:

ngrok 8080

  • Note: Follow the instructions on this Twilio blog post(link takes you to an external page) 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.

Next: Create a Task using the REST API »

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.