Last active
May 27, 2016 12:52
-
-
Save ObjectIsAdvantag/b4f95087f03edbbfe4a5d825eac00782 to your computer and use it in GitHub Desktop.
Tropo logs sent to Spark (via an Incoming integration)
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
// This function writes the log parameter to a Spark Room | |
// Invoke this function from the Tropo token-url with the "sparkIntegration" parameter set to the incoming Webhook ID you'll have prepared | |
function log2spark(newLogEntry) { | |
log("TROPO-LOGS-TO-SPARK: checking integration is well defined"); | |
if (!sparkIntegration) { | |
log("TROPO-LOGS-TO-SPARK: integration not defined, cannot send request to spark"); | |
throw { message: ("no incoming integration identifier") }; | |
} | |
log("TROPO-LOGS-TO-SPARK: integration defined, continuing"); | |
var result; | |
try { | |
// Open Connection | |
var url = "https://api.ciscospark.com/v1/webhooks/incoming/" + 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("TROPO-LOGS-TO-SPARK: posting: " + newLogEntry + " to: " + url); | |
contents = '{ "text": "' + newLogEntry + '" }' | |
bodyWriter.writeBytes(contents); | |
bodyWriter.flush (); | |
bodyWriter.close (); | |
log("TROPO-LOGS-TO-SPARK: bytes written"); | |
log("TROPO-LOGS-TO-SPARK: reading response code"); | |
result = connection.getResponseCode(); | |
log("TROPO-LOGS-TO-SPARK: read response code: " + result); | |
} | |
catch(e) { | |
log("TROPO-LOGS-TO-SPARK: socket Exception or Server Timeout"); | |
throw {message:("Socket Exception or Server Timeout: " + e), code:0}; | |
} | |
if(result < 200 || result > 299) { | |
log("TROPO-LOGS-TO-SPARK:Received non-2XX response"); | |
throw {message:("Received non-2XX response: " + result), code:result}; | |
} | |
} | |
log("TROPO-LOGS-TO-SPARK: begin"); | |
log2spark("Hi from Tropo scripting platform"); | |
log("TROPO-LOGS-TO-SPARK: end"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment