I'm using an Azure Web App Bot (Node v 6.9.1) and when the Test in Web Chat interface is initialized, text and textLocale are null. But when the first input is received from the user, event.textLocale is assigned a default 'en' value so I'm not able to reset this just once from bot.use(receive) after doing a language detect (as it is not null at this stage).
Is there an alternative way to reset event.textLocale just once as I'm not able to propagate the locale value through any global node.js variable from bot.use(receive) to bot.use(send) event either? Please help, thanks
app.js Snippet
bot.use({
receive: function (event, next) {
var text = '';
async.waterfall([
function(callback) {
text= event.text;
textLocale = event.textLocale;
request.get(
'http://<<<hostname>>>/translateIntent?intent='+text.toString()+'&textLocale='+(textLocale).toString(),
function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonStr = JSON.parse(body);
event.text=jsonStr.outputText;
// Storing the user's text locale if not already set
// - does not execute as event.textLocale is set by default and global variables are not propagating from receive to send
if(!event.textLocale && (jsonStr.inputLanguageContext != "(Unknown)")) {
event.textLocale = jsonStr.inputLanguageContext;
console.log("Rewriting event text locale to :" + jsonStr.inputLanguageContext);
}
callback(null, event);
}
}
);
},
function(event, callback) {
console.log("Rec after modification:");
console.log("event.textLocale: " + event.textLocale);
next();
callback(null, 'done');
}
]);
},
send: function (event, next) {
// translate response using event.textLocale as languageTo {event.text, languageFrom='en', event.textLocale}
}
});
translate api:
app.get("/translateIntent", function(request, response) {
var inputText = request.param('intent');
if(inputText) {
var textLocale = ''; var outputText = '';
// If locale is already set, languageDetectHelper does not need to be called
if(request.param('textLocale')) {
textLocale = request.param('textLocale');
if(textLocale == 'en') {
// No translation required, just send back the inputText
response.end({outputText:inputText, inputLanguageContext:languageCodeDetected});
} else {
// Call translate, with languageFrom as textLocale, and languageTo = 'en'
translateHelper.GetTranslation(inputText, textLocale, languageTo).then(jsonStr => {
response.end(jsonStr);
}).catch(error => {
console.log("Error: " + error);
})
}
} else {
// Locale not yet set, call detectLanguage
languageDetectHelper.GetTextLanguage(inputText).then(languageCodeDetected => {
var languageTo = 'en';
if(languageCodeDetected == 'en' || languageCodeDetected == '(Unknown)') {
// Send back without translation
response.end({outputText:inputText, inputLanguageContext:languageCodeDetected});
} else {
// Translate to English
translateHelper.GetTranslation(inputText, languageCodeDetected, languageTo).then(jsonStr => {
console.log("JSON stringify output in server: " + JSON.stringify(jsonStr));
response.end(jsonStr);
}).catch(error => {
console.log("Error: " + error);
})
}
}).catch(error=> {
console.log("Error: " + error);
});
}
} else {
console.log("No translation required, waiting for the next event");
}
});