How to Use A Twilio Function to Interact with Calls Forwarded from Spoke to Twilio

 

Background: Routing Calls to Twilio from Spoke

Calls in Spoke can be routed to Twilio in two ways: 

  1. Redirect URL.  If you had sent a call to Spoke from Twilio (using Spoke's Redirect URL), you are able to  add parameters to the Redirect URL that tell Spoke to send unanswered calls back to either a Twilio Studio Flow, a Twilio Task Router Workflow, or a Twilio Function. See Routing Twilio calls to Spoke Phone in the developer API documents.
  2. Send to Twilio On No Answer. If you are using Spoke Teams to offer calls to groups of users, you can use the Spoke Team's On No Answer Rules to send unanswered calls to a Twilio Function.

This article relates to option #2, using a Twilio Function to interact with calls forwarded from a Spoke Team.

Prerequisites

  • Spoke deployed into your Twilio Account (BYO Twilio)
  • Administrator Access to Spoke
  • Administrator or Developer Access to Twilio Console for your Twilio Account
  • A Team in Spoke that is set up to forward unanswered calls to Twilio.
  • A new Twilio function with a logical name such as /callsfromspoke
  • A deep understanding of Twilio functions, workflows, tasks, workers, TwiML, and developing complex functions.

Setting Up Your Twilio Function

Below is a very basic Twilio Function structure that catches the call sent over from Spoke and places it in a Twilio Workflow.  

exports.handler = function (context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
try {
twiml
.enqueue({ workflowSid: "<INSERT WORKFLOW SID>" })
.task({ priority: 1 }, JSON.stringify({ name: event.From }));

console.log('Generated TwiML:', twiml.toString());
} catch (error) {
console.log("##### ERROR", error);
callback(error);
}
callback(null, twiml);
};

Limitations when sending calls to a Twilio function

Studio Flows: Twilio prevents sending a existing Twilio call to a Studio Flow - only new calls can trigger a Studio Flow execution. The one exception to this is when you use the Redirect Widget within a Studio Flow and then send the call back to the same Flow (and execution context). In other words, a call can only be sent to a Studio Flow if it originally started in that Flow and was redirected out using the Redirect Widget. Calls in Spoke are already in flight and cannot be sent to a Studio Flow, even though the call is part of your Twilio Account

To get around this limitation, calls forwarded from a Spoke Team are sent to a Twilio Function allowing developers to manipulate the call using TwiML to programmatically play sounds, add tasks, or put the call into a Workflow to be answered in Twilio Flex.

 

See also:

Was this article helpful?
0 out of 0 found this helpful