Skip to content

Instantly share code, notes, and snippets.

@ObjectIsAdvantag
Last active May 29, 2016 12:59
Show Gist options
  • Save ObjectIsAdvantag/02f1d5c329580889bd2fbdc7ca96ac71 to your computer and use it in GitHub Desktop.
Save ObjectIsAdvantag/02f1d5c329580889bd2fbdc7ca96ac71 to your computer and use it in GitHub Desktop.
Tropo script to onboard new users into Spark
//
// Spark Logging Library
//
// 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) {
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
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;
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
bodyWriter = new java.io.DataOutputStream(connection.getOutputStream());
log("SPARK_LOG: posting: " + newLogEntry + " to: " + url);
contents = '{ "text": "' + this.tropoApp + ': ' + newLogEntry + '" }'
bodyWriter.writeBytes(contents);
bodyWriter.flush ();
bodyWriter.close ();
result = connection.getResponseCode();
log("SPARK_LOG: read response code: " + result);
}
catch(e) {
log("SPARK_LOG: socket Exception or Server Timeout");
throw {message:("Socket Exception or Server Timeout: " + e), code:0};
}
if(result < 200 || result > 299) {
log("SPARK_LOG:Received non-2XX response");
throw {message:("Received non-2XX response: " + result), code:result};
}
log("SPARK_LOG: log successfully sent to Spark, code: " + result);
}
var SparkInfo = new SparkLog("Spark Onboarding - INFO:", "Y2lzY29zcGFyazovL3VzL1dFQkhPT0svNzAyOTRkNjMtZDczYy00YzM3LWFkMDctOTdjNmQwYTlmMDk4");
var SparkDebug = new SparkLog("Spark Onboarding - DEBUG:", "Y2lzY29zcGFyazovL3VzL1dFQkhPT0svNzAyOTRkNjMtZDczYy00YzM3LWFkMDctOTdjNmQwYTlmMDk4");
//
// Tropo Helper Functions
//
function isIncoming() {
return (currentCall != null);
}
function isPhoneCall() {
return (currentCall != null) && (currentCall.channel == "VOICE");
}
function isSMS() {
return (currentCall != null) && (currentCall.channel == "TEXT");
}
//
// Script logic starts here
//
// Is it an incoming call ?
if (isIncoming()) {
SparkDebug.log("new incoming call, callerId: +" + currentCall.callerId + ", callId: " + currentCall.id + ", sessionId: " + currentCall.sessionId);
if (isPhoneCall()) {
SparkInfo.log("speaking welcome invite to " + currentCall.callerId);
say("Thanks for joining Tropo Europe Testers service.");
wait(1000);
say("We welcome your responses via SMS only for now");
wait(1000);
say("Please text your message to this number !");
wait(1000);
}
else {
if (isSMS()) {
var input = currentCall.initialText;
var caller = currentCall.callerID;
SparkDebug.log("incoming SMS: " + input + ", from: +" + caller);
// TODO : parse text
SparkInfo.log("sending welcome invite to " + caller);
call(testerNumber, { network:"SMS" });
say("Thanks for joining Tropo Europe Testers service.");
}
else {
SparkDebug.log("incoming call error, network:UNKNOWN, with callerId: " + currentCall.callerID);
say("Sorry we cannot process your message");
}
}
}
// No call active, a new session will be started
else {
SparkDebug.log("no session yet, this is an outgoing call request initiated from an API");
// Robustify, make user testNumber is injected
if ((typeof testerNumber === 'undefined') || !(testerNumber)) {
SparkDebug.log("testerNumber parameter is not set, aborting...");
}
else {
// Start an SMS session
call(testerNumber, { network:"SMS" });
SparkDebug.log("started SMS session to: " + testerNumber + ", callId: " + currentCall.id + ", sessionId: " + currentCall.sessionId);
//say("Hi Tropo Europe Tester, this SMS with trackID: " + trackID + " was delivered via: " + country + ". Please check the sender number is " + troponumber + ". If yes, respond and include your trackID. For example: received " + trackID + ". If no, send an SMS to XXXX");
say("Tropo test: " + testID + ", sms sent from: " + tropoCountry + ", please check sender is: +" + tropoNumber + ", and respond CORRECT or WRONG. Thank you");
SparkInfo.log("sent SMS to: " + testerNumber + ", test: #" + testID + ", from: " + tropoCountry + " - " + tropoNumber);
// IN PROGRESS
// Close SMS session to avoid conflits.
// Note : a conflict sometimes happens when the testers answers : the tester answer may be processed on current session which could terminate because of a timeout)
// But also do not close otherwise we cannot respond to the initial call
//hangup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment