How to send an abandoned call back to Studio Flow?

257 Views Asked by At

I want to implement a solution where, if there are no available workers Twilio I don't send it to Flex, but do something else (e.g: say a message to contact us by other channel type, run a Function, or any other option)

Best posible scenario: Inside a Studio Flow, before creating a Task and sending it to Flex, I want to check if in that queue there are any available worker.

Alternative: If that not posible, because to know the worker's availability depends on TaskRouter's Workflow, Is there any way to go back to Studio after a Timeout (i.e: an abandoned call)?

The only available option I found (but did not implemented yet) is setting a callback in the TaskRouter > Settings and handle de Workflow Timeout event. But in this case I lose all the context I had in the Studio Flow (e.g: the options the user selected in the IVR).

1

There are 1 best solutions below

1
On BEST ANSWER

Flex TSE here.

Both options are possible.

For the first, you can use a Run Function widget in your Studio flow to run something like:

exports.handler = async function(context, event, callback) {
 
  const client = context.getTwilioClient();

  try {
    const rt = await client.taskrouter.workspaces(context.WORKSPACE_SID)
      .taskQueues(context.TASK_QUEUES_SID)
      .realTimeStatistics()
      .fetch()
      .then(stats => {
        console.log('Realtime Stats', stats);
        return stats;
      })
 
    if (rt.totalAvailableWorkers !== 0){
        callback(null, 'workers_available'); 
    } else {
        callback(null, 'no_workers_available');
    }

  } catch (e) {
    console.log('error', e, e.stackTrace);
    callback(null, e);
  }
 
}

to retrieve the available agent count. From there, you can route accordingly based on the return value.

If you want to route a task back to the IVR, you can create a Flex Plugin that uses the TwiML redirect verb to point to the Webhook for the studio flow.

For example:

  const twiml = `
      <?xml version="1.0" encoding="UTF-8"?>
      <Response>
          <Redirect>https://webhooks.twilio.com/v1/Accounts/${ACCOUNT_SID}/Flows/${studioFlowSid}?FlowEvent=return&amp;transferToIVRMenu=${transferToIVRMenu}</Redirect>
      </Response>
  `.trim();

  const client = context.getTwilioClient();
  await client.calls(CallSid).update({ twiml });
  return Response(callback, 'ok');

Additionally, you can get full sample code for an agent-driven redirect from this blog post.