Skip to content

Instantly share code, notes, and snippets.

@theFreedomBanana
Last active December 11, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theFreedomBanana/6d4939374c3248e8555824d76a2c8417 to your computer and use it in GitHub Desktop.
Save theFreedomBanana/6d4939374c3248e8555824d76a2c8417 to your computer and use it in GitHub Desktop.
// Receiving a message
if (currentCall) {
// Define the person sending the message
var callerId = currentCall.callerID;
var originalMessage = currentCall.initialText;
var messageReceived = currentCall.initialText.toLowerCase();
/*
//partie à envoyer dans Recast
//var command_recast = "";
*/
// We send the message to our VERTKAWAA server (dealing with the database of known phone numbers/users
var data = invoke(callerId, messageReceived);
call(callerId, { "network":"SMS" });
// Answer back to the person with their own message :
//say("message received : " + originalMessage);
say(data.question);
// Our answer :
}
// Traitement par POSTMAN
else {
var phone = "33628348162";
var response = invoke(phone, "inscrire");
//var data = '{"question":"Coool, quel est ton prenom ?"}';
call(phone, { "network":"SMS" });
say("Dernier jour");
}
function invoke(phonenumber, text) {
try {
// Open Connection
var url = "https://vertkawaa.localtunnel.me/tropo";
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");
// TODO : check if this cannot be removed
var payload = '{ "phonenumber": "' + phonenumber + '", "text": "' + text + '" }';
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("RECAST: posting: " + payload);
bodyWriter.writeBytes(payload);
bodyWriter.flush();
bodyWriter.close();
var result = connection.getResponseCode();
log("RECAST: read response code: " + result);
if (result < 200 || result > 299) {
log("RECAST: could not invoke recast");
return null;
}
// Read stream and create response from JSON
var bodyReader = connection.getInputStream();
// [WORKAROUND] We cannot use a byte[], not supported on Tropo
// var myContents= new byte[1024*1024];
// bodyReader.readFully(myContents);
var contents = new String(org.apache.commons.io.IOUtils.toString(bodyReader));
var parsed = JSON.parse(contents);
//log("JSON_LIBRARY: JSON is " + parsed.stringify());
return parsed;
}
catch (e) {
log("RECAST: could not log to Spark, socket Exception or Server Timeout");
return null;
}
//return { "results": { "uuid":1 }};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment