Skip to content

Instantly share code, notes, and snippets.

@ambiorixg12
Last active October 20, 2020 02:09
Show Gist options
  • Select an option

  • Save ambiorixg12/28fd9a58aab856ffaa2cd591ee96c462 to your computer and use it in GitHub Desktop.

Select an option

Save ambiorixg12/28fd9a58aab856ffaa2cd591ee96c462 to your computer and use it in GitHub Desktop.
twilio ivr time condition call recording
exports.handler = function(context, event, callback) {
let twiml = new Twilio.twiml.VoiceResponse();
// getting time With timezone:
// In Functions/Configure, add NPM name: moment-timezone, version: 0.5.14
// Timezone function reference: https://momentjs.com/timezone/
//https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/
let moment = require('moment-timezone');
//
// timezone needed for Daylight Saving Time adjustment
let timezone = event.timezone || 'America/Los_Angeles';
const hour = moment().tz(timezone).format('H');
const minutes = moment().tz(timezone).format('m');
const dayOfWeek = moment().tz(timezone).format('d');
console.log(" timezone: " + timezone);
console.log(" hour: " + hour);
console.log(" minutes: " + minutes);
console.log(" dayOfWeek: " + dayOfWeek);
const na ='+13487131002';
const nb ='+12343554089';
/*monday-tuesday , 6am - 2pm ring phone a */
if((hour >= 6 && hour <= 14) && (dayOfWeek >= 1 && dayOfWeek <= 2)) {
console.log(" calling to a " + na);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, na);
}
/*monday-tuesday ,< 6am - > 2pm ring phone b */
if((hour < 6 || hour > 14) && (dayOfWeek >= 1 && dayOfWeek <= 2)) {
console.log(" calling to b " + nb);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, nb);
}
/*weds ,< 6am - > 2pm ring phone b */
if((hour >= 6 && hour <= 12) && (dayOfWeek == 3)) {
console.log(" calling to a " + na);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, na);
}
/*weds ring phone b */
if((hour < 6 || hour > 12) && (dayOfWeek == 3)) {
console.log(" calling to b " + nb);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, nb);
}
///
/*thurs-frid , ring phone a */
if((hour >= 6 && hour <= 8) && (dayOfWeek >= 4 && dayOfWeek <= 5)) {
console.log(" calling to a " + na);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, na);
}
/*thurs-frid ring phone b */
if((hour < 6 || hour > 8) && (dayOfWeek >= 4 && dayOfWeek <= 5)) {
console.log(" calling to b " + nb);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, nb);
}
///
/*weekend ring phone b */
if((dayOfWeek===0 && dayOfWeek==6)) {
console.log(" calling to b " + nb);
twiml.dial({
record : 'record-from-answer',timeout: '25',
}, nb);
}
callback(null, twiml);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment