2
votes

I'm using Twilio's TasksRouter.

I have 3 TaskQueues in my workspace and new tasks are being forwarded to the correct TaskQueue but I would also like them to be offered to a specific worker in that queue.

I thought that the task attributes are supposed to do that but even when my task has attributes such as {user_id: 123} the task is still offered to a worker with attributes like {user_id: 999}

Is that the correct approach? How else can I offer a task to a specific worker?

Thanks!

2
Hey Leo, have you put a condition in your workflow to assign tasks based on a user_id attribute? - philnash
I totally missed that! Thanks a lot for pointing this out. Please submit an answer if you'd like - I'll mark it accepted. - Leo

2 Answers

3
votes

Twilio developer evangelist here.

In order to send a task to a specific worker you need to set up a condition in your TaskRouter workflow based on the user_id attribute that you have given the task. You can set it to match the attribute and direct the task to a queue that is manned by just that worker.

0
votes

I did it by using the reservation.created event. I use the standard workflow and task queue and then programatically reject all reservations until the reservation is done with the right agent, then I accept it.

In the TaskRouter UI in the Twilkio console I added a webhook on the reservation.created event, pointing to my server. The request is then handles as follows:

@app.route('/hook/reservation', methods=['POST'])
def fn_th_reservation():

    task_attributes = json.loads(request.form['TaskAttributes'])
    channel_sid = task_attributes["channelSid"]
    worker_sid = request.form['WorkerSid']
    reservation_sid = request.form["ReservationSid"]
    workspace_sid = request.form["WorkspaceSid"]
    task_sid = request.form["TaskSid"]

    # implement app specific logic here. you can use channel_sid and
    # worker_sid to compare them to a mapping from you database for instance
    is_right_worker = ...

    reservation_status = 'accepted' if is_right_worker else 'rejected'

    client = Client(account_sid, auth_token)
    # accept or reject reservation
    reservation = client.taskrouter.workspaces(workspace_sid) \
        .tasks(task_sid).reservations(reservation_sid) \
        .update(reservation_status=reservation_status)

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

    return('200')

just make sure that you don't create an inifinite loop by rejecting every worker