Time based Routing via Twillio Number

Time based Routing via Twillio Number

Hey guys,

One of our issues with Zoho desk is the inability to route to external numbers depending on the time of day.

Because our agents are not 24/7 on the desk, we will sometimes get calls after hours which will cause the customer to leave a message, which isn't ideal as some issues are urgent.

We use a 3rd party answering service to ensure that after hours calls get routed to the on-call technician's mobile phone, and if they don't answer, we have at least someone taking down the information needed to log a ticket with urgency.

We couldn't get this functionality in Zoho desk, but we could create it using Twillio Functions.
Note: Twillio notes that Functions are BETA - it's an additional cost, but the 10,000 invocations are free. 

Note that this will change the Webhooks that Zoho Desk set using the Twillio API. I'm unsure how frequently these API's get checked/changed, so this may break ( untested). Does anyone have any insight on this?

Let's get started.

First, let's create the Node.js code on Twillio Functions.
 
Log into Twillio - go to  RunTime > Functions
Create a new function and give it a path name and function name.
I called it "Call Forward"  .

The code is as below (note we take no responsibility or warranty for this code).

  1. exports.handler = function(context, event, callback) 
  2. {

  3.   let callerId =  event.CallerId || null;
  4.   let timeout = event.Timeout || null;

  5.   //---
  6.  
  7.   let twiml = new Twilio.twiml.VoiceResponse();
  8.   
  9.   //Get time/date information
  10.   
  11.   var date = new Date();
  12.   var day = date.getDay();
  13.   var current_hour = date.getHours();
  14.   
  15.   
  16.   //Check if Past 5pm or Saturday/Sunday
  17.   
  18.   if(current_hour > 17 || (day > 5 || day === 0) )
  19.   {
  20.       twiml. say("You have reached our after hours support, please hold while we direct your call") // Replace this with your own message or remove it if you just want it to redirect.
  21.       
  22.     //Set dial forwarding dial parameters
  23.       let dialParams = {};
  24.       if (callerId) {
  25.         dialParams.callerId = callerId;
  26.       }
  27.       if (timeout) {
  28.         dialParams.timeout = timeout;
  29.       }

  30.       twiml.dial(dialParams, "+6100000000"); //Replace with your forwarding number!
  31.       callback(null, twiml);
  32.   }
  33.   else
  34.   {
  35.       //Forward to the DESK!
  36.       
  37.       twiml.redirect({
  38.         method: 'POST'
  39.         }, 'https://desk.zoho.com/newctiapi/xml/cticall/twillio?cx0d0d0x0x0x'); // Replace this with the webhook set by Desk
  40.         
  41.   callback(null, twiml);
  42.   }

  43.  
  44.     twiml.say('Sorry, something went wrong. Please email us at help@help.com. Goodbye.');

  45.   // return the TwiML
  46.   callback(null, twiml);
  47. };

Save the code.
Now go to 

Phone Numbers > Active Numbers > (Click on your Desk Phone Number)

Where is says 
A CALL COMES IN

Change this to Functions and select the function you just created.
Note that you will need to pull the Webhook and put this URL into line 42 of the code above




I hope this helps someone else in need of time based call routing