我试图在控制台上回显更改的名称..但程序退出时,我输入新名称

时间:2016-06-11 09:15:11

标签: node.js botframework

我正在使用textbot来回显用户输入的名称,并添加了一个使用set name命令更改用户名的选项,但该程序仅采用新名称并且不会打印它。想知道出了什么问题?

这是我的代码

var builder = require('botbuilder');

var helloBot = new builder.TextBot();
helloBot.add('/', new builder.CommandDialog()
        .matches('^set name', builder.DialogAction.beginDialog('/profile'))
        .matches('^quit', builder.DialogAction.endDialog())
        .onDefault([
            function (session, args, next) {
                if (!session.userData.name) {
                    session.beginDialog('/profile');
                } else {
                    console.log('in else part..');
                    next();
                }
            },
            function (session, results) {
                session.send('Hello %s!', session.userData.name);
            }
            ]));
helloBot.add('/profile',  [
        function (session) {
            if (session.userData.name) {
                builder.Prompts.text(session, 'What would you like to change it to?');
                console.log('setting name..');
            } else {
                builder.Prompts.text(session, 'Hi! What is your name?');
            }
        },
        function (session, results) {
            session.userData.name = results.response;
            session.endDialog();
        }
        ]);

helloBot.listenStdin();

输出应该是这样的:

Hi
Hi! What is your name?
james
Hello james!
set name
What would you like to change it to?
Bond

程序在此处退出,而预期输出为 你好邦德!

1 个答案:

答案 0 :(得分:0)

问题是.onDefault永远不会被调用,因为根对话框符合“set name”条件。

试试这段代码(使用BotBuilder的更新版本):

var builder = require('botbuilder');

var connector = new builder.ConsoleConnector().listen();
var bot = new builder.UniversalBot(connector);
var intents = new builder.IntentDialog();
bot.dialog('/', intents);

intents.matches(/^set name/i, [
    function (session) {
        session.beginDialog('/profile');
    },
    function (session, results) {
        session.send('Hello %s!', session.userData.name);
    }
]);

intents.onDefault([
    function (session, args, next) {
        if (!session.userData.name) {
            session.beginDialog('/profile');
        } else {
            next();
        }
    },
    function (session, results) {
        session.send('Hello %s!', session.userData.name);
    }
]);

bot.dialog('/profile', [
    function (session) {
        if (session.userData.name) {
            builder.Prompts.text(session, 'What would you like to change it to?');
        } else {
            builder.Prompts.text(session, 'Hi! What is your name?');
        }
    },
    function (session, results) {
        session.userData.name = results.response;
        session.endDialog();
    }
]);