Functions + TaskRouter: <Enqueue> with child <Task> data

726 views
Skip to first unread message

Preston Doster

unread,
Jun 16, 2017, 11:03:05 AM6/16/17
to Twilio Functions
Hi, folks --

I'm working with Functions + TaskRouter, and I can successfully get it to <Enqueue>to my expected workflowSid; however, I haven't found a way to include an underlying <Task> node to route to a particular worker.  Here are a few things I've tried:

//var enqueueNode = twiml.enqueue({ workflowSid:"mysid" }, JSON.stringify(taskData));  
var enqueueNode = twiml.enqueue({ workflowSid:"mysid" }); // works but no <Task> node  
//var enqueueNode = twiml.enqueue({ workflowSid:"mysid" }, function(node){ node.task(JSON.stringify(taskData)) }); // nope
//var enqueueNode = twiml.enqueue({ workflowSid:"mysid",  }, function(node){ node.Task(JSON.stringify(taskData)) }); // nope
//var enqueueNode = twiml.enqueue({ workflowSid:"mysid" }, "<Task>" + JSON.stringify(taskData) + "</Task>"); // nope

twiml.enqueue does not appear to return a value (like gather), so no luck there. I've also tried manually creating the twiml via a string, but I don't know how to set the Content-Type in a twilio Function on the response, so no dice.  I'd be OK with that as a starting solution.

The docs are pretty sparse, so any help is much apprecitated.

Carter Rabasa

unread,
Jun 16, 2017, 11:16:15 AM6/16/17
to Twilio Functions
Hey Preston,

Sorry about the docs. I believe this is what you're looking for:

twiml.enqueueTask({ workflowSid:"mysid" }).task(null, JSON.stringify({foo: 'bar', baz: 'bat'}))

Cheers,
Carter

Preston Doster

unread,
Jun 16, 2017, 11:26:29 AM6/16/17
to Twilio Functions
Thanks, Carter. Found that in parallel while walking through the source code and works like a charm. :)

Aldo Pigni

unread,
Dec 21, 2018, 4:16:20 PM12/21/18
to Twilio Functions
Dear all,

I'm trying to create a Task in TaskRouter, to assign outgoing calls.

I tested the suggested code that uses Twiml - no error, but I don't see any new task in TaskRouter console:

exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.enqueueTask({ workflowSid:"WWxxxxx" }).task(null, JSON.stringify({foo: 'bar', baz: 'bat'}));
callback(null, twiml);
};


Then I tried to use API - straight from Twilio docs: Create a task

exports.handler = function(context, event, callback) {
    var client = context.getTwilioClient();
    client.taskrouter.workspaces('WSxxxx')
                 .tasks
                 .create({attributes: JSON.stringify({type: 'recall'}), 
                          workflowSid: 'WWxxxx'})
                 .then(task => console.log(task.sid))
                 .done();
callback(null, "OK");
};


No success, again no errors, but I don't create any task in TaskRouter.

Thanks in advance for any help.
Aldo

Douglas Buxton

unread,
Dec 21, 2018, 9:36:51 PM12/21/18
to Twilio Functions
Aldo, the calls you are making to twilio are performed asynchronously.  You are essentially calling callback() in both cases at the same time as the API call which terminates API call before it can finish.

In twilio functions callback() should be placed somewhere such that all necessary calls have been completed.  For example:

exports.handler = function(context, event, callback) {
   
var client = context.getTwilioClient();
    client
.taskrouter.workspaces('WSxxxx')
       
.tasks
       
.create({
            attributes
: JSON.stringify({ type: 'recall' }),
            workflowSid
: 'WWxxxx'
       
})

       
.then(task => {
            console
.log(task.sid);
            callback
(null, "OK");
       
}).catch(error=>console.error(error));
}

Would be more successful.

Alan

unread,
Dec 22, 2018, 8:16:43 AM12/22/18
to Twilio Functions
The below Twilio Function generates TwiML. Note that TwiML is attached to some action (for example, a call comes into your Twilio number and you webhook out to the Function hosting the code below to generate the TwiML), rather then causing something to happen itself. Something needs to consume the TwiML to make it happen. The REST API causes things to happen, instead of a result of another action (you could, for example, kick off a REST API request that then consume TwiML), and Douglas' response is spot on, with placing the callback in the proper location, to make sure the aysnc operation (HTTPS POST request to Twilio) completes before the Function terminates. The Twilio Node examples are more generic to Node rather then Functions. More details here: Why isn't my code running.

exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.enqueue({workflowSid: 'WW0123401234...'})
.task({"selected_language": "es"});
callback(null, twiml);
};

Resulting TwiML (when function run)

<Response>
<Enqueue workflowSid="WW0123401234...">
<Task selected_language="es"/>
</Enqueue>
</Response>



Alan

aldo pigni

unread,
Dec 22, 2018, 11:49:23 AM12/22/18
to Twilio Functions
Many thanks Douglas and Alan,
for solving my problem and pointing me in the right direction to understand the principles behind the solution, I'm twice grateful for your help.

Reply all
Reply to author
Forward
0 new messages