I implemented following custom recognizer for my bot. I followed this example where recognize method accepts context object as 1 param and callback as 2nd param. I am seeing an error which says 'Error: Callback was already called' I am sure its because done correctly.
done(null, intent);
bot.recognizer({
recognize: function (context, done) {
var defaultIntent = { score: 0.0 };
var noneIntent = {intent: 'None', score: 1.0 };
var intent;
if (context.message.text) {
request.get(`LUIS-dispatcher http call`, (err, res, dbody) => {
// Based on result from dispatcher
let dispatcherBody = JSON.parse(dbody)
let topIntent = getLUISTopIntent(dispatcherBody.intents)
let intentName = topIntent.name
switch (intentName) {
case 'l_Dev':
request.get(`LUIS app http call`, (err, res, lbody) => {
let luisBody = JSON.parse(lbody)
let topIntent = getLUISTopIntent(luisBody.intents)
let luisIntent = {}
let innerIntent = {}
innerIntent.entities = luisBody.entities
innerIntent.intent = topIntent.name
innerIntent.score = topIntent.score
innerIntent.intents = luisBody.intents
luisIntent.topIntent = innerIntent
luisIntent.intent = topIntent.name
luisIntent.score = topIntent.score
intent=luisIntent
**done(null, intent);**
});
case 'None':
intent = noneIntent;
**done(null, intent);**
break;
case 'q_QnA':
intent = { score: 1.0, intent: 'q_QnA'};
**done(null, intent);**
break;
default:
intent = defaultIntent
**done(null, intent);**
break;
}
})
}
//done(null, intent);
}
});
1) I need to make sure program control waits on switch (intentName) and then execute respective case. and call done(null, intent) 2) But before intentName value becomes available control goes to 2nd switch case ('None' in this case and calls done). later when 2nd http request finished control enters into 1st switch case, and call done.
this is time when I see error 'Error: Callback was already called.'