| [ Web Proxy ] |
| Viewing: https://www.twilio.com/docs/taskrouter/twiml-queue-calls | [Back] [Original] |
Twilio's programmable voice <Queue> noun provides a simple mechanism to park calls on hold and place them in a named First-In, First-Out (FIFO) queue. By combining <Queue> with TaskRouter, <Queue> can be extended with richer routing models. TaskRouter's agent availability model and Task reservation/acceptance flow can automatically push relevant work to agents as they become available.
Want to use TaskRouter and TwiML to implement call routing? Try the TaskRouter Quickstart for an easy introduction.
Not familiar with TwiML voice applications? Read about Twilio's TwiML API.
Voice tasks or calls are limited to 100 by default per queue.
To find the call queue that corresponds with your Flex Task Assignment workflow, list your account's queue resources(link takes you to an external page) filtered by FriendlyName, or your workflow Sid (prefixed by WW). For example:
1curl 'https://api.twilio.com/2010-04-01/Accounts/ACxxx/Queues.json' -X GET \2--data-urlencode 'FriendlyName=WWyyy' \3-u ACxxx:[AuthToken]
Using a Queue Sid (prefixed by QU) from the Programmable Voice API response, you can change the maximum number of calls for that queue by updating the queue resource(link takes you to an external page) and specifying a different MaxSize. For example:
1curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Queues/QUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json" \2--data-urlencode "FriendlyName=WWyyy" \3--data-urlencode "MaxSize=200" \4-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
Using TaskRouter with TwiML calls follows the basic flow:
<Enqueue> with attributes indicating which TaskRouter WorkflowSid should control call routing, and any Task attributes you would like to attach to the call.<Enqueue> to route calls with TaskRouterBelow are some example TwiML documents that take advantage of TaskRouter for automatic call distribution.
To use Enqueue with TaskRouter, specify WorkflowSid when you generate the <Enqueue> statement in your TwiML document. Omit the named queue in the element body. The following example TwiML document will create a task in the provided Workflow and then put the caller into the default hold music:
1// Download the Node helper library from twilio.com/docs/node/install2// These consts are your accountSid and authToken from https://www.twilio.com/console3const VoiceResponse = require('twilio').twiml.VoiceResponse;4const express = require('express');5const app = express();67app.post('/enqueue_call', (request, response) => {8const resp = new VoiceResponse();9resp.enqueue({ workflowSid: 'WW0123456789abcdef0123456789abcdef' });1011response.setHeader('Content-Type', 'application/xml');12response.write(resp.toString());13response.end();14});1516app.listen(8080);
A task will be created in this Workflow with the call's standard parameters as attributes (call_sid, account_sid, to, from, etc).
You may optionally also include a <Task> noun element to add additional attributes to the Task. <Task> must contain a valid JSON object as its body for the attributes of the Task. For example:
1// Download the Node helper library from twilio.com/docs/node/install2// These consts are your accountSid and authToken from https://www.twilio.com/console3const VoiceResponse = require('twilio').twiml.VoiceResponse;4const express = require('express');5const app = express();67app.post('/enqueue_call', (request, response) => {8const resp = new VoiceResponse();910const json = { account_number: '12345abcdef' };1112resp.enqueue('Friendly Name').task({}, JSON.stringify(json));1314response.setHeader('Content-Type', 'application/xml');15response.write(resp.toString());16response.end();17});1819app.listen(8080);
You may optionally provide priority and/or timeout for a Task. This example provides a dynamic priority and timeout to a task as a property of the <Task> noun element. Priority and Timeout both must be valid integers. For example:
1// Download the Node helper library from twilio.com/docs/node/install2// These consts are your accountSid and authToken from https://www.twilio.com/console3const VoiceResponse = require('twilio').twiml.VoiceResponse;4const express = require('express');5const app = express();67app.post('/enqueue_call', (request, response) => {8const resp = new VoiceResponse();910const json = { account_number: '12345abcdef' };1112resp13.enqueue({14workflowSid: 'WW0123456789abcdef0123456789abcdef',15})16.task(17{18priority: '5',19timeout: '200',20},21JSON.stringify(json)22);2324response.setHeader('Content-Type', 'application/xml');25response.write(resp.toString());26response.end();27});2829app.listen(8080);
This example uses the waitUrl property to provide custom hold music to the caller, and the action property to specify a post-call survey application that will be executed after the Worker and caller disconnect from one another. For more details, see the action attribute of <Enqueue>.
1// Download the Node helper library from twilio.com/docs/node/install2// These consts are your accountSid and authToken from https://www.twilio.com/console3const VoiceResponse = require('twilio').twiml.VoiceResponse;4const express = require('express');5const app = express();67app.post('/enqueue_call', (request, response) => {8const resp = new VoiceResponse();910const json = { account_number: '12345abcdef' };1112resp13.enqueue({14workflowSid: 'WW0123456789abcdef0123456789abcdef',15waitUrl: '/hold_music.php',16action: '/post_bridge_survey.php',17})18.task({}, JSON.stringify(json));1920response.setHeader('Content-Type', 'application/xml');21response.write(resp.toString());22response.end();23});2425app.listen(8080);
Note: TaskAttributes is a legacy verb that is still supported as an alternative to Task.
Once a Worker has been reserved to handle a call, your application will receive an HTTP request at the AssignmentCallbackURL specified by the WorkflowSid you used in the <Enqueue> statement. Your application can respond with one of the following examples to control how the call is actually bridged to the Worker. Read about other options for handling the Assignment Callback here.
The 'dequeue' instruction removes the call from hold and bridges it to the selected Worker. To use the 'dequeue' instruction, your application should reply to the Assignment Callback request with a JSON document containing the following fields:
1{2"instruction": "dequeue",3"to": "{the Worker's phone number, sip uri, or client uri. Required.}",4"from": "{the caller ID that you want to send to the Worker. Required.}",5"post_work_activity_sid": "{the ActivitySid that should be assigned to the Worker after the call ends. Optional.}"6}
More about these fields:
instruction specifies the assignment activity to perform. Use 'dequeue' to dequeue a call to a Worker. For a full list of assignment instructions, read about handling assignment callbacks here.to specifies the Worker recipient's phone number.from specifies the caller ID that should be sent when extending the call to the Worker.post_work_activity_sid specifies the ActivitySid that you want to assign to the Worker after the call completes. Use this to transition your worker to a status that makes sense for their after-call, work for example "Wrapping up".Upon issuing a Dequeue Instruction, TaskRouter will update the TaskAttributes of the Task with a worker_call_sid to denote the CallSid that is being created for the outgoing call to a given worker.
The TaskAttributes of the Task will then contain both the call_sid and worker_call_sid. Any event thereafter relating to the Task, such as task.completed, will have this information as well.
If you need Linked Call Tracking, consider listening to or querying for task.completed events that will have this information.
Sometimes you may want your application to perform additional actions before bridging the call to the selected Worker. TaskRouter and TwiML have a few additional points of integration that make this possible. Here's how it typically works:
<Enqueue>s a call using TwiML, as described above. TaskRouter will reserve a Worker to handle the call and make an AssignmentCallback HTTP request to your application.POST request to Twilio's /Calls REST API to place a call to the Worker's phone.<Dial>s <Queue> with the Tasks' reservationSid. For example:1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Dial>4<Queue reservationSid="WR0123456789abcdef0123456789abcdef"/>5</Dial>6</Response>
Upon execution of this TwiML document, the Worker and the call will be bridged, and the pending Reservation will be marked as 'accepted'.
Note: If you plan to execute additional logic before bridging the waiting call with the Worker, make sure you set an appropriately high Task Reservation Timeout on your Workflow. The default is 120 seconds, which should be sufficient for most operations.
<Dial>ing a ReservationSidThis example will accept the reservation and bridge to the task's call as in the previous example. When the call completes, the worker will be moved to the provided activity state ("wrapping up" for example).
1<?xml version="1.0" encoding="UTF-8"?>2<Response>3<Dial>4<Queue5reservationSid="WR0123456789abcdef0123456789abcdef"6postWorkActivitySid="WA0123456789abcdef0123456789abcdef"/>7</Dial>8</Response>
When combining TwiML and TaskRouter, there are a few important behaviors that you should know about.
When using <Enqueue> and <Dial><Queue reservationSid="..."></Dial> to create and manage Tasks, Twilio will keep the Task's lifecycle updated. For example, if the call disconnects before being answered by a Worker, TaskRouter will cancel the associated Task. Because of this linking, we recommend you do not modify the state of a Task that is linked to a waiting Twilio call.
Using the TaskRouter REST API to modify a Task's attributes can interfere with Twilio's ability to manage the Task's lifecycle. Additionally, Dial->Queue is responsible for accepting the reservation provided. If the reservation has been accepted already, either via the TaskRouter REST API or via an assignment instruction, the dequeue attempt will fail with an error.
Each Task generated from TwiML contains a "call_sid" attribute, as well as the other standard Twilio call attributes ("to", "from", etc.).
Here is the full list of attributes,
1{2"from_country": "",3"called": "",4"to_country": "",5"to_city": "",6"to_state": "",7"caller_country": "",8"call_status" : "",9"call_sid" : "",10"account_sid" : "",11"from_zip" : "",12"from" : "",13"direction" : "",14"called_zip" : "",15"caller_state" : "",16"to_zip" : "",17"called_country" : "",18"from_city" : "",19"called_city" : "",20"caller_zip" : "",21"api_version" : "",22"called_state" : "",23"from_state" : "",24"caller" : "",25"caller_city" : "",26"to" : ""27}
When you use TaskRouter with <Enqueue>, TaskRouter will dynamically create voice Queues to hold your Tasks, using the provided WorkflowSid as a friendly name. These queues are used internally by the TaskRouter system to park the calls on hold. Modifying these queues or their members using the REST API may lead to unpredictable or undesirable results which prevent calls from being properly bridged.
We all do sometimes; code is hard. Get help now from our support team(link takes you to an external page), or lean on the wisdom of the crowd by browsing the Twilio tag(link takes you to an external page) on Stack Overflow.
| Web Proxy Viewer | New URL | Original Page |