-1

I am trying to block spam and 800#s in one shot using a twilio function, but it's not working...

1 Answers1

0

You could modify the function as below:

exports.handler = function(context, event, callback) {
  // set-up the variables that this Function will use to forward a phone call using TwiML
  
  // REQUIRED - you must set this to the number you want calls forwarded to
  let phoneNumber = event.PhoneNumber || "+16787801234";  
  
  // OPTIONAL
  let callerId =  event.CallerId || null;
  // OPTIONAL
  let timeout = event.Timeout || null;
  // OPTIONAL +266696687 = ANONYMOUS Callers
  let blacklistedCallers = event.blacklistedCallers || ["+14073601234","+15185001234", "+266696687"];
  
  // generate the TwiML to tell Twilio how to forward this call
  let twiml = new Twilio.twiml.VoiceResponse();
  
  console.log(event.From.substring(0,5));
  
  let allowedThrough = false;
  if (blacklistedCallers.length > 0) {
    if ((blacklistedCallers.indexOf(event.From) === -1) && (event.From.substring(0,5) != "+1800")) {
      allowedThrough = true;    
    }
  }

  let dialParams = {};
  if (callerId) {
    dialParams.callerId = callerId;
  }
  if (timeout) {
    dialParams.timeout = timeout;
  }
  
  if (allowedThrough) {
    twiml.dial(dialParams, phoneNumber);
  }
  else {
    twiml.reject({reason: 'rejected'});
  }
  
  // return the TwiML
  callback(null, twiml);
};
Alan
  • 10,465
  • 2
  • 8
  • 9
  • Thanks Alan, but how would I restrict 800#s from calling? E.g., is there anyway with this node.js script to do a wildcard of 800#s , 866#s, etc? I see the one line showing the 800# but would I keep adding another if(.. for each circumstance that I would want to block? – jasonCbraatz Jun 18 '18 at 13:14