Last active
July 10, 2017 17:11
-
-
Save mattmakai/4c94b798c977ad307cf786684bea7384 to your computer and use it in GitHub Desktop.
IT Support Line
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
// Code for Twilio Functions (https://www.twilio.com/functions) that builds an automated speech recognition IT support desk. | |
// Read the companion blog post (https://www.twilio.com/blog/2017/07/building-the-it-crowd-answering-machine-using-twilio-functions.html) for full explanations. | |
/**************** Version 1 ******************/ | |
exports.handler = function(context, event, callback) { | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
twiml.say({ voice: 'man', language: 'en-gb' }, 'Welcome to the IT support hotline. Sorry, we are currently closed. We don not have any open hours.'); | |
callback(null, twiml); | |
}; | |
/**************** Version 2 ******************/ | |
exports.handler = function(context, event, callback) { | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
if (!event.SpeechResult) { | |
twiml.gather({ input: 'speech', action: '/hotline-v2?next=issue'}).say({ voice: 'man', language: 'en-gb' }, 'Hello this is I.T. What is your issue?'); | |
} else if (event.SpeechResult.toLowerCase().indexOf('thank') !== -1) { | |
twiml.say({ voice: 'man', language: 'en-gb' }, 'You are welcome.'); | |
twiml.hangup(); | |
} | |
callback(null, twiml); | |
}; | |
/**************** Version 3 ******************/ | |
exports.handler = function(context, event, callback) { | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
if (!event.SpeechResult) { | |
twiml.gather({ input: 'speech', action: '/hotline-v3?next=issue'}).say({ voice: 'man', language: 'en-gb' }, 'Hello this is I.T. What is your issue?'); | |
} else if (event.SpeechResult.toLowerCase().indexOf('thank') !== -1) { | |
twiml.say({ voice: 'man', language: 'en-gb' }, 'You are welcome.'); | |
twiml.hangup(); | |
} else { | |
askOnOff(twiml); | |
} | |
callback(null, twiml); | |
}; | |
function askOnOff(twiml) { | |
twiml.gather({ input: 'speech', action: '/it-support?next=plugged'}).say({ voice: 'man', language: 'en-gb' }, 'Did you try turning it off and on again?'); | |
} | |
/**************** Version 4 ******************/ | |
exports.handler = function(context, event, callback) { | |
let twiml = new Twilio.twiml.VoiceResponse(); | |
if (!event.SpeechResult) { | |
twiml.gather({ input: 'speech', action: '/it-support?next=onoff'}).say({ voice: 'man', language: 'en-gb' }, 'Hello I.T.'); | |
} else if (event.SpeechResult.toLowerCase().indexOf('thank') !== -1) { | |
twiml.say({ voice: 'man', language: 'en-gb' }, 'You are welcome.'); | |
twiml.hangup(); | |
} else if (event.next === 'plugged') { | |
if (event.SpeechResult.toLowerCase().indexOf('yes') !== -1) { | |
askPluggedIn(twiml); | |
} else { | |
askOnOff(twiml); | |
} | |
} else if (event.next === 'problem') { | |
askForProblem(twiml); | |
} else if (event.next === 'solution') { | |
const linkToStackOverflow = 'https://stackoverflow.com/search?q=' + require('querystring').escape(event.SpeechResult); | |
twiml.say({ voice: 'man', language: 'en-gb' }, 'Alright I found a few solutions. I will send you an SMS with them. Bye.'); | |
twiml.sms(linkToStackOverflow); | |
} else { | |
askOnOff(twiml); | |
} | |
callback(null, twiml); | |
}; | |
function askOnOff(twiml) { | |
twiml.gather({ input: 'speech', action: '/it-support?next=plugged'}).say({ voice: 'man', language: 'en-gb' }, 'Did you try turning it off and on again?'); | |
} | |
function askPluggedIn(twiml) { | |
twiml.gather({ input: 'speech', action: '/it-support?next=problem'}).say({ voice: 'man', language: 'en-gb' }, 'Is it definitely plugged in?'); | |
} | |
function askForProblem(twiml) { | |
twiml.gather({ input: 'speech', action: '/it-support?next=solution'}).say({ voice: 'man', language: 'en-gb' }, 'Alright what was your exact problem again?'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment