Last active
January 5, 2017 11:52
-
-
Save ObjectIsAdvantag/d26abbedaa644bb3843556529c42c809 to your computer and use it in GitHub Desktop.
Tropo ChatOps library that logs to Cisco Spark (javascript)
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
// QUICK START GUIDE | |
// | |
// 1. Create a Cisco Spark bot account dedicated to 'ChatOps', and store its token in a safe place | |
// 2. Create a Group room named "Tropo ChatOps", store its identifier in a safe place | |
// 3. Add your ChatOps bot as a member of the "Tropo ChatOps room" | |
// 4. Clone this gists, make it secret, and edit it | |
// 5. Replace the ChatOps bot token and ChatOps room identifier | |
// 6. Save your gists (keep it secret as it contains your bot's token), and click Raw | |
// 7. Create a Tropo application, with 'Scripting' type, pointing to your gist raw URL | |
// | |
// Cisco Spark Logging Library for Tropo (leveraging bot tokens) | |
// | |
// Factory for the Cisco Spark Logging Library, with 2 parameters | |
// - the token of the ChatOps bot, | |
// - the roomId, room identifier in which the bot will write its logs. The ChatOps bot must be a member of this room. | |
function SparkLog(token, roomId) { | |
if (!token) { | |
log("SPARK_LOG : bad configuration, no Spark token specified, exiting..."); | |
throw Error("SparkLog configuration error: no Spark token specified"); | |
} | |
this.token = token; | |
if (!roomId) { | |
log("SPARK_LOG : bad configuration, no Spark room identifier specified, exiting..."); | |
throw Error("SparkLog configuration error: no Spark room identifier specified"); | |
} | |
this.roomId = roomId; | |
log("SPARK_LOG: all set with SparkLog to room: " + this.roomId); | |
} | |
// This function sends the log entry to the registered Cisco Spark Room | |
// 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 = "[empty log]"; | |
} | |
var result; | |
try { | |
// Open Connection | |
var url = "https://api.ciscospark.com/v1/messages"; | |
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("Authorization", "Bearer " + this.token); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
//Send Post Data | |
var bodyWriter = new java.io.DataOutputStream(connection.getOutputStream()); | |
var contents = '{ "roomId": "' + this.roomId + '", "text": "' + newLogEntry + '" }'; | |
bodyWriter.writeBytes(contents); | |
bodyWriter.flush(); | |
bodyWriter.close(); | |
var result = connection.getResponseCode(); | |
if (result < 200 || result > 299) { | |
log("SPARK_LOG: could not log to Spark, response code: " + result); | |
return false; | |
} | |
} | |
catch (e) { | |
log("SPARK_LOG: could not log to Spark, socket Exception or Server Timeout"); | |
return false; | |
} | |
return true; // success | |
} | |
// info level used to get a synthetic sump up of what's happing | |
var SparkInfo = new SparkLog("CHATOPS_BOT_TOKEN", "CHATOPS_ROOM_ID"); | |
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); | |
} | |
// | |
// Script logic starts here | |
// | |
info("Script version 2017-01-05 12:52"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment