I recently ported my home landline number to Twilio. For now, I created a very basic call forwarding TwiML Bin to forward any incoming calls to this former landline number to my cell phone:
<Response>
<Dial>mycellnumber</Dial>
</Response>
What I'd like to do is have some logic to forward incoming calls to a different cell based on the incoming caller matching a number in a contact list, and default forwarding if the incoming number is not on a contact list.
For example, if the incoming call is from a number on the contact list for Cell-X
then forward the call to Cell-X
, else if on the contact list for Cell-Y
forward to Cell-Y
, else maybe go to cloud voice mail or another number.
Is there a way to do something like this as a TwiML Bin or in the Studio or is it too complicated? Maybe TaskRouter? This is residential so I'd like it to be invisible to the caller as opposed to something like an IVR solution where the caller is prompted to press a number for the person they want to reach.
I've not had any luck finding a call forwarding solution with logic like this by looking through the Twilio docs or by searching for examples. Please help!
You could do this with a Twilio function.
Let's say that your Cell-X list looks something like this:
const cellXContactList = ["+17782001001", "+17782001002", "+17782001003"];
and your Cell-Y list looks something like this:
const cellYContactList = ["+17782001004", "+17782001005", "+17782001006"];
then you could distribute incoming calls with something like this:
if (cellXContactList.length && cellXContactList.indexOf(event.From) !== -1) {
// caller number found in Cell-X contact list
destinationPhoneNumber = "+17781001001";
} else if (cellYContactList.length && cellYContactList.indexOf(event.From) !== -1) {
// caller number found in Cell-Y contact list
destinationPhoneNumber = "+17781001002";
}
Below is the entire code for the function (replace with your phone numbers):
// forward calls based on the incoming phone number
exports.handler = function (context, event, callback) {
// reference the Twilio helper library
const twiml = new Twilio.twiml.VoiceResponse();
// contacts lists
const cellXContactList = ["+17782001001", "+17782001002", "+17782001003"];
const cellYContactList = ["+17782001004", "+17782001005", "+17782001006"];
// if not in any contact list forward to this number
let destinationPhoneNumber = "+17781001000";
if (cellXContactList.length && cellXContactList.indexOf(event.From) !== -1) {
// caller number found in Cell-X contact list
destinationPhoneNumber = "+17781001001";
} else if (cellYContactList.length && cellYContactList.indexOf(event.From) !== -1) {
// caller number found in Cell-Y contact list
destinationPhoneNumber = "+17781001002";
}
twiml.dial({}, destinationPhoneNumber);
// return the TwiML
callback(null, twiml);
};
Once you've created and published your function, you can configure your Twilio number to run it when "A CALL COMES IN" (https://www.twilio.com/console/phone-numbers/incoming).