-
Notifications
You must be signed in to change notification settings - Fork 93
/
Voice mail
36 lines (34 loc) · 1.28 KB
/
Voice mail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const moment = require('moment');
const { google } = require('googleapis');
exports.handler = function(context, event, callback) {
const cal = google.calendar({
version: 'v3',
auth: context.GOOGLE_API_KEY
});
cal.freebusy.query({
resource: {
timeMin: moment().toISOString(),
timeMax: moment().add(10,'minutes').toISOString(),
items: [{ id: context.GOOGLE_CALENDAR_ID }]
}
})
.then((result) => {
const busy = result.data.calendars[calendar].busy;
if (busy.length !== 0) {
//Say I am unavailable
let twiml = new Twilio.twiml.VoiceResponse();
twiml.say("Sorry, 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.MOBILE_NUMBER);
callback(null, twiml);
}
})
}