Reputation: 510
Hi I am trying to write a bot that translate whatever user write into Finnish. I have configured the default fallback intent that take whatever the user say and translated it, but it does not work
function translateStuff(input){
return new Promise(function(resolve, reject) {
googleTranslate.translate(input, 'eng', function(err, translation) {
if (err !== null) reject(err);
else resolve(translation.translatedText);
});
});
}
function fallback(agent) {
var userInput = agent.query;
translateStuff('kuka sina olet').then(function(value) {
agent.add(value);
});
}
But the fallback function does not return the translated text, does any one know what is the problem. Thank you
Upvotes: 0
Views: 316
Reputation: 2166
function handlers support promises now so you can return a promise and process things like http requests in the promise. Here is an example of using the request library:
function dialogflowHanlderWithRequest(agent) {
return new Promise((resolve, reject) => {
request.get(options, (error, response, body) => {
JSON.parse(body)
// processing code
agent.add(...)
resolve();
});
});
};
You can also move the HTTP call to another function that returns a promise. Here is an example with the axios library:
function dialogflowHandlerWithAxios(agent) {
return callApi('www.google.com').then(response => {
agent.add('My response');
}).catch (error => {
// do something
})
};
function callApi(url) {
return axios.get(url);
}
Does this satisfy your use case? You just need to put your translate function in place
Upvotes: 1