Last active
June 14, 2016 06:47
-
-
Save ObjectIsAdvantag/cc131aab9409f014db22e18e572f722d to your computer and use it in GitHub Desktop.
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
// DO NOT DELETE : this gist is referenced by https://www.tropo.com/2016/06/gists-can-help-tropo-scripting-development/ | |
// and https://www.tropo.com/2016/06/devops-follow-tropo-spark/ | |
// returns true or false | |
function isEmail(email) { | |
// extract from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript | |
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | |
return re.test(email); | |
} | |
// returns an email address if found in the phrase specified | |
function extractEmail(phrase) { | |
if (phrase) { | |
var parts = phrase.split(" "); | |
for (var i = 0; i < parts.length ; i++) { | |
if (isEmail(parts[i])) { | |
return parts[i]; | |
} | |
} | |
} | |
return null; | |
} | |
// You may check currentCall features here : https://www.tropo.com/docs/scripting/currentcall | |
if (currentCall) { | |
if (currentCall.network == "SMS") { | |
var input = currentCall.initialText; | |
log("DEBUG: received: " + input + ", from: " + currentCall.callerID ); | |
// check we received a valid email address | |
var email = extractEmail(input); | |
if (email) { | |
log("INFO: New subscription for email: " + email); | |
say ("Thanks for suscribing to our service."); | |
} | |
else { | |
log("DEBUG: could not extract email from: " + input + ", for: " + currentCall.callerID ); | |
say ("Sorry we could not read your email"); | |
} | |
} | |
else { | |
// Speak a welcome message if the incoming call is not a SMS | |
log("INFO: incoming call from: " + currentCall.callerID ); | |
say("Please text your email to this number."); | |
} | |
} | |
else { | |
log("DEBUG: no outgoing call support here"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment