Control Worker Activities using Worker.js: Add an Agent UI to our Project

Let's get started on our agent UI. Assuming you've followed the conventions so far in this tutorial, the UI we create will be accessible using your web browser at:

http://localhost:8080/agents?WorkerSid=WK01234012340123401234 (substitute your Alice's WorkerSid)

We pass the WorkerSid in the URL to avoid implementing complex user management in our demo. In reality, you are likely to store a user's WorkerSid in your database alongside other User attributes.

Let's add on our to our run.py file to add an endpoint to generate a page based on a template.

run.py

# -*- coding: latin-1 -*-

from flask import Flask, request, Response, render_template
from twilio.rest import Client
from twilio.jwt.taskrouter.capabilities import WorkerCapabilityToken
from twilio.twiml.voice_response import VoiceResponse

app = Flask(__name__)

# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "{{ account_sid }}"
auth_token  = "{{ auth_token }}"
workspace_sid = "{{ workspace_sid }}"
workflow_sid = "{{ workflow_sid }}"

client = Client(account_sid, auth_token)

@app.route("/assignment_callback", methods=['GET', 'POST'])
def assignment_callback():
    """Respond to assignment callbacks with an acceptance and 200 response"""

    ret = '{"instruction": "dequeue", "from"="+15556667777"}' # a verified phone number from your twilio account
    resp = Response(response=ret, status=200, mimetype='application/json')
    return resp

@app.route("/create_task", methods=['GET', 'POST'])
def create_task():
    """Creating a Task"""
    task = client.taskrouter.workspaces(workspace_sid) \
                 .tasks.create(workflow_sid=workflow_sid, attributes='{"type":"support"}')

    print(task.attributes)
    resp = Response({}, status=200, mimetype='application/json')
    return resp

@app.route("/accept_reservation", methods=['GET', 'POST'])
def accept_reservation():
    """Accepting a Reservation"""
    task_sid = request.args.get('task_sid')
    reservation_sid = request.args.get('reservation_sid')

    reservation = client.taskrouter.workspaces(workspace_sid) \
                        .tasks(task_sid) \
                        .reservations(reservation_sid) \
                        .update(reservation_status='accepted')

    print(reservation.reservation_status)
    print(reservation.worker_name)

    resp = Response({}, status=200, mimetype='application/json')
    return resp

@app.route("/incoming_call", methods=['GET', 'POST'])
def incoming_call():
    """Respond to incoming requests."""

    resp = VoiceResponse()
    with resp.gather(numDigits=1, action="/enqueue_call", method="POST", timeout=5) as g:
        g.say("Para Español oprime el uno.".decode("utf8"), language='es')
        g.say("For English, please hold or press two.", language='en')

    return str(resp)

@app.route("/enqueue_call", methods=['GET', 'POST'])
def enqueue_call():
    digit_pressed = request.args.get('Digits')
    if digit_pressed == 1 :
        language = "es"
    else:
        language = "en"

    resp = VoiceResponse()
    with resp.enqueue(None, workflowSid=workflow_sid) as e:
        e.task('{"selected_language": language}')

    return str(resp)

@app.route("/agents", methods=['GET'])
def generate_view():
    worker_sid = request.args.get('WorkerSid')

    worker_capability = WorkerCapabilityToken(account_sid, auth_token, workspace_sid, worker_sid)
    worker_capability.allow_activity_updates()
    worker_capability.allow_reservation_updates()

    worker_token = worker_capability.to_jwt()

    return render_template('agent.html', worker_token=worker_token)

if __name__ == "__main__":
    app.run(debug=True)

Now create a folder called templates. Inside that folder, create a template file that will be rendered when the URL is requested:

templates/agent.html

<!DOCTYPE html>
<html>
<head>
    <title>Customer Care - Voice Agent Screen</title>
    <link rel="stylesheet" href="//media.twiliocdn.com/taskrouter/quickstart/agent.css"/>
    <script src="//media.twiliocdn.com/taskrouter/js/v1.4/taskrouter.min.js"></script>
    <script type="text/javascript">
        /* Subscribe to a subset of the available TaskRouter.js events for a worker */
        function registerTaskRouterCallbacks() {
            worker.on('ready', function(worker) {
                agentActivityChanged(worker.activityName);
                logger("Successfully registered as: " + worker.friendlyName)
                logger("Current activity is: " + worker.activityName);
            });

            worker.on('activity.update', function(worker) {
                agentActivityChanged(worker.activityName);
                logger("Worker activity changed to: " + worker.activityName);
            });

            worker.on("reservation.created", function(reservation) {
                logger("-----");
                logger("You have been reserved to handle a call!");
                logger("Call from: " + reservation.task.attributes.from);
                logger("Selected language: " + reservation.task.attributes.selected_language);
                logger("-----");
            });

            worker.on("reservation.accepted", function(reservation) {
                logger("Reservation " + reservation.sid + " accepted!");
            });

            worker.on("reservation.rejected", function(reservation) {
                logger("Reservation " + reservation.sid + " rejected!");
            });

            worker.on("reservation.timeout", function(reservation) {
                logger("Reservation " + reservation.sid + " timed out!");
            });

            worker.on("reservation.canceled", function(reservation) {
                logger("Reservation " + reservation.sid + " canceled!");
            });
        }

        /* Hook up the agent Activity buttons to TaskRouter.js */

        function bindAgentActivityButtons() {
            // Fetch the full list of available Activities from TaskRouter. Store each
            // ActivitySid against the matching Friendly Name
            var activitySids = {};
            worker.activities.fetch(function(error, activityList) {
                var activities = activityList.data;
                var i = activities.length;
                while (i--) {
                    activitySids[activities[i].friendlyName] = activities[i].sid;
                }
            });

            /* For each button of class 'change-activity' in our Agent UI, look up the
             ActivitySid corresponding to the Friendly Name in the button’s next-activity
             data attribute. Use Worker.js to transition the agent to that ActivitySid
             when the button is clicked.*/
            var elements = document.getElementsByClassName('change-activity');
            var i = elements.length;
            while (i--) {
                elements[i].onclick = function() {
                    var nextActivity = this.dataset.nextActivity;
                    var nextActivitySid = activitySids[nextActivity];
                    worker.updateActivity(nextActivitySid);
                }
            }
        }

        /* Update the UI to reflect a change in Activity */

        function agentActivityChanged(activity) {
            hideAgentActivities();
            showAgentActivity(activity);
        }

        function hideAgentActivities() {
            var elements = document.getElementsByClassName('agent-activity');
            var i = elements.length;
            while (i--) {
                elements[i].style.display = 'none';
            }
        }

        function showAgentActivity(activity) {
            activity = activity.toLowerCase();
            var elements = document.getElementsByClassName(('agent-activity ' + activity));
            elements.item(0).style.display = 'block';
        }

        /* Other stuff */

        function logger(message) {
            var log = document.getElementById('log');
            log.value += "\n> " + message;
            log.scrollTop = log.scrollHeight;
        }

        window.onload = function() {
            // Initialize TaskRouter.js on page load using window.workerToken -
            // a Twilio Capability token that was set from rendering the template with agents endpoint
            logger("Initializing...");
            window.worker = new Twilio.TaskRouter.Worker("{{ worker_token }}");

            registerTaskRouterCallbacks();
            bindAgentActivityButtons();
        };
    </script>
</head>
<body>
<div class="content">
    <section class="agent-activity offline">
        <p class="activity">Offline</p>
        <button class="change-activity" data-next-activity="Idle">Go Available</button>
    </section>
    <section class="agent-activity idle">
        <p class="activity"><span>Available</span></p>
        <button class="change-activity" data-next-activity="Offline">Go Offline</button>
    </section>
    <section class="agent-activity reserved">
        <p class="activity">Reserved</p>
    </section>
    <section class="agent-activity busy">
        <p class="activity">Busy</p>
    </section>
    <section class="agent-activity wrapup">
        <p class="activity">Wrap-Up</p>
        <button class="change-activity" data-next-activity="Idle">Go Available</button>
        <button class="change-activity" data-next-activity="Offline">Go Offline</button>
    </section>
    <section class="log">
      <textarea id="log" readonly="true"></textarea>
    </section>
</div>
</body>
</html>

You'll notice that we included two external files:

  • taskrouter.min.js is the primary TaskRouter.js JavaScript file that communicates with TaskRouter's infrastructure on our behalf. You can use this URL to include Worker.js in your production application, but first check the reference documentation to ensure that you include the latest version number.
  • agent.css is a simple CSS file created for the purpose of this Quickstart. It saves us having to type out some simple pre-defined styles.

And that's it! Open http://localhost:8080/agents?WorkerSid=WK012340123401234 in your browser and you should see the screen below. If you make the same phone call as we made in Part 3, you should see Alice's Activity transition on screen as she is reserved and assigned to handle the Task.

If you see "Initializing..." and no progress, make sure that you have included the correct WorkerSid in the "WorkerSid" request parameter of the URL.

For more details, refer to the TaskRouter JavaScript SDK documentation.

Completed Agent UI

  • This simple PoC has been tested in the latest version of popular browsers, including IE 11. *

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.