Skip to content

Instantly share code, notes, and snippets.

@ObjectIsAdvantag
Last active July 5, 2016 11:41
Show Gist options
  • Save ObjectIsAdvantag/7da823d4466349fe8d531f22346d56b1 to your computer and use it in GitHub Desktop.
Save ObjectIsAdvantag/7da823d4466349fe8d531f22346d56b1 to your computer and use it in GitHub Desktop.
DEVNET3002 - Step 2 - SMS Bridge
//
// Cisco Spark Client Library for Tropo
//
// Factory for the Spark Library, with 1 parameter
// - the Spark API token
function SparkClient(spark_token) {
if (!spark_token) {
log("SPARK_CLIENT : bad configuration, no API token, exiting...");
throw createError("SparkClient configuration error: no API token specified");
}
this.token = spark_token;
log("SPARK_CLIENT: all set; ready to invoke spark");
}
// Returns a status code
SparkClient.prototype.createMemberShip = function(roomID, email) {
// Robustify
if (!roomID) {
return 400;
}
if (!email) {
return 400;
}
var result;
try {
// Open Connection
var url = "https://api.ciscospark.com/v1/memberships";
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");
connection.setRequestProperty("Authorization", "Bearer " + this.token);
// Prepare payload
var payload = '{ "roomId": "' + roomID + '", "personEmail": "' + email + '", "isModerator": "false" }'
// [TODO] Check if this cannot be removed
connection.setRequestProperty("Content-Length", payload.length);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send Post Data
var bodyWriter = new java.io.DataOutputStream(connection.getOutputStream());
log("SPARK_CLIENT: posting: " + payload + " to: " + url);
bodyWriter.writeBytes(payload);
bodyWriter.flush();
bodyWriter.close();
result = connection.getResponseCode();
log("SPARK_CLIENT: read response code: " + result);
}
catch (e) {
log("SPARK_CLIENT: could not log to Spark, socket Exception or Server Timeout");
return 500;
}
if (result < 200 || result > 299) {
log("SPARK_CLIENT: could not add user with email: " + email + " to room:" + roomID);
}
else {
log("SPARK_CLIENT: user with email: " + email + " added to room:" + roomID);
}
return result; // success
}
//
// Custom Tropo script logic starts here
//
// 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;
}
// Adds the user with email to the room,
// Returns an HTTP status referenced here: https://developer.ciscospark.com/endpoint-memberships-post.html
function addToQuizRoom(email) {
var client = new SparkClient("SPARK_TOKEN");
var result = client.createMemberShip("ROOM_ID", email);
return result;
}
// STARTS TROPO SESSION
if (currentCall) { // SMS
var input = currentCall.initialText;
var extractedEmail = extractEmail(input);
if (!extractedEmail) { // send Welcome message
say("Welcome to the DevNet Quiz. Text your email to join the Quiz room");
}
else { // register to the Sandbox Room
var statusCode = addToQuizRoom(extractedEmail);
switch (statusCode) {
case 200:
say("Thanks for joining, " + extractedEmail + " is now part of the 'DevNet Quiz' room");
break;
case 409:
say("You're all set, " + extractedEmail + " is already a member of the 'DevNet Quiz' room");
break;
default:
say("sorry but we could not add " + extractedEmail + " to the 'DevNet Quiz' room, try again...");
break;
}
}
}
// END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment