Skip to content

Instantly share code, notes, and snippets.

@ObjectIsAdvantag
Last active December 10, 2016 22:19
Show Gist options
  • Save ObjectIsAdvantag/f2b54f811e50c3909d081728a4a07111 to your computer and use it in GitHub Desktop.
Save ObjectIsAdvantag/f2b54f811e50c3909d081728a4a07111 to your computer and use it in GitHub Desktop.
Snippet to invoke Recast.ai from Tropo
// POST https://api.recast.ai/v2/request
// Authorization=Token YOUR_RECAST_TOKEN
// {
// "text" : "I need somone speaks swedish",
// "language" : "en"
// }
function invokeRecast(token, text, language) {
try {
// Open Connection
var url = "https://api.recast.ai/v2/request";
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", "Token " + token);
// TODO : check if this cannot be removed
var payload = '{ "text": "' + text + '", "language": "' + language + '" }';
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 successfully");
return null;
}
// Read stream and create response from JSON
var bodyReader = connection.getInputStream();
var contents = new String(org.apache.commons.io.IOUtils.toString(bodyReader));
var parsed = JSON.parse(contents);
return parsed;
}
catch (e) {
log("RECAST: could not log to Spark, socket Exception or Server Timeout");
return null;
}
return { "results": { "uuid":1 }};
}
var response = invokeRecast(recast, "I need somone speaks swedish", "en");
log("TEST: " + response.results.uuid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment