0

我开始使用 MS 的 Bot Frameword,我正在尝试构建一个简单的机器人。

问题是,如果我在对话框中发送消息,我无法结束该对话框。

这是我的代码:

我的机器人的入口点文件

var restify = require('restify');
var builder = require('botbuilder');
require('dotenv').load();
var fs = require('fs');

//Project modules
var consts = require('./modules/consts');
var u = require('./modules/utils');
var ds = require('./modules/dialogs');

if (process.env.config == consts.PRODENV) {
    u.dlog('Production environment; loading HTTPS');
    var server = restify.createServer({
        key: fs.readFileSync(process.env.keyPath || './ssh/key'),
        certificate: fs.readFileSync(process.env.certPath || './ssh/cert')
    });
} else {
    u.dlog('Development environment; loading HTTP');
    var server = restify.createServer();
}

/*Definindo variáveis*/
//Lista de comandos
var commands = {
    evento: {
        pattern: 'evento',
        action: builder.DialogAction.beginDialog('evento')
    },
    eventos: {
        pattern: 'eventos',
        action: builder.DialogAction.beginDialog('eventos')
    },
    convidados: {
        pattern: 'convidados',
        action: builder.DialogAction.beginDialog('convidados')
    },
    cancelar: {
        pattern: 'cancelar',
        action: builder.DialogAction.endDialog()
    }
}

//Objeto de bot principal
var bot = new builder.BotConnectorBot();
//Roteador de comandos
var cm = new builder.CommandDialog();

//Adicionando comandos
for (command in commands) {
    cm.matches("/?%s(.*)".replace('%s', commands[command].pattern), commands[command].action);
}

//Diálogo inicial
cm.onDefault(function(session) {
    session.send('Hey there!');
});

//Rota padrão
bot.add('/', cm);

//Adicionando diálogos
for (dialog in ds) {
    bot.add(dialog, ds[dialog].flow);
}

//Inicializando servidor
if (process.env.config == consts.PRODENV) {
    server.use(bot.verifyBotFramework({ appId: 'events-organizer-bot', appSecret: process.env.appSecret }));
}
server.post(process.env.uri || '/', bot.listen());
server.listen(process.env.port || 8080, function() {
    u.dif(function() {
        console.log('%s listening to %s', server.name, server.url);
    })
})

对话框.js

module.exports = {
    "evento": {
        flow: function(session) {
            session.send('*evento*: implementando');
            session.endDialog();
        }
    },
    "eventos": {
        flow: function(session) {
            session.send('*eventos*: implementando');
            session.endDialog();
        }
    },
    "convidados": {
        flow: function(session) {
            session.send('*convidados*: implementando');
            session.endDialog();
        }
    }
}

我从该代码中得到的异常:

Session Error: builder is not defined

我知道我可以在不启动对话的情况下发送消息,但是这些对话会变得更加复杂(当然)。如果我想确认用户发送到对话框的信息然后结束它怎么办?

----------------------------- 编辑 1 -------------------- ----------

更新后我重新运行了我的代码,但我仍然遇到异常。

代码如下:

入口点:

var restify = require('restify');
var builder = require('botbuilder');
require('dotenv').load();
var fs = require('fs');

//Project modules
var consts = require('./modules/consts');
var u = require('./modules/utils');
var ds = require('./modules/dialogs');

if (process.env.config == consts.PRODENV) {
    u.dlog('Production environment; loading HTTPS');
    var server = restify.createServer({
        key: fs.readFileSync(process.env.keyPath || './ssh/key'),
        certificate: fs.readFileSync(process.env.certPath || './ssh/cert')
    });
} else {
    u.dlog('Development environment; loading HTTP');
    var server = restify.createServer();
}

/*Definindo variáveis*/
//Lista de comandos
var commands = {
    evento: {
        pattern: 'evento',
        action: builder.DialogAction.beginDialog('evento')
    },
    eventos: {
        pattern: 'eventos',
        action: builder.DialogAction.beginDialog('eventos')
    },
    convidados: {
        pattern: 'convidados',
        action: builder.DialogAction.beginDialog('convidados')
    },
    cancelar: {
        pattern: 'cancelar',
        action: builder.DialogAction.endDialog()
    }
}

//Objeto de bot principal
var bot = new builder.BotConnectorBot();
//Roteador de comandos
var cm = new builder.CommandDialog();

//Adicionando comandos
for (command in commands) {
    cm.matches("/?%s(.*)".replace('%s', commands[command].pattern), commands[command].action);
}

//Diálogo inicial
cm.onDefault(function(session) {
    session.send('Hey there!');
});

//Rota padrão
bot.add('/', cm);

//Adicionando diálogos
for (dialog in ds) {
    bot.add(dialog, ds[dialog].flow);
}

//Inicializando servidor
if (process.env.config == consts.PRODENV) {
    server.use(bot.verifyBotFramework({ appId: 'events-organizer-bot', appSecret: process.env.appSecret }));
}
server.post(process.env.uri || '/', bot.listen());
server.listen(process.env.port || 8080, function() {
    u.dif(function() {
        console.log('%s listening to %s', server.name, server.url);
    })
})

对话框.js

module.exports = {
    "evento": {
        flow: function(session) {
            //session.send('*evento*: implementando');
            session.endDialog('*evento*: implementando');
        }
    },
    "eventos": {
        flow: function(session) {
            //session.send('*eventos*: implementando');
            session.endDialog('*eventos*: implementando');
        }
    },
    "convidados": {
        flow: function(session) {
            //session.send('*convidados*: implementando');
            session.endDialog('*convidados*: implementando');
        }
    }
}

调用默认对话框很好,但调用任何其他对话框都会Session Error: Maximum call stack size exceeded在我脸上抛出异常。

不过,如果我不将任何参数传递给 enDialog 方法,则不会有任何问题。

4

1 回答 1

0

抱歉耽搁了...我不知道您为什么会遇到该特定异常,但是现在存在与发送消息有关的问题,然后无法在同一个呼叫中结束对话。我有一个修复/改进,可让您在调用 endDialog() 时传递一条消息。让我今天尝试推出解决此问题的 0.7.0 更新。

于 2016-04-06T19:16:55.573 回答