Last active
June 20, 2016 08:34
-
-
Save ObjectIsAdvantag/f006c72a2d036f34ec6a54b613a09da1 to your computer and use it in GitHub Desktop.
Tech2Day post event Welcome IVR
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
// | |
// Cisco Spark Logging Library for Tropo | |
// | |
// Factory for the Spark Logging Library, with 2 parameters | |
// - the name of the application will prefix all your logs, | |
// - the Spark Incoming integration (to which logs will be posted) | |
// To create an Incoming Integration | |
// - click integrations in the right pane of a Spark Room (Example : I create a dedicated "Tropo Logs" room) | |
// - select incoming integration | |
// - give your integration a name, it will be displayed in the members lists (Example : I personally named it "from tropo scripting") | |
// - copy your integration ID, you'll use it to initialize the SparkLibrary | |
function SparkLog(appName, incomingIntegrationID) { | |
if (!appName) { | |
appName = ""; | |
//log("SPARK_LOG : bad configuration, no application name, exiting..."); | |
//throw createError("SparkLibrary configuration error: no application name specified"); | |
} | |
this.tropoApp = appName; | |
if (!incomingIntegrationID) { | |
log("SPARK_LOG : bad configuration, no Spark incoming integration URI, exiting..."); | |
throw createError("SparkLibrary configuration error: no Spark incoming integration URI specified"); | |
} | |
this.sparkIntegration = incomingIntegrationID; | |
log("SPARK_LOG: all set for application:" + this.tropoApp + ", posting to integrationURI: " + this.sparkIntegration); | |
} | |
// This function sends the log entry to the registered Spark Room | |
// Invoke this function from the Tropo token-url with the "sparkIntegration" parameter set to the incoming Webhook ID you'll have prepared | |
// Returns true if the log entry was acknowledge by Spark (ie, got a 2xx HTTP status code) | |
SparkLog.prototype.log = function(newLogEntry) { | |
// Robustify | |
if (!newLogEntry) { | |
newLogEntry = ""; | |
} | |
var result; | |
try { | |
// Open Connection | |
var url = "https://api.ciscospark.com/v1/webhooks/incoming/" + this.sparkIntegration; | |
var connection = new java.net.URL(url).openConnection(); | |
// Set timeout to 10s | |
connection.setReadTimeout(10000); | |
connection.setConnectTimeout(10000); | |
// Method == POST | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
// TODO : check if this cannot be removed | |
connection.setRequestProperty("Content-Length", newLogEntry.length); | |
connection.setUseCaches(false); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
//Send Post Data | |
var bodyWriter = new java.io.DataOutputStream(connection.getOutputStream()); | |
log("SPARK_LOG: posting: " + newLogEntry + " to: " + url); | |
var contents = '{ "text": "' + this.tropoApp + ': ' + newLogEntry + '" }' | |
bodyWriter.writeBytes(contents); | |
bodyWriter.flush(); | |
bodyWriter.close(); | |
var result = connection.getResponseCode(); | |
log("SPARK_LOG: read response code: " + result); | |
if (result < 200 || result > 299) { | |
log("SPARK_LOG: could not log to Spark, message format not supported"); | |
return false; | |
} | |
} | |
catch (e) { | |
log("SPARK_LOG: could not log to Spark, socket Exception or Server Timeout"); | |
return false; | |
} | |
log("SPARK_LOG: log successfully sent to Spark, status code: " + result); | |
return true; // success | |
} | |
// | |
// Library to send outbound API calls | |
// | |
// Returns the JSON object at URL or undefined if cannot be accessed | |
function requestJSONviaGET(requestedURL) { | |
try { | |
var connection = new java.net.URL(requestedURL).openConnection(); | |
connection.setDoOutput(false); | |
connection.setDoInput(true); | |
connection.setInstanceFollowRedirects(false); | |
connection.setRequestMethod("GET"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setRequestProperty("charset", "utf-8"); | |
connection.connect(); | |
var responseCode = connection.getResponseCode(); | |
log("JSON_LIBRARY: read response code: " + responseCode); | |
if (responseCode < 200 || responseCode > 299) { | |
log("JSON_LIBRARY: request failed"); | |
return undefined; | |
} | |
// Read stream and create response from JSON | |
var bodyReader = connection.getInputStream(); | |
// [WORKAROUND] We cannot use a byte[], not supported on Tropo | |
// var myContents= new byte[1024*1024]; | |
// bodyReader.readFully(myContents); | |
var contents = new String(org.apache.commons.io.IOUtils.toString(bodyReader)); | |
var parsed = JSON.parse(contents); | |
log("JSON_LIBRARY: JSON is " + parsed.toString()); | |
return parsed; | |
} | |
catch (e) { | |
log("JSON_LIBRARY: could not retreive contents, socket Exception or Server Timeout"); | |
return undefined; | |
} | |
} | |
// Returns the Status Code when GETting the URL | |
function requestStatusCodeWithGET(requestedURL) { | |
try { | |
var connection = new java.net.URL(requestedURL).openConnection(); | |
connection.setDoOutput(false); | |
connection.setDoInput(true); | |
connection.setInstanceFollowRedirects(false); | |
connection.setRequestMethod("GET"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setRequestProperty("charset", "utf-8"); | |
connection.connect(); | |
var responseCode = connection.getResponseCode(); | |
return responseCode; | |
} | |
catch (e) { | |
log("JSON_LIBRARY: could not retreive contents, socket Exception or Server Timeout"); | |
return 500; | |
} | |
} | |
// | |
// Script logic starts here | |
// | |
// Let's create several instances for various log levels | |
var SparkInfo = new SparkLog("", "Y2lzY29zcGFyazovL3VzL1dFQkhPT0svN2U1MzY5MDUtOWU1MS00MWMwLThiNmMtNWEyNDM0NjkyMzUy"); | |
var SparkDebug = new SparkLog("", "Y2lzY29zcGFyazovL3VzL1dFQkhPT0svYWI3YmJjNDctODYzOS00MjE2LTgwODYtODMyM2FlMzQ0ODRh"); | |
// info level used to get a synthetic sump up of what's happing | |
function info(logEntry) { | |
log("INFO: " + logEntry); | |
SparkInfo.log(logEntry); | |
// Uncomment if you opt to go for 2 distinct Spark Rooms for DEBUG and INFO log levels | |
//SparkDebug.log(logEntry); | |
} | |
// debug level used to get detail informations | |
function debug(logEntry) { | |
log("DEBUG: " + logEntry); | |
SparkDebug.log(logEntry); | |
} | |
// 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; | |
} | |
function fetchNextSessions() { | |
var url = "https://tech2day2016.cleverapps.io/api/v1/sessions/next"; | |
var response = requestJSONviaGET(url); | |
if (response && response instanceof Array) { | |
return response; | |
} | |
return []; | |
} | |
// Returns true if successfully registered | |
function registerSandbox(email) { | |
var url = "https://tech2day2016.cleverapps.io/api/v1/spark/onboard?email=" + email + "&token=CiscoDevNet"; | |
return requestStatusCodeWithGET(url); | |
} | |
// You may check currentCall features here : https://www.tropo.com/docs/scripting/currentcall | |
if (currentCall) { | |
if (currentCall.network == "SMS") { // SMS | |
say("Merci de votre participation au Tech2Day 2016 ! Rendez-vous les 7, 8 et 9 juin 2017."); | |
info("sent welcome SMS to : +" + currentCall.callerID); | |
} | |
else { // Voice | |
// Speak a welcome message | |
wait(1000); | |
say("Merci pour votre participation au TekToutDai 2016 ! Rendez-vous les 7, 8 et 9 juin 2017 à Nantes.", { | |
voice: "Aurelie" | |
}); | |
wait(500); | |
info("spoke the welcome message to: " + currentCall.callerID); | |
} | |
} | |
else { // Token URL | |
// Horloge parlante | |
var now = new Date(Date.now()); | |
debug("Il est exactement " + (now.getHours() + 2) + " heures et " + now.getMinutes() + " minutes"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment