Search code examples
node.jsbotframeworkazure-bot-serviceazure-qna-maker

Bot Framework: How to create a menu in Node.js SDK?


I'm developing a chatbot using Bot Framework and Node.js. My purpose is to guide the user with a Menu. I imagine that the user open the bot and it automatically prompts: " Hi I'm your bot you what are you looking for? - option1 - option2 "

Once the user clicks on one of the option he can ask me several question connected to that option and I imagine I will attach a specific QnA Maker knowledgebase.

I tried seaching on the web and I looked for the samples posted on Github but are not so helpful. Anyone could help me with a pratical example? Thanks in advance.


Solution

  • Generally speaking, you can create several QnAmaker services, and define several QnAMakerRecognizer with different kbs in your bot application, then leverage recognize() of QnAMakerRecognizer in your bot waterfalls to match the questions, by your own conditions.

    For a quick sample:

    var recognizer1 = new cognitiveservices.QnAMakerRecognizer({
        knowledgeBaseId: <knowledgeBaseId>,
        subscriptionKey: <subscriptionKey>
    });
    var recognizer2 = new cognitiveservices.QnAMakerRecognizer({
        knowledgeBaseId: <knowledgeBaseId>,
        subscriptionKey: <subscriptionKey>
    });
    let QNARecognizer;
    bot.dialog('/', [(session, args) => {
            var msg = new builder.Message(session)
                .text("Select a choice")
                .suggestedActions(
                    builder.SuggestedActions.create(
                        session, [
                            builder.CardAction.imBack(session, "option1", "option1"),
                            builder.CardAction.imBack(session, "option2", "option2"),
                        ]
                    )
                );
            builder.Prompts.choice(session, msg, ["option1", "option2"]);
        }, (session, results,next) => {
            console.log(results);
            session.userData.kb = results.response.entity;
            switch (results.response.entity) {
                case 'option1':
                QNARecognizer = recognizer1;
                    break;
                case 'option2':
                QNARecognizer = recognizer2;
                    break;
                default:
                    session.endDialog('not matched');
            }
            builder.Prompts.text(session,'please ask your quesion');
        }, (session, results) => {
            QNARecognizer.recognize(session,(err,result)=>{
                session.send(result.answers[0].answer);
            })
        }
    ])