/** * 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 */ const fetch = require('node-fetch'); exports.handler = async function(context, event, callback) { let twiml = new Twilio.twiml.VoiceResponse(); let config = await fetch(context.DOORMAN_URL + `/api/door/info?buzzer=${event.From}`) .then(res => res.json()) .catch(err => { return undefined; }); // reject the call if this is not configured if (!config || !config.door) { twiml.reject(); callback(null, twiml); return; } let configQuery = `config=${encodeURIComponent(JSON.stringify(config))}`; // let user know someone is currently buzzing, and allow unlock by discord user let msg = `🔔 Someone is dialing right now @ Door "${config.door}" [Click to unlock](${context.DOORMAN_URL}/api/door/auth?door=${config.door}&key=`; config.discordUsers.forEach((u) => { // unlock me by discord userid fetch(context.DOORMAN_URL + `/api/door/notify?discordUser=${u}&msg=${encodeURIComponent(msg + u + ')')}`); }); // poll Doorman, to see if we should unlock const interval = setInterval(() => { fetch(context.DOORMAN_URL + `/api/door/status?door=${config.door}`) .then(async res => { // handle the case where doorman is explictly rejecting the buzzer if (res.status === 410) { clearInterval(interval); twiml.redirect(`/text-me?method=doorman-time-lock&${configQuery}`); callback(null, twiml); } // we got the successful unlock else if (res.status === 200) { clearInterval(interval); const body = await res.json(); twiml.redirect(`/door-open?fingerprint=${encodeURIComponent(JSON.stringify(body))}&${configQuery}`); callback(null, twiml); } }) .catch(err => console.log(err)); }, 500); // redirect to call after 6s setTimeout(() => { twiml.redirect(`/call-residents?${configQuery}`); callback(null, twiml); }, 6000); };