【问题标题】:conversationUpdate event not getting user input对话更新事件未获得用户输入
【发布时间】:2017-07-11 02:59:29
【问题描述】:

当用户第一次连接时,我使用以下方法通过机器人提问。但是在用户回答问题后,机器人不会进入下一步,而是进入新对话框。

这适用于模拟器,但不适用于直接 api。

bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
        message.membersAdded.forEach(function (identity) {
            if (identity.id === message.address.bot.id) {
                bot.beginDialog(message.address, '/startConversation');
            }
        });
    }
});

bot.dialog('/startConversation', [
    function (session) {
        builder.Prompts.text(session, 'I\'m the SPF VA Assistant. How may I address you?');
    },
    function (session, results) {
        session.userData.name = results.response;
        session.send('Hi, %s, Please choose one of the options below to lodge a Police report.', session.userData.name);
        var cards = getReportsAsCards();
        var reportOptions = new builder.Message(session)
            .attachmentLayout(builder.AttachmentLayout.carousel)
            .attachments(cards);
        session.send(reportOptions).endDialog();
    }
]);

【问题讨论】:

    标签: node.js botframework


    【解决方案1】:

    在您发送“请选择以下选项之一...”的问题后,您将致电endDialog()。这就是导致它像您所说的那样进入新对话框的原因。

    如果您想prompt the user for input,请使用Choice Prompt,并在您的瀑布对话框中添加另一个步骤来处理用户的选择输入。

    例如,要获取用户的姓名,首先使用文本提示提出问题,然后在瀑布对话框的第 2 步中处理用户的响应。用户的响应文本将在步骤 2 中的 results.response 对象中提供。

    bot.dialog('greetings', [
        // Step 1
        function (session) {
            builder.Prompts.text(session, 'Hi! What is your name?');
        },
        // Step 2
        function (session, results) {
            session.endDialog('Hello %s!', results.response);
        }
    ]);
    

    要提示用户进行一系列选择,请使用瀑布对话框和builder.Prompts.choice() 调用相结合。例如:

    var noiseComplaintName = "Noise Complaint";
    var trashDumpingName = "Trash Dumping";
    var weirdSmellName = "Weird Smell";
    var trafficSignalName = "Traffic Signal";
    
    var reportTypes = [
        noiseComplaintName,
        trashDumpingName,
        weirdSmellName,
        trafficSignalName
    ];
    
    bot.dialog('pick-report-type', [
        function(session) {
            session.send('Choice prompt example:');
    
            var promptOptions = {
                listStyle: builder.ListStyle.button
            };
    
            builder.Prompts.choice(session, "What do you want to report?", reportTypes, promptOptions);
        },
        function (session, results) {
            // handle response from choice prompt
            var selectedReportType = results.response.entity;
    
            switch (selectedReportType) {
                case noiseComplaintName:
                    // handle response here
                    break;
                case trashDumpingName:
                    // handle response here
                    break;
                case weirdSmellName:
                    // handle response here
                    break;
                case trafficSignalName:
                    // handle response here
                    break;
                default
                    // handle response here
                    break;
            }
        }
    ]);
    

    有关将提示与Rich Cards 结合的更详细示例,请查看 GitHub 上的示例代码:BotBuilder-Samples/Node/cards-RichCards

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2019-12-28
      • 1970-01-01
      • 2021-04-22
      相关资源
      最近更新 更多