diff --git a/buzzer-activated.js b/buzzer-activated.js new file mode 100644 index 0000000..d132177 --- /dev/null +++ b/buzzer-activated.js @@ -0,0 +1,25 @@ +/** + * Simple call box routine + * + * This function is meant for the apartment building callbox + * It gives the user a couple of seconds to produce the password + * Then dials all the residents to grant manual entry + */ +exports.handler = function(context, event, callback) { + + let twiml = new Twilio.twiml.VoiceResponse(); + + // Gather both speech and digit entry from user + twiml.gather({ + action: '/door-open', + hints: context.PASSPHRASE, + input: 'speech dtmf', + numDigits: '4', + speechTimeout: 'auto', + timeout: 2, + }) + .say({voice: 'woman'}, 'Please wait') + + twiml.redirect('/call-residents') + callback(null, twiml) +} \ No newline at end of file diff --git a/call-residents.js b/call-residents.js new file mode 100644 index 0000000..0e4f0e2 --- /dev/null +++ b/call-residents.js @@ -0,0 +1,11 @@ +exports.handler = function(context, event, callback) { + let twiml = new Twilio.twiml.VoiceResponse(); + + // If no valid answer after timeout, dial all residents until someone picks up + let dial = twiml.dial({action: '/text-me?Method=call'}); + // dial.number(context.SKYLER_PHONE); // Skyler + // dial.number(context.CHAYA_PHONE); // Chaya + dial.number(context.JOHN_PHONE); // John + + callback(null, twiml) +} \ No newline at end of file diff --git a/door-open.js b/door-open.js new file mode 100644 index 0000000..c0b8cf9 --- /dev/null +++ b/door-open.js @@ -0,0 +1,21 @@ +exports.handler = function(context, event, callback) { + let twiml = new Twilio.twiml.VoiceResponse(); + + console.log('Speech: ' + event.SpeechResult + '. Confidence: ' + event.Confidence) + console.log('Digits: ' + event.Digits) + + if ( (event.SpeechResult == context.PASSPHRASE && event.Confidence > 0.5) || event.Digits == context.PASSCODE) { + // Check if we have a password match, and open the door + twiml.say({voice: 'man'}, 'Nice! fam!') + twiml.play({digits: '9'}) // Pressing 9 sends DTFM tone to open the door + twiml.pause({length:1}) + twiml.play('http://demo.twilio.com/docs/classic.mp3') + + // Also send me a text on this + twiml.redirect('/text-me?Method=code') + callback(null, twiml) + } else { + twiml.redirect('/call-residents') + callback(null, twiml) + } +} \ No newline at end of file diff --git a/text-me.js b/text-me.js new file mode 100644 index 0000000..bdc4e3f --- /dev/null +++ b/text-me.js @@ -0,0 +1,19 @@ +exports.handler = function(context, event, callback) { + let twiml = new Twilio.twiml.VoiceResponse(); + + var bodyText; + + if (event.Method == 'code') {bodyText = 'Someone used the password to get in the building.'} + else {bodyText = 'Somebody buzzed the door but didn\'t know the passcode.'} + + context.getTwilioClient().messages.create({ + to: context.JOHN_PHONE, + from: context.TWILIO_PHONE, + body: bodyText, + }) + .then((message) => { + console.log(message.sid); + callback(null, twiml); + }) + .catch((err) => callback(err, null)); +}; \ No newline at end of file