Created
May 6, 2016 11:07
-
-
Save aylarov/02441538ad92def07ce3363a1e45809d to your computer and use it in GitHub Desktop.
Example for Medium post
This file contains hidden or 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
var inbound_call; | |
// Incoming call has arrived | |
VoxEngine.addEventListener(AppEvents.CallAlerting, function (event) { | |
inbound_call = event.call; | |
// Add event listener for connected event | |
inbound_call.addEventListener(CallEvents.Connected, function (callevent) { | |
// Make http request to external webserivce to get some data | |
Net.httpRequest("http://domain.com/getData/?" + encodeURIComponent(inbound_call.callerid()), handleDataReceived); | |
}); | |
inbound_call.addEventListener(CallEvents.Disconnected, VoxEngine.terminate); | |
// Answer the call | |
inbound_call.answer(); | |
}); | |
function handleDataReceived(e) { | |
// If everything is ok - parse the answer | |
if (e.code == 200) { | |
try { | |
var result = JSON.parse(e.text), | |
text; | |
// Some business logic | |
switch (result.name) { | |
case "John": | |
text = "Hi John, your debt is " + (result.debt) + " USD"; | |
break; | |
default: | |
text = "I don't know you"; | |
break; | |
} | |
inbound_call.say(text, Language.US_ENGLISH_FEMALE); | |
inbound_call.addEventListener(CallEvents.PlaybackFinished, VoxEngine.terminate); | |
} catch (e) { | |
Logger.write(JSON.stringify(e)); | |
handleError(); | |
} | |
} else handleError(); | |
} | |
function handleError() { | |
// Something went wrong - end the call gracefully | |
inbound_call.say("We are sorry, but the service is temporarily unavailable", Language.US_ENGLISH_FEMALE); | |
inbound_call.addEventListener(CallEvents.PlaybackFinished, VoxEngine.terminate); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment