0

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.'

Roy J
  • 23
  • 1
  • 6
  • If you need to decide what the intent is after the request returns, you need to put that logic in the request callback so it happens at the right time. – Mark Aug 15 '18 at 04:46
  • I have 2 http requests in this case. 1) LUIS-dispatcher http call . 2) LUIS app http call 2nd is in the switch case which should be executed only when case 'l_Dev': for all other cases I don't need to run http request. So what should I do in this case. – Roy J Aug 15 '18 at 14:05
  • @RoyJ there is no break; statement in your 'l_Dev' case. This would probably mean that it's hitting case 'l_Dev' then firing into case 'None' right after. Try adding a break and see if that resolves the issue – Zeryth Aug 20 '18 at 18:53

0 Answers0