Last active
December 10, 2016 18:47
-
-
Save ObjectIsAdvantag/ad87bdfe5d41ba68bb61db59977b1781 to your computer and use it in GitHub Desktop.
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
// POST https://api.recast.ai/v2/request?Authorization=Token XXXXX | |
// { | |
// "text": "I need somone speaks swedish", | |
// "language" : "en" | |
// | |
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 }}; | |
} | |
var response = invoke(phonenumber, text); | |
log("TEST: " + response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment