Created
June 1, 2016 20:03
-
-
Save joshuajnoble/8fc90e1273ab5a2064bd0fd27876f187 to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
// Quickstart example | |
// See https://wit.ai/l5t/Quickstart | |
// When not cloning the `node-wit` repo, replace the `require` like so: | |
const Wit = require('node-wit').Wit; | |
//const Wit = require('../').Wit; | |
var request = require('request'); | |
const token = "6ST6EYTHPRWF7DGQ5ZQGHIIK63KBAHZX"; | |
const session = 'my-user-session-42'; | |
const context0 = {}; | |
// what is this? | |
const firstEntityValue = (entities, entity) => { | |
const val = entities && entities[entity] && | |
Array.isArray(entities[entity]) && | |
entities[entity].length > 0 && | |
entities[entity][0].value | |
; | |
if (!val) { | |
return null; | |
} | |
return typeof val === 'object' ? val.value : val; | |
}; | |
function yandexSanitizer(input) | |
{ | |
var tmp = ""; | |
Object.getOwnPropertyNames(input).forEach(function(val, idx, array) { | |
//console.log(val + ' -> ' + body[val]); | |
tmp += input[val]; | |
}); | |
tmp.replace('/'); | |
tmp = tmp.split('}')[0] + '}'; | |
var res = JSON.parse(tmp); | |
return res; | |
}; | |
// these are the actions, don't totally understand these | |
const actions = { | |
say(sessionId, context, message, cb) { | |
//console.log(message); | |
cb(); | |
}, | |
merge(sessionId, context, entities, message, cb) { | |
// Retrieve the location entity and store it into a context field | |
const lang = firstEntityValue(entities, 'language'); | |
const phrase = firstEntityValue(entities, 'phrase_to_translate'); | |
if (lang) { | |
context.lang = lang; | |
context.phrase = phrase; | |
console.log(context); | |
} | |
cb(context); | |
}, | |
error(sessionId, context, error) { | |
// console.log(" error?? "); | |
console.log(error.message); | |
}, | |
['do_translate'](sessionId, context, cb) { | |
// Here should go the api call, e.g.: | |
// var dataString = "https://translate.yandex.net/api/v1.5/tr.json/translate?"+ | |
// "key=trnsl.1.1.20160531T182702Z.716ab31006556b85.f1843370ddb632510512306b779caa20a45c80c7&text=What%27s+your+name&lang=Danish"; | |
var dataString = "https://translate.yandex.net/api/v1.5/tr.json/translate?"+ | |
"key=trnsl.1.1.20160531T182702Z.716ab31006556b85.f1843370ddb632510512306b779caa20a45c80c7&text=" + encodeURIComponent(context.phrase) + "&lang=" + context.lang; | |
request(dataString, function (error, response, body) { | |
// console.log("making a request"); | |
//Check for error | |
if(error){ | |
return console.log('Error:', error); | |
} | |
//Check for right status code | |
if(response.statusCode !== 200){ | |
return console.log('Invalid Status Code Returned:', response.statusCode); | |
} | |
var ress = yandexSanitizer(body); | |
//context.result = 'sunny'; | |
context.translation_result = ress.text; | |
cb(context); | |
}); | |
}, | |
}; | |
const client = new Wit(token, actions); | |
//client.interactive(); | |
client.runActions(session, 'How do you say what is your name in Spanish?', context0, (e, context1) => { | |
if (e) { | |
console.log('Oops! Got an error: ' + e); | |
return; | |
} | |
//console.log('The session state is now: ' + JSON.stringify(context1)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment