Skip to content

Instantly share code, notes, and snippets.

@ChristopherThorpe
Created June 13, 2017 00:17
Show Gist options
  • Save ChristopherThorpe/521bfdbd903ac7628ac01f8bb1d651a5 to your computer and use it in GitHub Desktop.
Save ChristopherThorpe/521bfdbd903ac7628ac01f8bb1d651a5 to your computer and use it in GitHub Desktop.
Twilio simple PBX function
exports.handler = function(context, event, callback) {
const moment = require('moment');
//// Useful for debugging
//const util = require('util');
//console.log(util.inspect(context.getTwilioClient()));
//console.log(util.inspect(event));
// be sure to update numDigits below to match, or delete it for variable length
let phoneBook = {
"888" : "+1-800-800-8000", // super 8 motel
"666" : "+1-800-466-8356", // motel 6
"000" : null
};
let callerId = event.Caller; // || "+1-000-000-0000"; // default caller ID
let digits = event.Digits;
let twiml = new Twilio.twiml.VoiceResponse();
if (digits && phoneBook[digits]) {
twiml.say("Dialing extension " + digits);
twiml.dial({ callerId: callerId }, phoneBook[digits]);
twiml.hangup();
}
// Twilio time is in UTC. This allows 10 am to 7 pm PDT, or 9 am to 6 pm PST, weekdays.
// Twilio doesn't seem to have https://momentjs.com/timezone/ installed.
if ((moment().hour() >= 17 || moment().hour() < 2) && moment().isoWeekday() <= 5) {
let gather = twiml.gather({ numDigits: 3, timeout: 3 });
gather.say("Thank you for calling COMPANY NAME. Please dial your party's extension, or, hold, to leave a message.");
} else {
twiml.say("Thank you for calling COMPANY NAME. You have reached us outside business hours.");
}
twiml.redirect("http://twimlets.com/voicemail?Email=[YOUR-EMAIL]&Message=Please%20leave%20a%20message.&Transcribe=true");
callback(null, twiml);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment