Leo
Leo

Reputation: 930

How to offer a task to a specific worker on Twilio

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!

Upvotes: 3

Views: 1142

Answers (4)

AQuirky
AQuirky

Reputation: 5266

A much better solution to this problem than the accepted answer is to simply setup a dedicated workflow to forwarding tasks. Routing for such a workflow might look like this...

{
  "task_routing": {
    "filters": [
      {
        "filter_friendly_name": "forward-filter",
        "expression": "1==1",
        "targets": [
          {
            "queue": "WQxxxx",
            "known_worker_sid": "task.worker_sid"
          }
        ]
      },
    "default_filter": {
      "queue": "WQxxxx"
    }
  }
}

The queue, WQxxxx, targets all the workers to whom task can be forwarded. So in your case, you want to have 3 workflows, one that targets each of the 3 task queues you have setup.

The advantage of this approach is that you don't have to setup a new task queue for each worker.

So when you create the task and you want to assign it to a specific worker, then use the workflow for the specific queue you want to target and add the worker_sid for the user in the task attributes.

The downside if this approach is that you need to be sure that the worker can accept the reservation that will be created for him/her. If the reservation is rejected, then the same reservation will be created, over and over again as the reservation either times out or is rejected. You will not be able to redirect the task to another worker by changing the worker_sid in the task attributes. You will need to delete the task and start over.

Upvotes: 0

pwaterz
pwaterz

Reputation: 731

This also can be achieved by using known worker filter in your task workflow

Known Worker can be used if the Worker who should be targeted here is already known. Choose whether the Worker is identified by Worker friendly name or Worker SID. Provide the field in the Task attributes that holds the relevant information (e.g. “task.worker_friendly_name”).

enter image description here

Upvotes: 0

user3200478
user3200478

Reputation: 93

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

Upvotes: 0

philnash
philnash

Reputation: 73075

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.

Upvotes: 3

Related Questions