Created
June 20, 2019 14:42
-
-
Save nokenwa/5237c64dd6812aa60ac70f2b809ea3cd to your computer and use it in GitHub Desktop.
A Twilio Function that checks your calendar and either forwards your calls to your mobile number or records a voicemail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = function(context, event, callback) { | |
const moment = require('moment'); | |
const { google } = require('googleapis'); | |
const cal = google.calendar({ | |
version: 'v3', | |
auth: context.GoogleAPIKEY | |
}); | |
let calendar = context.myGoogleCalendar; | |
let now = moment(); | |
let tenMinutesFromNow = moment().add(10,'minutes'); | |
// Make the query | |
return cal.freebusy.query({ | |
resource: { | |
// Set times to ISO strings as such | |
timeMin: now.toISOString(), | |
timeMax: tenMinutesFromNow.toISOString(), | |
items: [{ id: calendar }] | |
} | |
}).then((result) => { | |
const busy = result.data.calendars[calendar].busy; | |
const errors = result.data.calendars[calendar].errors; | |
if (busy.length !== 0) { | |
// Respond and Prompt for recording | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
twiml.say("Nathaniel is unavailable right now. If you leave a message he will get back to you as soon as possible."); | |
twiml.record({ | |
timeout: 10, | |
recordingStatusCallback: '/voicemail', | |
recordingStatusCallbackEvent: 'completed' | |
}); | |
callback(null, twiml); | |
} else { | |
// Forward to mobile | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
twiml.dial(context.MobileNumber); | |
callback(null, twiml); | |
} | |
}).catch((e) => { | |
console.error(e); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment