node.js - 提示不等待用户输入

标签 node.js azure botframework prompt chatbot

我目前正在尝试使用 Microsoft Bot Framework 编写聊天机器人。我想使用选择提示,但它不会等待我/用户选择选项。甚至没有 here 中的示例代码为我工作。请参阅此工作示例:

var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 

});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata 
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var bot = new builder.UniversalBot(connector);
bot.set('storage', new builder.MemoryBotStorage()); 

var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || 'westus.api.cognitive.microsoft.com';

var LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v2.0/apps/' + `enter code here`luisAppId + '?subscription-key=' + luisAPIKey;

/**
 * RECOGNIZERS
 */

var recognizer = new builder.LuisRecognizer(LuisModelUrl);
bot.recognizer(recognizer);


/**
 * DIALOG SECTION
 */
 var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);



   intents.onDefault([
        function(session){
            session.send('I am sorry but I don`t know that.');
        }
    ]);


intents.matches('City' ,[
     function(session, args) {

        session.beginDialog('getLocationDialog');
        session.endDialog();
    } 
]);

 var locationLabels = {
     City1: 'City 1',
     City2: 'City 2',
     City3: 'City 3'
 };

 bot.dialog('getLocationDialog', [
    function (session) {
       builder.Prompts.choice(session, "Choose a city", locationLabels
           , { listStyle: builder.ListStyle.button },{
                maxRetries: 3,
                retryPrompt: 'This is not a valid option. Choose one of the options listed above.' }); 
       },
    function (session, results) {
        console.log(results.response.entity);
        if (results.response){
            var location = locationLabels[results.response.entity];
            session.send('You are interested in %s', location);
            session.endDialog();
        }
        else {
           session.send('Okay.');
           session.endDialog(); 
        }
    }
]);  

带有提示的对话框就是这部分(使用LUIS来匹配意图):

intents.matches('City' ,[
     function(session, args) {
   session.beginDialog('getLocationDialog');
   session.endDialog();
   } 
]);

 var locationLabels = {
     City1: 'City 1',
     City2: 'City 2',
     City3: 'City 3'
 };

 bot.dialog('getLocationDialog', [
    function (session) {
       builder.Prompts.choice(session, "Choose a city", locationLabels
           , { listStyle: builder.ListStyle.button },{
                maxRetries: 3,
                retryPrompt: 'This is not a valid option. Choose one of the options listed above.' }); 
       },
    function (session, results) {
        console.log(results.response.entity);
        if (results.response){
            var location = locationLabels[results.response.entity];
            session.send('You are interested in %s', location);
            session.endDialog();
        }
        else {
           session.send('Okay.');
           session.endDialog(); 
        }
    }
]);  

查看此代码的实际操作 in action

机器人不会等待我选择选项。有人能告诉问题出在哪里吗?

最佳答案

请尝试下面列出的代码。我做了一些改变,让它对我有效。首先,删除“City”意图匹配中的 session.EndDialog() 调用。该调用导致对话框在原始对话框完成之前重新启动。

其次,您将 locationLabels 变量设置为键/值对。这需要是一个简单的数组。如果您需要使用键/值对,请考虑使用 iChoice。

第三,您的位置变量只需分配 results.response.entity。由于 locationLabels 是一个简单的数组,因此您不再需要尝试匹配它。

希望这有帮助。

史蒂夫。

var intents = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', intents);

intents.onDefault([
    function(session){
        session.send('I am sorry but I don`t know that.');
    }
]);

intents.matches('City' ,[
    function(session, args) {
        session.beginDialog('getLocationDialog');
        // session.endDialog();
    } 
]);

var locationLabels = ['City 1', 'City 2', 'City 3'];

bot.dialog('getLocationDialog', [
    function (session) {
        builder.Prompts.choice(session, "Choose a city", locationLabels, { listStyle: builder.ListStyle.button }); 
        },
    function (session, results) {
        console.log(results.response.entity);
        var location = results.response.entity;
        if (results.response){
            session.send('You are interested in %s', location);
            session.endDialog();
        }
        else {
           session.send('Okay.');
           session.endDialog(); 
        }
    }
]);

关于node.js - 提示不等待用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49897214/

相关文章:

node.js - PropTypes 已被弃用以及如何解决它

node.js - Babel 只是将 .js 文件处理到 dist 文件夹中

azure - 获取 ARM 中函数应用程序的 Webhook url 以用于事件网格订阅

node.js - 有没有办法从 Microsoft Bot Framework 中的 WaterfallStepContext 主动结束提示,即使用计时器或事件

c# - 当用户提交字段名称作为值时,FormFlow 禁用字段之间的切换

botframework - 微软机器人框架审查流程

node.js - 我应该更改环境变量以在 Windows 中的 Cygwin 下使用 npm 脚本吗?

javascript - 单独的日志级别

powershell - VSTS Azure PowerShell 任务 SQL 防火墙规则

c# - Azure 计划程序 Rest API - 更新作业错误 : Schedules are not supported for recurrence unit 'Hour'