【问题标题】:Bot Framework: How to create a menu in Node.js SDK?Bot Framework:如何在 Node.js SDK 中创建菜单?
【发布时间】:2019-05-16 20:41:09
【问题描述】:

我正在使用 Bot Framework 和 Node.js 开发一个聊天机器人。我的目的是用菜单引导用户。我想用户打开机器人,它会自动提示:“嗨,我是你的机器人,你在找什么?- option1 - option2”

一旦用户单击其中一个选项,他可以问我几个与该选项相关的问题,我想我会附上一个特定的 QnA Maker 知识库。

我尝试在网上搜索,并查找了发布在 Github 上的示例,但没有太大帮助。任何人都可以帮我举一个实际的例子吗? 提前致谢。

【问题讨论】:

    标签: node.js botframework azure-bot-service qnamaker


    【解决方案1】:

    一般来说,您可以创建多个 QnAmaker 服务,并在您的机器人应用程序中定义多个具有不同 kbs 的 QnAMakerRecognizer,然后在您的机器人瀑布中利用 recognize() 或 QnAMakerRecognizer 来匹配问题,由您的自己的条件。

    快速示例:

    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);
            })
        }
    ])
    

    【讨论】:

    • 非常感谢!只是另一个问题,我如何将 QnA 识别器和 Luis 结合起来?我仍然阅读了一些内容,但没有太大帮助,场景相同,我仍然在两个 QnA maker kb 上配置了 2 个 Luis 服务,如何进行集成?
    • 是的,你可以参考github.com/Microsoft/BotBuilder-CognitiveServices/tree/master/…的样品,应该可以解决你的要求。
    【解决方案2】:

    您可以使用prompts.Choice 执行此操作。这将为用户提供每个选项的按钮 - 用户可以单击它们或键入响应。

    所以,如果您定义了QnAMaker dialog ...

    var recognizer = new cognitiveservices.QnAMakerRecognizer({
        knowledgeBaseId: 'set your kbid here', 
        subscriptionKey: 'set your subscription key here'});
    
    var basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({
        recognizers: [recognizer],
        defaultMessage: 'No match! Try changing the query terms!',
        qnaThreshold: 0.3
    });
    
    bot.dialog('/QnAMakerDialogue', basicQnAMakerDialog);
    

    您可以使用replaceDialog 切换到此对话框,具体取决于用户的选择...

    function (session, results) {
         builder.Prompts.choice(session, "Hi I'm your bot you what are you looking for?", ["Ask a question", "Other cool stuff"], {listStyle: builder.ListStyle.button});
    },
    function (session, results) {
         if(results.response) {
              switch (results.response.entity) {
                    case 'Ask a question':
                       session.replaceDialog('/QnAMakerDialogue');
                    case 'Other cool stuff':
                       session.replaceDialog('/CoolStuffDialog');
                    default:
                        session.send("Something went horribly wrong");
                        return;
              }
         } 
    }
    

    如果您的用户已回复他们想提出问题,您将需要提示该问题。为此,我有时会使用包装对话框 QnAPromptDialogue ...

    function (session,args,next) {
        //if the user just entered 'ask question' or similar, prompt for the actual question
        var regex  = new RegExp("^ask .*");
        if(regex.test(session.message.text)) {
            builder.Prompts.text(session, "Go ahead, what is your question?");
        } else {
            next();
        }  
    },
    function (session, results) {
        session.replaceDialog('/QnAMakerDialogue');
    }
    

    可能不适合所有人,但如果有帮助,也可以考虑。

    【讨论】:

    • 这个想法应该是创建另一个“var识别器2”,我将第二个知识库的数据放在QnA中对吗?查看第二个 sn-p 我尝试将代码复制并粘贴到我的内部,但它会产生错误,所以我尝试如下:'bot.dialog('/', [ function (session,result) { builder.Prompts.choice(session ,“嗨,我是你的机器人,你在找什么?”,[“问一个问题”,“其他很酷的东西”],{listStyle:builder.ListStyle.button}); } ...'但是,如何如果我想加入 QnA kb,我的“/QnAMakerDialogue”对话框看起来像?我希望在单击时进入规范 kb
    • 为了结合 QnA Maker 和 LUIS,these samples 工作得非常好,你应该可以查看 repo 并运行。
    • 我在上面添加了一个编辑来回答你的第二个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多