Skip to content

Instantly share code, notes, and snippets.

@ObjectIsAdvantag
Last active March 17, 2017 11:45
Show Gist options
  • Save ObjectIsAdvantag/7f6e05eaeb38dd145ef66ea0bc3232e0 to your computer and use it in GitHub Desktop.
Save ObjectIsAdvantag/7f6e05eaeb38dd145ef66ea0bc3232e0 to your computer and use it in GitHub Desktop.
Tropo script skeleton to create a Signaling Service
// Constructor for the Cisco Spark Logging Library
// - token: we recommend to leverage a Cisco Spark Bot Account
// - roomId: add your bot to the room you want to chat to
function SparkLog(token, roomId) {
this.token = token;
this.roomId = roomId;
}
SparkLog.prototype.log = function(newLogEntry) {
var result;
try {
// Open Connection
var url = "https://api.ciscospark.com/v1/messages";
var connection = new java.net.URL(url).openConnection();
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 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
}
// Posts to the Presence signalments space
function signal(msg) {
log("SIGNAL: msg");
var spark = new SparkLog("SPARK_TOKEN", "ROOM_ID");
return spark.log(msg);
}
// Posts to a Chatops space
function chatops(entry) {
log("DEBUG: entry");
var spark = new SparkLog("SPARK_TOKEN", "ROOM_ID");
return spark.log(entry);
}
//
// Tropo Code logic starts here
//
if (currentCall) {
wait(1000);
say("Welcome to the Presence Signaling Service");
chatops("spoke welcome message to callerId: " + currentCall.callerID);
wait(500);
ask("Dial 1 if you'll be late today, 2 if you won't join, and 3 to be transferred to an operator", {
choices: "1, 2, 3",
terminator: "#",
attempts: 3,
mode: "dtmf",
onBadChoice: function (event) {
switch(event.attempt) {
case 1: say("Your entry is not a valid."); break;
case 2: say("Sorry, still invalid."); break;
default: say("Looks like you did not make it."); break;
}
},
onChoice: function (event) {
chatops("caller: " + currentCall.callerID + " chose " + event.value);
var selected = parseInt(event.value) || 3;
switch (selected) {
case 1:
if (signal("Employee (tel: " + currentCall.callerID + ") will be late today")) {
say("Your delay has been signaled to the Operations team");
}
else {
say("Sorry, could not contact the Operations team, please retry...");
}
break;
case 2:
if (signal("Employee (tel: " + currentCall.callerID + ") won't join today")) {
say("Your absence has been signaled to the Operations team");
}
else {
say("Sorry, could not contact the Operations team, please retry...");
}
break;
case 3: // This is the default
default:
signal("Employee (tel: " + currentCall.callerID + ") asked to be transferred to an agent");
say("Got it, now transferring you. Please hold on...");
transfer("objectisadvantag@sip2sip.info", {
playvalue: "http://www.phono.com/audio/holdmusic.mp3",
terminator: "*",
onTimeout: function(event) {
chatops("transfer timeout for callerId: " + currentCall.callerID);
say("Sorry, nobody's available. Please contact us later....");
signal("Employee (tel: " + currentCall.callerID + ") could not be transferred to an agent");
},
onBusy: function(event) {
chatops("line busy for callerId: " + currentCall.callerID);
say("Sorry, line is busy. Please try again later...");
signal("Employee (tel: " + currentCall.callerID + ") could not be transferred to an agent");
},
onConnect: function(event) {
chatops("transfer successful for callerId: " + currentCall.callerID);
say("You're now connected !");
},
});
break;
}
}
});
say("Bye bye !");
wait(500);
}
else {
chatops("tropo script version: 2017-03-17 12:42");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment