const moment = require('moment-timezone'); function isServiceOpen() { let now = moment().tz('Australia/Brisbane'); let isWorkday = now.isoWeekday() < 6; let isWorkingHour = now.hour() > 7 && now.hour() < 17; return isWorkday && isWorkingHour; } exports.handler = function(context, event, callback) { const twiml = new Twilio.twiml.VoiceResponse(); console.log(event); const callIncludesRecording = !!event.RecordingUrl; const callWasAnswered = event.DialCallStatus === 'completed'; const callWasNotAnswered = event.DialCallStatus === 'no-answer'; const serviceIsOpen = isServiceOpen(); if (callWasAnswered) { twiml.hangup(); } else if (callIncludesRecording) { console.log('Call includes recording!'); // do something with the recording URL here console.log(event.RecordingUrl); twiml.say("Thank you! We'll come back to you shortly."); twiml.hangup(); } else if (callWasNotAnswered) { console.log('Call was not answered...'); twiml.say( 'Unfortunately no one can answer right now. But you can leave a message.' ); twiml.record({ action: '/handle-call' }); } else if (!serviceIsOpen) { console.log('Service is closed...'); twiml.say('Service is closed but you can leave a message'); twiml.record({ action: '/handle-call' }); } else { twiml.dial( { action: '/handle-call', method: 'POST', timeout: 5 }, '+4915...' ); } callback(null, twiml); };